Skip to main content

Selenium Test Automation: 6 Best Practices

By 2014-01-28April 8th, 2020No Comments

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.

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.

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)

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”);


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


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.


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);


Source :

Leave a Reply

Close Menu