Introduction
The term “Automation” is everywhere. Automation focuses on automating repetitive tasks and enhance efficiency. This is true for Testing as well. Test automation has been in the forefront, which has resulted in more optimum use of test resources with increased efficiency in test results and saved time, allowing to do more testing.
There are multiple tools available today to perform test automation and Selenium is one of them. Selenium has become the go to tool to automate web application testing. Its versatility makes it popular among test engineers.
In this article, we will see what are the best practices that we can adopt while using selenium as test automation tool.
Selenium Test Automation
The Selenium is an open source test framework 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. User can run the automated testing scripts in a variety of languages (Java, Ruby, PHP, Python, C#, NodeJS, etc.) against local browsers, a lab of browsers and devices using Selenium Grid or a device cloud like what Cross browser testing provides.
Selenium-based tests are ideal for testing different types and versions of web browsers. Selenium is a suite of Selenium tools used for testing. It contains Selenium IDE, Selenium RC, Selenium Web driver, and Selenium Grid. It is used for automating web interactions and regression testing and has recording and playback features. User also can export the scripts that are recorded to other languages including Java, C#, Python, Ruby, JavaScript, and PHP.
Best Practices of Selenium Automation Testing
Selenium is already a robust tool that can be used for web application testing. By following certain best practices, we can make sure to have tests that are more flexible and efficient. Read on to know some of the best practices that we can follow.
-
Use the Right Locators
One of the process which is at the heart of selenium web application testing is interacting with the browser. It should let you navigate through the website, click, type and perform different operations on the website. This can be done using several types of locators,
-
-
- Class
- ID
- Link Text
- Partial Link Text
- Tag Name
- XPath
-
Selecting the right locator to find elements on the website can make or break your tests. If you use a locator that will break on slightest UI change will make your tests brittle. For example, locators like link text can change all the time and may not work every time. Whereas unique identifiers like class and ID are less likely to change and will make the tests robust.
-
Use Page Object Model
Page Object Model (POM) is a popular design pattern used in selenium automation testing. This model improves Selenium test maintenance and reduces code duplication. Page Object Model pattern can be used in any framework. Selenium testing uses the methods from the POM whenever an interaction is required with the User Interface (UI). So, if the UI changes, the tests do not need to change.
The page object model has two core advantages:
-
-
- There is clean separation between test code and page specific code such as locators and layout.
- There is single repository for the services or operations offered by the page rather than having these services scattered throughout the tests.
-
-
Use Selenium Wait Commands Instead of Thread.sleep()
It is quite often that the web application takes some time to load due to the network speed, server issues or various other reasons. To tackle this, user need to pause the scripts to wait for all the elements to load. This is a good way to ensure that all web elements are there while you run your tests on them. The clear understanding of implicit and explicit wait will help a lot.
Wait
-
-
- Explicit – Wait till a certain condition occurs without continuing in the code.
- Implicit – WebDriver is instructed to poll the DOM till the search for an element is completed. The time is set to 0 by default.
-
Sleep
-
-
- sleep() waiting happens for the seconds specified within the brackets irrespective of the readiness of the working page.
-
‘Wait’ is better than ‘sleep’, because sleep waits until the defined time even after the task is completed. But wait knows how long to wait and is smart enough to start when the action is completed. Sleep will slow down the test, while wait does not affect the test time.
-
Use Data-Driven Testing for Parameterization
A web application should be tested against different combinations of browsers, devices, and OS combinations. Hard coding of test values in test automation scripts is not a scalable solution as it would lead to unnecessary bloatware and possible repetition of test code.
A better solution is using parameterization for achieving data-driven automation testing with Selenium. Parameterization helps in executing test cases against different input combinations. More extensive the data set, the better is the test coverage. This, in turn, helps in improving product quality and implementing good Selenium test practices.
-
Use Assert and Verify in Appropriate Scenarios
There are several cases in automation testing with Selenium, where the user would want to halt the test execution on encountering a hard error. For example, you are using Selenium to automate testing of the website login page, but the web locator being used for locating the sign-in box is not correct. In this case, assert should be issued as the remaining tests will falter out as they are dependent on the sign-in page.
Asserts should only be used when you want to halt the test execution in case of a hard failure. If the assert condition is false, execution stops, and further tests will not be executed. On the other hand, verify should be used where the criticality of the error is low, and you still want to proceed with test execution irrespective of the status of Verify condition.
-
Don’t Depend on a Specific Driver
Never depend on one driver implementation. Understand that the drivers are not instantaneous in different browsers. That is, there won’t necessarily be an IE driver, Firefox driver, etc. The thing to keep in mind about Selenium test automation is the fact that the drivers are not interchangeable, so there can be issues even if you use the right driver for the browser, which is something you really want to cover in a meaningful manner.
What you can do is to go with all kinds of Parameter notes to manage a variety of browser types. This will make it easier to prepare for execution simultaneously and that’s something you want to consider.
-
Avoid Code Duplication (or Wrap Selenium Calls)
One of the most common Selenium best practices for Selenium test automation is avoiding unnecessary duplication of code. You might be using different web locators such as XPath, ID, etc. for accessing the web elements present on the web page. The code that is frequently used in the implementation should be created as a separate API, so that code duplication is minimal.
Avoiding duplication also helps in the reduction of code size and improves the maintainability of the test code. Wrapping Selenium calls is one of the Selenium best practices that can significantly impact maintaining a complex test suite or test code.
-
Leverage Parallel Testing in Selenium
A primary factor for Selenium’s popularity is its support for parallel testing. Almost all popular test frameworks such as PyTest, PyUnit, TestNG, Cucumber, etc. provide features for executing tests in parallel on a Selenium Grid. Parallel testing in Selenium lets the user to execute the same tests simultaneously on different environments such as a combination of browsers, platforms, and device emulators. Using Selenium, it is recommended to enable parallel testing in the implementation as it reduces the test execution time by a significant margin.
At Brainbox, we have developed a Test Automation Framework for end to end application testing of web and mobile android platforms. We have followed all the above mentioned best practices for Selenium automation testing to make the framework robust, flexible and reliable.
To know more about the Brainbox framework, you can refer here https://brainbox.consulting/blogs-news/software-testing-blog/brainbox-test-automation-framework/.
Summary
Selenium allows you to create a stable, true, and reliable automation process if you use it to its full capability. There’s a lot that can be done to improve test automation efforts and these practices would certainly help to improve. Finding bugs early in the development, that can-do wonders while performing test automation. Also, it’s always to play smart and think over the test design and strategy before you start writing Selenium test automation scripts.
Contact us at sales@brainbox.consulting for more information and we would be happy to assist you with testing related services.