logo logo

Selenium Automation Best Practices & Tips You Must Know

Selenium Automation

In this article, we will look at the best practices for Selenium automation from End-to-End. Selenium is by far one of the leading endeavors for testing a web-based application. However, to automate from End-to-End, we need more than Selenium. As a result, I will explain the best practices along with some tips to get outstanding results from Selenium Automation 💥

Before starting automation, we must identify first cases that generate benefits. A couple of the cases are designing an automation test strategy & analyzing which test to automate. By the end of this article, you will see why Programming, incorporating an Automation Build Tool, having a Test Framework, utilizing a Design Pattern, creating an Automation Framework, and Continuous Integration/Deployment are tips for Selenium Automation.

Tutorial Chapters

Best Practices

Best Practices are guidelines for producing superior results. When it comes to Selenium automation, it’s important to first design an automation test strategy. A test strategy includes some of the following fundamentals:

  • Setting goals
  • Developing standards
  • Determining what to accomplish
  • Preparing for a high Return On Investment (ROI)

Prior to automating a test script, we need to decide which test to automate. It’s virtually impossible to automate every test case. A 100% test coverage is challenging due to so many test scenarios. Therefore, a risk-based plan will help limit automation to the most important business cases ✍

Establishing an automation test strategy and determining what to automate are processes before automation. However, the following are 5 best practices in the course of automation using Selenium:

  1. Create Concise Descriptive Names
  2. Test One Scenario At A Time
  3. Avoid Using Thread.sleep()
  4. Take Screenshots For Every Failure
  5. Setup Detailed Logging & Reporting

Create Concise Descriptive Names

A concise descriptive name provides a clear meaning about the test script. It helps the entire team know precisely what functionality is being tested in a few words. The objection is how can an engineer create a name that’s short and meaningful. If there’s a choice between concise and descriptive then descriptive wins 💡 Most engineers would agree that code readability is important. The following are examples of concise descriptive names:

