Browser Automation With Selenium Webdriver and Page Objects: Presented at Squad Colorado Meetup at Denver, Feb 09, 2016

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Presented At SQuAD Colorado Meetup at Denver, Feb 09, 2016

Browser Automation with


Selenium WebDriver and
Page Objects
Harsh Murari
[email protected]
Software Architect
Rhoynar Software Consulting, Boulder, CO
www.rhoynar.com
About Me

• Co-founder and Software Architect at Rhoynar Software Consulting


• Co-founder and Principal Instructor at DestinationJ Software Technologies
• Working on Build, Infrastructure and Process Improvement Solutions
• 12+ years of Software Engineering Experience
About This Presentation: Objectives
• Understand Web Automation Testing Landscape
• Overview of Selenium WebDriver
• Moving from Manual Test to Selenium WebDriver
• Common Problems with Test Automation
• Understand Page Object Models Design Pattern
• Test Automation Best Practices
• Page Factories
• Test Auto-Generation
• Demo of Rhoynar’s AutoTestR Solution for Automatic Test Generation
Automation Testing Problems

• Automation Tests are hard to write – large initial investment


• Automation Tests are broken most of the time – large upkeep costs
• Changes to UI break tests – not sure if tests are broken or UI is broken
• Unsatisfactory Automation Quality – poorly written tests
• Scripts take too long to run
• Scripts can’t run unattended
• Reports are difficult to analyze
• Tests have no documentation
Manual Test Example – CRM Application
Manual Test Example – CRM Application
Requirements Tests
• Add a New Lead • Add a New Lead
• Import Leads • Create a Valid Lead

• View Leads • Create an Invalid Lead

• View Lead Details • Create a Duplicate Lead

• Contact A Lead • Import Leads

• Contact Multiple Leads


• Valid Import
• Import with Duplicates
• Update a Lead
• Import with esoteric fields
• Delete a Lead
• Support for different files like .csv, .xls, .xlsx etc.
• …
Manual Test Example – CRM Application
• Testing once – fine
• Efficient – no upfront investment
• Allows for exploratory testing
• Allows to look at general performance issues
• Testing multiple times – ???
• Testing at every major release? 4hrs * 8 Modules = 4days

• Testing at every minor release?


• Testing at every bug-fix?
• Testing at every pre-flight? AUTOMATION
Selenium WebDriver Automation
• What does Selenium WebDriver Provide?
• Framework for Browser Automation
• Standards Compliant, Open Source, Free-to-use
• All Major Browsers Support
• All Major Operating Systems Support
• All Major Languages Support
• Remote Invocation Support
• Mobile Platforms Support
• Cross-browser compatible scripts
• Object oriented
Selenium WebDriver: How does it work

• Lead Creation
• Get a handle to the browser/window/tab
• Load the webpage
• Click on Leads Link
• Click on Create Leads
• Fill in the information (Text Boxes,
Dropdowns, Check-Boxes etc.)
• Hit Save Button
Selenium WebDriver: How does it work
So What’s Wrong with this example
• Procedural/Repetitive
• Hard-Coded, no data providers
• Prone to break:
• New fields are added
• Xpath Changes
• Field Name/Property name
changes
• CSS properties change
• Not re-entrant
• Most times we are fixing tests
to keep up with code…

Need for Abstraction on Top of WebDriver


Need For Abstraction – Page Objects
Need For Abstraction – Page Objects
• Pages are people too, my
friend!
• Every page has feelings and
emotions attributes and
actions associated with it
• How these are implemented is
abstracted
• What’s exported are the
actions this webpage can
perform

PAGE OBJECT MODEL


Need For Abstraction – Page Objects

Test • Test/
Business
Code Logic • Separate Business Logic from
Browser Handling
Page • Driver/ • Each Page has an Object
Config/
Objects UI Logic • Exported methods: Page’s
Functionality
• Abstracted methods: Browser,
UI, WebElement handling
Browser • Browser
Page Objects - Example
Page Objects – Few Things to Note
• Notes:
• Annotations: Object is assumed to be
initialized through PageFactory.
• @FindBy can support: id, name, tagName,

