Top 27 Best Practices for Selenium Test Automation | LambdaTest

Image for post
Image for post

In recent years, you’d hardly see an organization who had not transitioned to automation testing. After All, with quick feedback on new features, who’d want to miss out on Selenium testing. Even then, a few testers complain of the automation tests being unstable and unreliable. Trust me, that’s not true! A lot of the time the reason for your unstable tests is not following the right practices for Selenium test automation.

Although there are no hard and fast rules for writing automation scripts, there are certain tips you can follow to write better test scripts.

Speaking of which, in this article, I’ll share 27 top tips for Selenium test automation, which will help you get the most value out of automation testing efforts. So, let’s dive right in.

1. Test Early and Test Often

This gave rise to the concept of shift left testing, this means to involve testers right from the requirement gathering phase. That way, they are able to come up with test cases in advance by understanding the expectations of the end-users. The primary idea here is to avoid encountering bugs after the development is done. Testers can provide their valuable input and help developers develop the product in a way that improves the user experience. Also, to help developers avoid any development practice which isn’t recommended for the product. For example, imagine your developer leveraging CSS Subgrid in a pricing page table of a subscription-based model. The cross browser compatibility of CSS Subgrid:

Image for post
Image for post

If your testing team is already aware of your developer using a CSS Subgrid, then they can go ahead and prompt the developer in advance before the development is complete. That way, you are saving time for both developers and testers. Not to mention, you are also saving yourself from a potential outage.

This is why you should start testing as soon as possible, and do it as frequently as needed. The earlier you start testing, the sooner you find bugs and bugs found earlier can help you save both time & resources in your SDLC(Software Development Life Cycle).

2. Consider Using a BDD Framework

By writing your Selenium test automation scripts with BDD, you create specifications, which will help your team to understand the test and requirements in a much better way. This allows you to save time, as you won’t have to explain the test to any stakeholder as they are self-explanatory now.

As the business team has more insights into the tests, they might be able to give some input on the basis of business benefits.

3. Use Selenium Wait Commands Instead Of Thread.sleep()

One way to pause your script is by using the Thread.sleep() function, it pauses your test script for the specified time. But there is a major flaw with this function, let’s say you want to open a web page, and you’ve chosen the time to wait to be 6s, in cases where your website loads much faster, you would’ve wasted a lot of time.

In the faster scenarios, you might have lost a lot of extra time running Selenium automation tests, and in the slower ones, you’d get the result as a failure. So in order to avoid such situations, you need to incorporate implicit waits or explicit waits.

To know more about Implicit wait commands or Explicit wait commands you can visit our previous article on Selenium Waits.

4. Set Up Automation Test Reports

Selenium report generation can do wonders for you, as you save precious time as you manage your test data in an organized manner. Due to this, you have better control over tests, as using this data you can analyze when the test scripts are failing.

You can keep a tab on your test scripts with the LambdaTest Platform. With LambdaTest, you can run your Selenium testing scripts over a cloud-based Selenium Grid of 2000+ desktop and mobile browsers.

Image for post
Image for post

5. Use Page Object Model

With POM you only need to change the page objects, as all the locators are in one central repository. In POM, every webpage has a separate page class, to find web elements on it as well as the page methods for these web elements. This makes the project more reliable as, the developer doesn’t need to scan the whole code and test scripts, just to incorporate a few UI changes.

6. Take Automated Screenshots For Failure Investigation

If you’re running your Selenium test automation using our online Selenium Grid then you won’t have to worry about it. All you need to do is pass a simple capability through your Desired Capabilities class i.e. set the capability of the visual flag to true.

You can even refer to our Desired Capabilities Generator to turn on the button for generating step by step screenshots of your Selenium test script execution.

Image for post
Image for post

The Desired Capabilities Generator will provide you with the class in various programming languages as you turn on the toggle for Screenshot under the Advanced configurations.

7. Designing Tests Prior To Automating Them

Automating tests without adequate design, indicates that you are only interested in ensuring that the script works. It’s important to think about different possible scenarios that can help you perform foolproof testing. After all, reducing the testing scope just to make sure that the test passes is not a wise move! Selenium test automation enables testers to create test scripts using multiple programming languages.

8. Not Completely Relying On An Automation Tool

However, some managers live under a big misconception that if they choose the right tool, automating anything is possible. Well, no tool offers you everything. Even though they simplify the entire process, you can’t carry out testing without skilled resources.

Sometimes, when you try to use automation tools without any human intervention, they can get stuck while recognizing complex objects on software or application. But if you have skilled resources, they can make the tool move past complications and use them to the benefit of the company.

9. Identifying Which Tests To Automate

For instance, it’s a good idea to automate tests that involve visual scenarios including fonts, colors, and layout structure. You can also automate testing of static content such as logo, title, policy, and terms. Of course, we know that they rarely change and thus, you can easily automate them.

10. Automating Most Frequent and Predictable Test Cases

