Articles 6 Best Practices for Selenium

    6 Best Practices for Selenium

    1. Use PageObjects Pattern
    Page Object is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your Application Under Test.

    Six best practices for Selenium

    The tests then use the methods of this page object class whenever they need to interact with that page of the UI. The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change. Subsequently all changes to support that new UI are located in one place.

    The Page Object Design Pattern provides the following advantages:

    • There is a clean separation between test code and page specific code such as locators (or their use if you’re using a UI map) and layout.
    • There is a single repository for the services or operations offered by the page rather than having these services scattered throughout the tests.

    For example you have a LoginPage that you can reuse in your entire integration test.

    If the logon has now a new option or the layout changed a bit… adapt the LoginPage… that’s it !

    More on page object: page-objects-in-selenium-2-, PageObjectFactory, simple example based on Google search page

    2. Be Robust and Portable
    Preferred selector order: id > name > css > xpath

    To locate an element we can use

    • the element’s ID
    • the element’s name attribute
    • an XPath statement
    • by a links text
    • document object model (DOM)

    In practice, you will discover

    • id and name are often the easiest and sure way.
    • xpath are often brittle. For example you have 3 tables displayed but sometimes there are no data and the table isn’t rendered, your xpath locating the second table will not always return the intented one.
    • css are the way to goin conjunction of id and name !
    • Locating links in an internationalized application is sometimes hard… try to use partial href.

    3. Avoid Thread.sleep prefer Wait
    Instead of sleep

    public void clickOnContactType() {
    Use Wait
    (new WebDriverWait(driver, 30)).until(new ExpectedCondition() {
    public Boolean apply(WebDriver d) {
    return d.getTitle().toLowerCase().startsWith(“Java Developer”);

    4. Use jre 1.6
    If while starting your integration tests you encounter:

    java.lang.NoSuchFieldError: HOURS at org.openqa.selenium.remote.internal.HttpClientFactory.(
    java.lang.NoSuchFieldError: java/util/concurrent/TimeUnit.HOURS

    Run your test with a jre 1.6

    5. How to close Firebug startpage

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference(“extensions.firebug.currentVersion”, “1.8.2”);
    driver = new FirefoxDriver(profile);

    Note that you may need to adapt the version.

    6. IE Not Working

    • The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
    • On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings,choose “Internet Options…” from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled “Enable Protected Mode”.

    Note that you may need to adapt the version.

    Possible workaround for Protected Mode
    DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();

    ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY _DOMAILS,true);
    ieCapabilities.setCapability(“ensureCleanSession”, true);
    driver = new InternetExplorerDriver(ieCapabilities);