Best Practices For Using Selenium for Test Automation

DZone 's Guide to

Best Practices For Using Selenium for Test Automation

Make sure that you employ practices that will bring the best out of Selenium for your test automation needs.

· DevOps Zone ·
Free Resource

Software developers have the benefit of using automation tools to execute test case suites, unlike before, when manual testers used to slog through test script execution.

However, the purpose of automation testing is not to get away from manual testing completely, but to minimize the test case counts that are run manually. Automation testing allows you to test multilingual sites quickly and also increases the test execution speed.

The process of automation testing is simple and all you need to do is to:

Image title

What Is Selenium Automation?

Open sources tools and applications gained importance after the year 2000 because of their cost-effectiveness, efficiency, repeatability, accuracy, and ease of use. Selenium is one of the open source tools which provides all the benefits of an open source tool in terms of application testing.

Selenium is a suite of Selenium tools used for testing. It contains Selenium IDE, Selenium RC, Selenium Webdriver, and Selenium Grid. It is used for automating web interactions and  regression testing, and has recording and playback features. Further, you can export the scripts that are recorded to other languages including Java, C#, Python, Ruby, Javascript, and PHP.

Selenium in Cross-Browser Testing?

As the name implies, cross-browser testing is a method used to test web applications on different web browsers and devices to make sure it works on every device and browser seamlessly.

To understand why cross-browser testing is needed, ask yourself how many times you open Facebook and Twitter in the same browser. You might open them using IE at home and Firefox in the office. Therefore, cross-browser testing has become mandatory with web application testing.

 Selenium helps to automate test cases in Safari, Google Chrome, Mozilla Firefox, and IE. Executing a test case from the same machine on different browsers can happen with Selenium as well, simultaneously. It also supports multiple languages and operating systems.

Let’s take a look at the best practices of Selenium to make the best use of it in automating your cross-browser testing process.

11 Best Practices of Selenium Automation Testing

1. Make Use of Right Locators

The bottom of the Selenium framework is to interact with your browser and thereby allows you to check, type, and navigate several objects with the Document Object Model (DOM). This happens with a set of actions and uses several locators including CSS Selector, Name, Xpath, ID, Tag Name, Link Text, and Class.

For example, use Class and ID locators when you do not want to change the code without the knowledge of the developers and testers. On the other hand, use Link Text for a dynamic situation when a marketing team runs a test. Finally, XPath can be used in navigating the webdriver.

2. Data-Driven Testing

If you want to make use of the same test and the same code for different inputs, then you can rely on Selenium. It will allow both developers and QA team to make modifications, which means you can use it for system functional testing as well as browser compatibility testing.

Selenium also allows customers to reap the benefits of its framework. The client can leverage proprietary test accelerator and kick start test automation. This will reduce the automation cycle time. There are more than 90 function libraries letting the client start the automation process.

3. Don't Depend on a Specific Driver 

Never depend on one particular driver implementation. Understand that the drivers are not instantaneous in different browsers. That is, there won’t necessarily be a IE driver, FireFox driver, etc.

For example, at the time of executing an integration test during the continuous Linux build, RemoteDriver will be received. You can quickly create small frameworks in Selenium making use of LabelledParameterized (JUnit has @RunWith and TestNG it is  @Parameters.)

And ScreenShotWatchMan (JUnit @Rule, TestNG TestListenerAdapter <listener>). In other words, use Parameter notes to handle multiple browser types and get ready for simultaneous execution.

4. & 5. Choose the Selector Order

It is important to select the selector order because selectors like XPath and CSS are based on the location. They are slow when compared to ID, Name, and Link Text. Name and ID are especially straightforward and direct way selectors. CSS usually is a combination of ID and Name. XPath, by contrast, should be the last solution.

Here's what a robust solution would look like: XPath < CSS < Links Text < Name < ID. This means start with ID and make XPath the last selector. Among the 3 tables without data, XPath will be slowest to identify the second table and also may not return the correct one. Hence XPath is selected at last and they are brittle. CSS is always in conjunction with Name and ID.

6. Using PageObjects

PageObject has gained popularity as the best design pattern in test automation. It enhances test maintenance and also reduces duplication in codes. Further, it is an object-oriented class (OOC) that acts as an interface to the page of the application which is under test. To simplify, PageObject is an object-oriented design pattern and web pages are defined as classes. The different elements on the page become the variables. User interaction is implemented as methods.

  • Web pages = Class

  • Various Elements on the page = Variables

  • User interaction = Methods

Advantages of PageObject

  1. It helps in making a robust framework by offering resistance to minor UI tweaks. Test code and page codes are separated. (Locators and Layout).

  2. Services are not scattered through the test but there is one repository for all the services provided by the page.

  3. They are reliable and easy to maintain.

  4. The script is readable. The code is reusable.

  5. Eliminates duplicity altogether.

7. Prefer Wait and Avoid Thread.Sleep

Make use of wait in the place of sleep. Understand both explicit and implicit wait. Also, Thread.sleep()   logic. Then you will know why to choose to wait instead of sleep.


  • 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.


 Thread.sleep()  waiting happens for the seconds specified within the brackets irrespective of the readiness of the working page.

Now you can see why wait is better than sleep. It is 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.

8. Use Java Runtime Environment JRE 1.6

When beginning with the integration test, you might face the following error


The Selenium server is programmed with Java. Therefore when you encounter a runtime error, then remember it’s time to use the latest version. Now, the version is Java Runtime Environment (JRE) 1.6 which is required to run Selenium server.

Download JRE 1.6 from the Selenium Website

With the Java command present in the PATH, then use command java -jar selenium-server-standalone-2.x.x.jar   to start Selenium server and replace 2.x.x with the actual version.

9. Closing the Firebug Startpage

At the time of launching the firefox driver, you might have included firebug. This may work fine at times. But, if opening a new firebug tab simultaneously while launching the browser annoys you, then follow one of these tips given below close the firebug startpage.

* Set False in the “ showFirstRunPage ” flag as shown below.

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("extensions.firebug.showFirstRunPage", false);

Set a big number in the “current version” as given below.

10. Prepare for the Browsers

When the browser is not working, then you need to follow some adjustments to make it work.

  • Set the browser zoom level to cent percent. This will allow the native mouse event to make the coordinates get aligned.

  • Protected Mode Settings must be fixed the same for each zone. Irrespective it being on or off, it has to be the same for each zone. Do this by choosing Tool Menu → Internet Options → Security Tab → Tick Enable Protected Mode for each zone.

11. Don't Be Afraid to Hack Selenium When Time Demands

Smart work is appreciated, so don't be afraid to hack Selenium Webdriver to make an effective automation scripting.

There are two lines in particular that you can use as shortcuts.

Hack #1: TestNG report – Screenshot Linking

Use the following code for screenshot linking to a TestNG report. This will become a hyperlink allowing you to open the captured screenshot.

Reporter.log("<a href="+"E:\\screenshot\\"+screenshot_timestamp+".png"+">)click to open

Hack #2: Check the Existence of A Web Element

The code given below will let you check the code size of the web object and identify the presence of a particular web element.


Final Takeaway

It's up to you to stabilize the web test automation. Selenium allows you to create a stable, true, and reliable UI automation process if you use it to its full capability. Don’t forget these best practices for Selenium before moving further.

automation testing tool, cross browser testing, devops, selenium automation testing, selenium practical tips and tricks

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}