For instance, when you click on a tab, an app starts to launch. This is a generic test with a condition. In other words, when we know that an event is sure to occur, the result is predictable. So, to avoid the repetition of executions, you can automate these test cases.

11. Choosing The Correct Test Automation Too

For instance, check whether the tool supports the platform your firm works on. Some other questions to ask include the following. Do you need the tool to perform mobile application testing? What are the operating systems you work with? Does the tool support record and playback testing? Can you manually create automated tests?

12. Removing Uncertainty From Selenium Test Automation

When there are any inconsistencies or false positives, it increases the amount of time we take to analyze results. Therefore, when it comes to regression packs, never hesitate to eliminate unstable tests. After all, you have got to keep reliable and consistent results as goals!

13. Hiring a Dedicated Team of Experts

But if you are hesitant to hire new staff for automating and leave the task to your experienced manual testers, first free them of manual testing and related responsibilities. They shouldn’t be automating with half of their focus on something else.

Or else, hire an automation engineer to do the job. Now, suppose your firm operates on a large scale. In that case, I suggest you appoint an automation architect. Then, hire automation engineers whom the architect can supervise. The number of engineers you hire can be according to your company’s production requirement.

14. Ensure Collective Test Automation Ownership

Keep the involvement of all the team members equal for integrating automation in your workplace. This way, the automation testers also get a chance to impart their knowledge to the rest of the team. Hence, when everyone is aware of what’s going on, they can all contribute to the success of the company.

15. Dividing Efforts During Automation Testing

Even the non-technical staff can use keyword tests to create powerful and robust automated tests. Thus, when everyone in the team contributes according to their skills, finding defects becomes a lot easier.

16. Reviewing The Validity Factor For Automated Tests

Therefore, it’s vital that you review validity for all automated tests. In other words, all the time, you need to ensure that the tests are up to date. It’s best to keep someone in charge of the validity and sanity of automated tests.

17. Selecting Automation Tools That Hold Familiarity With Company Resources

18. Using Test Techniques

For instance, four test techniques, in particular, have a lot of benefits to offer if testers use them in test automation. The are Boundary Value Analysis, State Transition Testing, Equivalence Partitioning, Pairwise Testing.

19. Good Automation Equals Great Manual Test Case

Also, keep the objective of each test case clear. Make sure that it doesn’t depend too much on other test cases. I suggest automation engineers run test cases manually at least one time. This will help them decide the navigation flow and which objects need identification. Another upside to this practice is that sometimes, you can even identify bugs before writing the automation script. Don’t you think it’s better to catch bugs in the development phase instead of in execution?

20. Not Automating Unstable Functionalities

We can say that keeping up with so many unexpected changes is quite exhausting. Moreover, if you can no longer apply a functionality, all that effort goes in vain! Therefore, a wise move is to automate a function only when it’s not subject to many changes and is stable.

21. Creating High-Quality Test Data

It’s a good idea to invest significant time and effort into creating high-quality test data. This makes the writing of automated tests much simpler. This practice also helps in extending the existing automated tests and pacing up the application development process.

22. Creating Test Resistant To Changes In The User Interface

If you run automation tests in such cases, the test will fail as test scripts won’t be able to find the object. To ensure the success of a test, you can add new names to replace the old ones. If names for the controls are unique, the changes in the user interface won’t impact test results.

23. Identifying Automation Opportunities

The login function has different possible scenarios. For example, invalid password, invalid username, blank username, invalid email, etc. List them out with the outcomes expected in an excel file. Use this as a source of data for your test case. So, when you automate this manual test, you can check different scenarios in a single go.

24. Avoiding GUI Automation If There Is Another Option

You should also know that it’s okay if you have to stop automating the GUI. Sometimes, when GUI is concerned, automation just stops making sense at some point. Too much GUI automation when unnecessary can make a really bad mess almost impossible to untangle.

25. Understanding The Context

26. Not Considering Automation As a Replacement For Manual Testing

But the newly added functionalities can interfere with the working of existing ones. So, blindly automating end-to-end testing won’t do you any good. Test automation can’t capture unforeseen circumstances. In this case, you might want to perform exploratory testing. Here, the testers have to use their own creativity. To sum up, automation is to prevent manual testers from doing repetitive work and focus on finding bugs and more test scenarios.

27. Targeting Quick Feedback

Continuous feedback from stakeholders, functional Experts, Testers, and QA Analysts, ensures an error-free and speedy release. Feedback includes necessary information as well as actions to keep fixing issues as they come.

Summing It Up

Image for post
Image for post

That’s all for the best practices for Selenium Test Automation. There’s a lot that can be done to improve test automation efforts and I hope these practices would certainly help you 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.

Liked the tips, or you have another to share, feel free to share them with us in the comment section down below. Meanwhile, share the article with your fellow automation testers! Happy testing.

Originally published at LambdaTest

Author Vethee Dixit

Image for post
Image for post

Written by

Product Growth at @lambdatesting (

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store