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 CrossBrowserTesting’s. To get the most out of automation, we’ve put together five Selenium best practices for 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:
- Link Text
- PartPartial Link Textial
- Tag Name
- X Path
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, you can read more about Xpath to CSS conversion here.
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 by 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.
Page Object Model. Use it.
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:
- 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.
- 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 SeleniumEasy’s explanation and example:
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 blocker for a truly agile or CI workflow. Keep tests as small as possible.
Use Your Resources
Selenium best practices 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 best practices, here are a few of our favorites: