Browser Automation With Selenium Webdriver and Page Objects: Presented at Squad Colorado Meetup at Denver, Feb 09, 2016
Browser Automation With Selenium Webdriver and Page Objects: Presented at Squad Colorado Meetup at Denver, Feb 09, 2016
Browser Automation With Selenium Webdriver and Page Objects: Presented at Squad Colorado Meetup at Denver, Feb 09, 2016
• 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…
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
• 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
} }
} }
Login Page
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”);
}
}
@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!