Attributes
linkText, partialLinkText, css, xpath
• @CacheLookup annotation: Object is
assumed to never change in the DOM.
• All buttons/actions are usually translated into
functions.
• Functions use data providers to provide
configurable, re-entrant data.

Actions
Page Object Paradigm

• Expose the service you’re interacting with, not the implementation.


- Selenium Wiki

• If you’re using WebDriver API in your test methods… You are doing it wrong.
- Simon Stewart
Business Logic Driver Logic
Return value for exported services
Service Methods Returning void
public class LoginPage { public class AccountsPage {
public void login(){ public void viewAccountDetails(){
//Selenium Code //Selenium Code
} }
} }

Service Methods Returning correct Object

public class LoginPage { public class AccountsPage {


public AccountsPage login(){ public
//Selenium Code AccountsDetailsPage viewAccountDetails(){
} //Selenium Code
} }
}
Return Value - continued
Accounts Page

public class LoginPage {


public AccountsPage login(){
//Selenium Code
}
}

Login Page

• Return a generic Page Interface Object Instead


Timeouts

void testApplication() { void testApplication() {


loginPage = new LoginPage(); loginPage = new LoginPage();
accountsPage = loginPage.login(); accountsPage = loginPage.login();

Thread.sleep(3000); accountsPage.waitForPage();

accountsPage.viewAccounts(); accountsPage.viewAccounts();
} }
Page Factory public class LoginPage {
private WebElement email;
private WebElement password;

//Constructor
public LoginPage(WebDriver driver){
driver.findById(“email”);
driver.findById(“password”);
}
}

public class LoginPage {


@FindBy(id = “email”)
private WebElement email;

@FindBy(id = “password”)
private WebElement password;

//Constructor
public LoginPage(WebDriver driver){
PageFactory.initElement(driver, this);
}
}
Where do we stand?

Test/Business Logic

Auto
Page Object Page Object Page Object Generated!
Data Provider Data Provider Data Provider

Web App
Auto Generation of Page Object Code
• An advantage of putting structure to the
process gives us a model for auto-generating
object test code
• AutoTestR uses node.js framework to parse
the DOM for the page and generate
corresponding test object code.
One step further…
- Each node represents a page-object
(or rather business object)
- Each node specifies what are its
exported interfaces, what are its next
states
- Based on this graph, AutoTestR
auto-generates test case skeletons
as well!

- Example Test Cases:


- Login->Unregistered Page
- Login->Main Page->CreateUser->End
- Login->Main Page->SearchUser->End
- Login->Main Page->DeleteUser->End
- Login->Main Page->Unregistered Page
Auto Generated Test Cases: AutoTestR Demo
CI with AutoTestR
AutoTestR – Roadmap and Upcoming Features

• Open-source and release a community version by end of the year.


• More graph based intelligent testing, configurable edge weights.
• Integrating with Cloud Based Testing Infrastructure
• Separate Test Assertions from Auto-Generated Code
• Support for business logic testing
• Auto generate TestNG files for regression and performance testing
• Database based test key-value generation
Summary

• Separate Business Logic from Implementation Logic


• Return the Page Object (generic Page superclass) from exported services
• Page Load Wait time is a property of the page – keep it in the page object!
• Keep common utilities in the base class
• Use Data Providers
• Keep Exported methods Re-entrant
• AutoTestR – To Auto Generate Page Objects and Test Cases
Services: Courses:
- QA Automation Consulting - QA Masters (Manual testing, HP ALM/QC,
- Continuous Integration Selenium IDE, SQL)
- Web and Mobile Testing - QA Advanced (Java, SQL, Selenium WebDriver,
- Security Testing POM, BDD, Cucumber)
- IOT Testing - Full-Stack Web Development using NodeJS,
- QA Staffing AngularJS, ExpressJS and MongoDB
- Web Development (MEAN stack) - Corporate training (various topics) (1-5 days)
- Mobile App Development (iOS/Android)

Contact Us: Contact Us:


Rhoynar Software Consulting DestinationJ Software Technologies
Web: www.rhoynar.com Web: www.destinationj.com
Ph.: +1-855-5-RHOYNAR Ph.: +1-303-408-9848
Email: [email protected] Email: [email protected]

You might also like