Selenium Best Practices

Applies to CrossBrowserTesting SaaS, last modified on January 10, 2023

The Selenium automation framework is a great tool-set for automating interactions with the browser. Because of its powerful open source features and standard adoption across many browsers, Selenium has become the most popular web testing framework in the world. You can run your automated testing scripts in a variety of languages against local browsers, a lab of browsers and devices using Selenium Grid or a device cloud like what CrossBrowserTesting provides. To get the most out of Selenium automation, we've put together five best practices for automated browser testing.

Use the right locators

At the heart of the Selenium framework is interaction with the browser, letting you navigate, click, type and check different objects within the DOM using a few set actions. It does this using several different types of locators:

  • Class
  • ID
  • Link Text
  • PartPartial Link Textial
  • Tag Name
  • XPath

Selecting the right locators can be the difference between a test script that is flexible and successful, and a brittle test that breaks on the slightest UI change. If they are present, use unique Classes or IDs as Selenium locators, as they are less likely to change without someone on your QA or Dev team knowing. But locators like link text can change all the time - think about a dynamic button based on logged in state, or your marketing team running A/B tests. You can also use XPath to navigate the WebDriver.

Communication between QA, developers, marketers, and designers is key

There are two aspects of collaborating with different teams that can make your Selenium testing a success: constant communication and instilling the importance of testing to every department. Instilling the importance of the QA process on your development team can save time and efficiency when building a testing framework. Make sure they know how important unique CSS selectors are to your testing efforts, and ask them to write semantic and human-friendly code. Having constant communication across teams can help QA plan for changes to the UI of a website or application mandated from Marketing or the Design team. Having small conversations about the front-end aspect of your app decreases the chance of surprise UI changes that will go untested.

Use the page object model

A popular test automation design pattern, the page object model will help you make robust testing frameworks that are resistant to small tweaks in the UI. The page object model has two core advantages:

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

In both cases this allows any modifications required due to UI changes to all be made in one place. See Selenium Easy's explanation and example:

public class SignInPage {

private WebDriver driver;

private By headerPageText = By.cssSelector(".hidden-small");
private By createAccountLink ="link-signup");
private By emailTextBox ="Email");
private By passwordTextBox ="Passwd");
private By loginBtn ="signIn");
private By errorMsgTXT +"errormsg_0_Passwd");

Make tests independent of each other

Testing one single action or process, independent of any other tests is known as atomic testing. This testing strategy will keep you from making chained, brittle tests. Chaining together tests, while saving time upfront, can be a huge hindrance to an agile or CI workflow. Keep tests as small as possible.

Use your resources

Selenium can be tough to learn because it requires a certain amount of tangential knowledge. You have to have some understanding of programming and to truly be a skilled Selenium tester, an understanding of test automation best practices. Luckily, there are some fantastic resources available on the Internet to learn from. While we run monthly webinars to get you up and running with Selenium, here are a few of our favorites:

See Also

Selenium and JavaScript

Highlight search results