CONFIDENTIAL Copyright © 2008 Constant Contact, Inc.1Using Page Objects in SeleniumEric GetchellQuality Engineering ManagerDonna ReardonPrincipal Quality Engineer
CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.2Record and Playback What are Utility Classes and how are they used in testing?What is a Page Object?Some Advantages of Page ObjectsRe-usability	Maintainability	Productivity GainsHow to create a Page ObjectDemoQ & A
Record and Playback – Early Automation Approach Testers can quickly develop test scripts, with little to no coding experience.
 Does NOT scale for 1k’s of tests, or even 100’s.
 Not easy to re-use a scenario as part of a larger user story.
 Requires a large amount of copy/paste to develop each test scenario.Perform a query for “Smith” in the last name field.	Perform a query for “Smith in the last name field, and “John” in the first	name field.CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.3
Utility ClassesUtility classes typically contain methods that are used often in your testing. It might contain methods for writing to a log file, writing to an event log, or parsing a data file. Methods can easily be duplicated across classes and hard to find inside large util classes.com.roving.taf.utility.NowString()com.roving.taf.utility.getCurrencyString(numberToConvertToString)CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.4
What is a Page Object?Page Objects are a representation of a web page. All elements seen on the web page can be interacted using their respective methods in the page object.Page Objects are classes that allow you to abstract the functionality from the GUI, removing the need for your tests to expose UI components.PageObject: app.addUser()BrowserObject: Window(“App”).button(“Add”).click() Application logic is then scripted in classes specific to your test scenarios.PageObjects + Test Code = scripted business logicCONFIDENTIAL Copyright © 2009 Constant Contact, Inc.5
What is a Page Object?Allows you to model the UI, but not recreate it
Exposes methods for actions in the UI, such as
UserRegistration.getFullname();
UserRegistration.setLastname();
Consolidates code specific to UI elements
getFullname() is found on User Registration page, not TemplatePicker page.