// Not Descriptive
public void testErrMess () {

public void addCustomer () {
// Concise & Descriptive
public void testErrorAfterInvalidUserName () {

public void addNewCustomer () {

The 2 not descriptive names don’t provide details regarding what’s being tested. For example, testErrMess is clear about testing an error message but not clear if the Username or Password is invalid. It can be a scenario where the Username and Password are both invalid. Similarly, with the addCustomer method. Is it the method adding a new customer, existing customer, etc.? The examples show methods but classes and variables should also have concise descriptive names.

Test One Scenario At A Time

Testing one scenario at a time is a best practice that spins off of programming. In programming, a method should have 1 logical task. It’s not mandatory but beneficial for several reasons. One of the reasons involves debugging our program. Debugging consists of finding and fixing a problem in our code. One test scenario in our code removes uncertainty if our test script fails during execution. It’s easier to know exactly what failed by having one rather than having a group of tests and being unsure which test failed.

Avoid Using Thread.sleep()

The loading speed of an Application Under Test (AUT) depends on factors like Machine Performance, Server Performance, Server Location, and Network. As a result, we are unable to predict the time to load an application or element within an application. This leads to possible wait complications which are outside components that cause problems with our test script.

We need to avoid using Thread.sleep() because it’s not a dynamic wait statement. It pauses our test script but waits for a specific amount of time. For example, if we hard-code Thread.sleep() for 5 seconds but an element loads in 2 seconds then our test script will wait for the full 5 seconds. This practice should be avoided because it’s unreliable. It’s not reliable because on a different machine the same element may take 6 seconds to load. For Selenium, the best practice is to use a Wait Method such as an Explicit Wait statement. Explicit Wait is dynamic and pauses execution until time expires or an expected condition is met.

Take Screenshots For Every Failure

At some point, our automation Test Script will face a failure. When it happens, we are not sure if it’s a bug in the AUT or a problem with our code. Capturing a screenshot on a failure is a best practice because it saves time when investigating the reason for a test failure. There are some projects that take screenshots of all steps whether they pass or fail. The following code snippet uses getScreenshotAs method to take a screenshot.

public void captureScreenshotOnFailure () throws IOException 	{
  WebDriver driver = new ChromeDriver ();
  driver.findElement(By.id("name")).sendKeys("Selenium Automation");

  File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(source, new File("TestProject Screenshot.png"));

TestProject’s Example page

Note: TestProject’s Example page has 12345 as a password hint but the Test Script entered 3434. Therefore, the executed test script passed due to an expected invalid password message.

Setup Detailed Logging & Reporting

Setting up detailed logs and reports enhance our test. Logs generate information that occurs within the AUT. Reports help measure and keep track of the Test Results. A popular logging API is Log4j. It has log levels that can be defined in our code. In order, the log levels are:

  1. All – logs everything by turning all logs on
  2. Debug – prints helpful debugging information events
  3. Info – prints informational messages that highlight an application’s progress
  4. Warn – prints potentially harmful information concerning faulty behavior
  5. Error – prints error events that might allow the application to keep running
  6. Fatal – prints critical information that causes the application to crash
  7. Off – turns off all logs

Extent and Allure are reports that can be integrated with Selenium. They provide a big picture representation of the test and details (pass or fail) regarding each test. The following is a screenshot of the extent report.

Extent Report -Selenium Automation

In this comparison article, you can read more about 8 leading test reporting tools for Selenium, amongst them is TestProject that automatically creates out of your Selenium tests detailed HTML/PDF test reports and dashboards in a cloud-based environment. You can read more about it here:

Tips/Components For Selenium

In addition to the best practices, the following are components/tips to optimize Selenium Automation. They will help our project operate in a first-rate manner:


What is Programming?
Programming is the process of creating instructions that direct a computer on how to execute a task. In alphabetical order, Selenium has language bindings for C#, Java, JavaScript, Python, and Ruby.

How is Programming a Best Practice Tip For Selenium?

It’s a best practice tip 💡 Programming Is The Foundation For Automation. That’s why some companies attempt to let developers write automation test scripts. An engineer must understand the programming principles in order to create clean Selenium test scripts. The WebDriver language bindings for C#, Java, JavaScript, Python, and Ruby are designed to drive actions on a browser. Each language has its own bindings. Bindings mean the same commands written for one language is also written for the other languages. For example, Java has a command to load a browser and C#, JavaScript, Python, and Ruby also have a command to load a browser.

Note: If you are using a powerful test automation tool like TestProject to record a test for web and mobile, then programming is not the foundation for automation. No coding skills are required. We can record our test then convert the test to a coded language (though if you wanted to, you could start straight from using TestProject’s open-sourced OpenSDK for Java, Python or C#, using pure Selenium API commands).

A question someone may ask is “How much programming should I know to get the most out of Selenium?”. An understanding of the core programming principles is enough to be effective with Selenium automation 🙌

Automation Build Tool

What is an Automation Build Tool?
An automation build tool compiles source code and machine code. It automates the whole process of converting files and a variety of assets. Some of the advantages are:

  • Improving product quality
  • Eliminating redundant tasks
  • Containing a history of builds and releases

How is an Automation Build Tool a Best Practice Tip For Selenium?
An Automation Build Tool like Maven or Gradle is a best practice tip because it prevents us from downloading Selenium for every update. Downloading Selenium is not a bad idea but a build tool is faster and easier to update via dependency management system. We can replace the Selenium dependency for Maven in a pom.xml file or Gradle through a build.gradle file. In addition to updating Selenium, Maven and Gradle are designed to manage our projects. It comes with a pre-defined folder structure to add our Selenium test scripts. Here’s a screenshot of Maven’s pre-defined folder structure:

Maven’s pre-defined folder structure

Test Framework

What is a Test Framework?
A Test Framework provides a collection of guidelines to test our automation Test Scripts. We can perform all sorts of testing such as regression testing, integration testing, data-driven testing, etc. The purpose is to simplify the process of testing an AUT.

How is a Test Framework a Best Practice Tip For Selenium?
A Test Framework such as TestNG is a best practice tip because it comes with a set of assertion statements. There are multiple benefits but an assertion statement verifies whether our test Pass or Fail. We have the option of implementing a Hard Assert, Soft Assert, or a mix of Hard and Soft Asserts.

An example of using a hard assert is failing to sign into an application. It’s best to use a Hard Assertion because there is no value in attempting to executing the remaining test. However, if verifying an application is not critical then we can use a Soft Assertion. We must use a method called assertAll to receive all AssertionErrors and fail the Test Script.

Design Pattern – Page Object Model

What is a Design Pattern?
A design pattern is a reusable solution in software development that address recurring problems. It has a relationship between classes, objects, and methods to perform the solution. To solve those recurring problems, our design pattern should incorporate Object-Oriented Programming concepts.

How is a Design Pattern a Best Practice Tip For Selenium?
The Design Pattern is a best practice tip because it has 3 benefits. It helps reduce code reusability, improve code readability, and promote code maintainability.

  • Code reusability is when we reuse code in multiple locations in our program.
  • Code readability means the code is easy to follow.
  • Code maintainability means it will take less time to make a change to our code.

The most popular design pattern is Page Object Model. It has classes that represent each page of an application. Elements and interactions with those elements are stored separately from our Test Scripts. Here’s a screenshot that illustrates the Page Object Model.

Page Object Model

Automation Framework

What is an Automation Framework?
An automation framework is a comprehensive set of tools used for building test scripts to support automation. The tools are integrated with guidelines and processes such as:

  • Test Data
  • Test Libraries
  • Coding Standards
  • Reusable Modules

How an Automation Framework a Best Practice Tip For Selenium?
An automation framework is a best practice tip because it produces desired results and processes that’s scalable. The following are some frameworks for Selenium automation:

  • Data-Driven Framework uses an external source like Excel to send data as input to our Automation Test Script.
  • Keyword-Driven Framework reads a set of instructions based on keywords from an external source like Excel.
  • Hybrid Framework combines the Data-Driven and Keyword-Driven Frameworks.
  • Behavior Driven Development (BDD) is a development approach that facilitate communication between technical and non-technical people. Test Scripts are written in a plain English language called Gherkin.

Continuous Integration/Continuous Deployment

What is Continuous Integration/Deployment?
Continuous Integration and Continuous Deployment (CI/CD) is a process that involves frequent and reliable code changes to the main branch. A couple of benefits consist of high quality and fast delivery. Quality is high when the build has minimal errors. Delivery is fast to the customers after committing repeated code changes.

How is Continuous Integration/Deployment a Best Practice Tip For Selenium?
CI/CD is a best practice tip because it empowers technical teams to focus on quality and meeting business requirements. Selenium Automation helps by responding to frequent changes by the customer. We have the option of kicking off Test Scripts within their own CI job or integrated with the development CI job. Here’s the difference between CI and CD:

  • Continuous Integration (CI) authorizes a consistent way to build, package, and test applications.
  • Continuous Deployment (CD) automates the delivery of applications.

Combining CI/CD requires Selenium to continuous testing applications so they are delivered with quality and sent to customers.

That wraps up Selenium WebDriver: From A to Z and the best practices for Selenium Automation. Hope you enjoyed and would love for you to share your feedback and/or additional Selenium tips and tricks in the comments below 😊

About the author

Rex Jones II

Rex Jones II has a passion for sharing knowledge about testing software. His background is development but enjoys testing applications.

Rex is an author, trainer, consultant, and former Board of Director for User Group: Dallas / Fort Worth Mercury User Group (DFWMUG) and member of User Group: Dallas / Fort Worth Quality Assurance Association (DFWQAA). In addition, he is a Certified Software Tester Engineer (CSTE) and has a Test Management Approach (TMap) certification.

Recently, Rex created a social network that demonstrate automation videos. In addition to the social network, he has written 6 Programming / Automation books covering VBScript the programming language for QTP/UFT, Java, Selenium WebDriver, and TestNG.

✔️ YouTube https://www.youtube.com/c/RexJonesII/videos
✔️ Facebook http://facebook.com/JonesRexII
✔️ Twitter https://twitter.com/RexJonesII
✔️ GitHub https://github.com/RexJonesII/Free-Videos
✔️ LinkedIn https://www.linkedin.com/in/rexjones34/

Leave a Reply