The best practices test automation with Selenium WebDriver.

Anton Smirnov
ITNEXT
Published in
6 min readOct 24, 2021

--

Photo by Domenico Loia

Selenium WebDriver has been very popular since the time of its inception.

Selenium is an open-source framework used for the automation of web applications. Apart from this, it can also work on various administrative tasks such as monitoring websites.

Selenium automated testing is the most popular open-source automated testing tool in the technology industry today. Bringing immense benefits, selenium testing offers robust, browser-based automation for end-to-end testing.

Selenium Web Driver − Web Driver works on the browser directly and uses the browser’s in-built features to trigger the automation test written by the tester.

So first let’s take a look at how Selenium WebDriver API interacts with real browsers using browser drivers and understand major blocks that comprise the Selenium WebDriver architecture. Well, Selenium WebDriver is comprised of four major blocks:

· Selenium Client Libraries

· JSON Wire Protocol

· Browser Drivers

· Browsers.

After years of working with the Selenium framework, I have realized that the ‘One size fits all approach does not apply to Selenium test automation.

This article discusses the top Selenium best practices for Selenium test automation, which might help you develop well-designed and scalable test suites.

  1. Set Naming Conventions. Establishing standard naming conventions for every type of file created helps streamline development and Selenium testing processes. For example: When working in a team, there are cases where your team members may be required to enhance the tests that you had written. If you revisit the same test after a couple of months, you might not be able to figure out the purpose of a test, until you go through the complete implementation. If some tests have failed during the execution stage, it should be easy to figure out which functionalities are broken by just taking a quick look at the test name. These problems can be easily fixed by giving naming test cases in a manner that they are self-explanatory so that neither you nor your teammates spend time unnecessarily scrolling through the implementation.
  2. Avoid Blocking Sleep Calls. In particular, this pattern of sleep is an example of explicit waits. So this isn’t actually a feature of Selenium WebDriver, it’s a common feature in most programming languages though.
    Thread.Sleep() does exactly what you think it does, it sleeps the thread. So when your program runs, in the majority of your cases that program will be some automated checks, they are running on a thread.
    So when we call Thread. Sleep we are instructing our program to do absolutely nothing for a period of time, just sleep.
    It doesn’t matter what our application under test is up to, we don’t care, our checks are having a nap time. Selenium provides Implicit wait and Explicit wait that handle delays more efficiently than sleep. Implicit wait informs the browser to wait for a specified time duration for all the web elements present on the page. If the element is available faster than the implicit delay time, the execution moves to the next line of code execution. For more information please read “How to use implicit and explicit waits in Selenium”.
  3. Choose the Best-Suited Web Locator. One of the challenges with Selenium test automation is that automation tests have to be modified if there are changes in the implementation related to locators used in the test code. ID, Name, Link Text, XPath, CSS Selector, DOM Locator, etc.

With so many web locators, it is necessary to choose the right one to minimize the impact on tests caused due to changes in the user interface. ID, Class, and Name are not only the easiest to use but also less brittle than other web locators. For more information please read “The concept of the right locator for Selenium WebDriver automated scripts.”

4. Implement Logging and Reporting. If a particular test in an extensive test suite fails, locating the failing test case can become challenging. Logging can be a huge savior in such cases as console logs at appropriate places in the test code help develop a better understanding of the code and help in zeroing on the problem.

Some of the popular log levels (available in popular programming languages) are debug, info, warning, error, and critical. Adding unnecessary logs in the test implementation can cause delays in the test execution. Hence, it is recommended to add logs with level error (and/or critical) in scenarios that aid in tracking the cause of failure. For more information please read “Implement Logging and Reporting Best Practices.

5. Use Design Patterns and Principles. There are a lot of problems in design and development automation, and faced with these problems, people formulated patterns. Initially, the classic patterns were formulated a long time ago by the four, who released the book Design Patterns. This four is also called the gang of four.

The book formulates all the patterns they encountered at that time in an object-oriented world. There is a problem — there is a solution, and for a long time, this concept of design patterns has been growing and developing, replenishing with new patterns. Factory and Decorator.

Also, you may read “ All about design patterns in automation testing”.

6. Centralized Folder Structures. In a central repository for the Selenium team, organize and define folder structures with established naming conventions to set up a solid foundation for Dev and the Selenium testers. Time wasted on erroneous filenames and code issues has a far greater impact negatively. For example: When working on tests that use the Selenium framework, it is essential to focus on the test code’s maintainability. A standard project can consist of Src and Test folders. The Src folder can contain subdirectories containing Page Objects, Helper functions, and file(s) containing web locator information used in test scenarios. The Test folder can include the actual test implementation.

7. Data-Driven Testing. With Selenium testing, you can use a Data-Driven Framework, a Keyword Driven Framework, or a Hybrid of both. Data-Driven frameworks are more effective in how the dataset is separated from actual test code. Input test data is taken from external sources (Excel, CSV, etc.) A website (or web application) should be tested against different combinations of browsers, devices, and OS combinations (i.e., multiple datasets). 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 (or data sets).

For example:

  • Parameterization using TestNG.
  • JUnit Parameterized.
  • Data-driven testing.

For more information please read “How to use the Data Provider pattern in the project”.

8. Use Assert and Verify-in Appropriate Scenarios.

9. Avoid Code Duplication (or Wrap Selenium Calls).

10. Leverage Parallel Testing in Selenium. This write examples for JUnit and TestNG.

Conclusion. In this article, we looked at some of the Selenium best practices. When coming up with Selenium test scenarios, you should always remember that Selenium is ideal for automation testing, so do not use the same for other types of testing since it might read favorable results.

https://test-engineer.site/

Author Anton Smirnov

--

--

I’m a software engineer who specializes in testing and automation. My top languages are Java and Swift. My blog is https://test-engineer.tech/