Using Page Objects

  • 1.
    CONFIDENTIAL Copyright ©2008 Constant Contact, Inc.1Using Page Objects in SeleniumEric GetchellQuality Engineering ManagerDonna ReardonPrincipal Quality Engineer
  • 2.
    CONFIDENTIAL Copyright ©2009 Constant Contact, Inc.2Record and Playback What are Utility Classes and how are they used in testing?What is a Page Object?Some Advantages of Page ObjectsRe-usability Maintainability Productivity GainsHow to create a Page ObjectDemoQ & A
  • 3.
    Record and Playback– Early Automation Approach Testers can quickly develop test scripts, with little to no coding experience.
  • 4.
    Does NOTscale for 1k’s of tests, or even 100’s.
  • 5.
    Not easyto re-use a scenario as part of a larger user story.
  • 6.
    Requires alarge amount of copy/paste to develop each test scenario.Perform a query for “Smith” in the last name field. Perform a query for “Smith in the last name field, and “John” in the first name field.CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.3
  • 7.
    Utility ClassesUtility classestypically contain methods that are used often in your testing. It might contain methods for writing to a log file, writing to an event log, or parsing a data file. Methods can easily be duplicated across classes and hard to find inside large util classes.com.roving.taf.utility.NowString()com.roving.taf.utility.getCurrencyString(numberToConvertToString)CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.4
  • 8.
    What is aPage Object?Page Objects are a representation of a web page. All elements seen on the web page can be interacted using their respective methods in the page object.Page Objects are classes that allow you to abstract the functionality from the GUI, removing the need for your tests to expose UI components.PageObject: app.addUser()BrowserObject: Window(“App”).button(“Add”).click() Application logic is then scripted in classes specific to your test scenarios.PageObjects + Test Code = scripted business logicCONFIDENTIAL Copyright © 2009 Constant Contact, Inc.5
  • 9.
    What is aPage Object?Allows you to model the UI, but not recreate it
  • 10.
    Exposes methods foractions in the UI, such as
  • 11.
  • 12.
  • 13.
  • 14.
    getFullname() is foundon User Registration page, not TemplatePicker page.
  • 15.
    Not tied toa specific framework
  • 16.
    Can be usedin Selenium, Webdriver, QTP, etc.CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.6
  • 17.
    The “-ilities” ofPage ObjectsAbstraction is the ability to remove the exact context of an object and replace it with a representation, in our case a Java class.Allows for elements to be represented in objected oriented languages.
  • 18.
    Allows for other-ilities, maintainability, re-useability, etc. public static final String TEXT_loginName = "username"; public static final String TEXT_loginPassword = "password"; CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.7
  • 19.
    Re-usability of PageObjectsPage objects are abstracted into classes. As such, they lend themselves greatly to re-useabilty in object oriented languages such as Java and C#. Decreases duplicate code
  • 20.
    Constants can beused and overloaded, depending on the data requirements or test case behavior.
  • 21.
    Hashmaps can beused as constants to prepopulate methods – and subsequently overloaded to replace values for different test scenariosselectColor(String color) public static booleanselectColorBlue() throws Exception { return selectColor("blue"); }CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.8
  • 22.
    Increased Maintainability ofTests using PageObjectsSince a single Page Object can be used across multiple testing scenarios (e.g. Login LP), and gets called by each test, you only need to update one class (Login.LP Page Object) when the page changes.
  • 23.
    This allows fortesters to quickly update their test scenarios.
  • 24.
    The alternative wouldbe to find all test scenarios that use the login() and update them individually.
  • 25.
    Makes tests lessbrittle by writing tests with code that allows for exception catching/conditionalizingCONFIDENTIAL Copyright © 2009 Constant Contact, Inc.9
  • 26.
    The Advantages ofPage ObjectsProductivity gains:Quickly identify the location of required methods
  • 27.
    Test development ismore simplistic and can be data driven
  • 28.
    Reusability of multiplePage Objects allow for user flows to be scripted more quickly
  • 29.
    Test code ismore readableCONFIDENTIAL Copyright © 2009 Constant Contact, Inc.10
  • 30.
    The Advantages ofPage ObjectsExample Productivity Increases through Re-Use and Maintainability** LoginViaAPI has 454 References In Test Cases **com.roving.taf.pages.UI.loginViaAPI(PARAM_username, PARAM_password); public static void loginViaAPI(String userName, String password, booleanonFailLogCritical) throws Exception { Rpt.info("UI Login via API Being Attempted - " + userName + " / " + password);com.roving.taf.pages.api.APIs.apiLogin(userName, password);isUserLoggedIn(onFailLogCritical); }CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.11
  • 31.
    Page Objects InUse – How to create a Page ObjectStart by …Run Firebug (Firefox plugin) to inspect elementsCreate a class in a package that has a logical name (lp.login).Declare a variable with the definition obtained from FirebugHave a defined naming conventionAdd methods to interact with newly defined objects.Consider exception handling for all methodsConsider constants for data that does not change frequentlyConsider constants for expected error messagesConsider standardized methods for commonly used actions, such as “goToPage”Consider collaboration with Development for object ID’s (for example all elements need to have an ID)Overloading methods for variability in testingConsider using the Page Title as a variable/constant to be used for page validationAs a rule, do not put assertions in page classes (they belong in the test code)Create test code to reference page object methodsReference the objects through methods For additional information please visit…http://code.google.com/p/selenium/wiki/PageFactoryCONFIDENTIAL Copyright © 2009 Constant Contact, Inc.12
  • 32.
    Appendix slides…CONFIDENTIAL Copyright© 2009 Constant Contact, Inc.13
  • 33.
    Hashmap constant usedin a method /* This Hashtable contains all of the calculations on the billing selections page. */ public static Hashtable<String, Double> calcPrepayProdPrice(String em,Stringsurvey,Stringevm,Stringih,Stringarchive,intprepayTerm,StringdiscountPct,doubletaxRate) throws Exception{Hashtable<String, Double> prodPrice = new Hashtable<String, Double>(); prodPrice.put("em", new Double(emDouble)); prodPrice.put("evm", new Double(evmDouble)); prodPrice.put("survey",new Double(surveyDouble)); prodPrice.put("ih",new Double(ihDouble)); prodPrice.put("archive",new Double(archiveDouble)); prodPrice.put("total", new Double (prodPrice.get("em")+prodPrice.get("evm")+prodPrice.get("survey")+prodPrice.get("ih")+prodPrice.get("archive")));prodPrice.put("prepayDiscount",new Double(calculatePrepayDiscountVerifySel(prodPrice.get("total"),discountPct)));prodPrice.put("subtotal",new Double(calculateSubTotal(prodPrice.get("total"),discountPct)));BigDecimal Subtotal = new BigDecimal(prodPrice.get("subtotal"));BigDecimalPrepayterm = new BigDecimal(prepayTerm);BigDecimal Tax = new BigDecimal(taxRate);BigDecimal value;/* * Tax is calculated as such: * SubTotal / PrePayTerm = x 189 / 6 = 31.5 * x * taxRate = y rounded up 31.5 * .0625 = 1.96875 rounded up to 1.97 * y * PrePayTerm = Total Tax 1.97 * 6 = 11.82 -> Total Tax */prodPrice.put("salesTax",new Double (value.doubleValue()));prodPrice.put("totalPlusTax",new Double(prodPrice.get("subtotal") + prodPrice.get("salesTax")));prodPrice.put("avgMonthlyCost",prodPrice.get("totalPlusTax")/prepayTerm);prodPrice.get("salesTax");return prodPrice; }CONFIDENTIAL Copyright © 2009 Constant Contact, Inc.14

Editor's Notes

  • #5 Talk towards inefficientutil classes due to large size (100’s of methods). Talk towards same/similar methods across multiple methods.
  • #12 Have 3 references for login, API, UI and LPs. Not all tests were required to log in via UI, so we only needed to change one method in order to log in (API). Also, removed the complexity and risk of having to test through ALL applications – just go straight to API’s for test setups.