Convention of Selenium Script

25 / May / 2016 by Kimi Agarwal 0 comments

Like developers write code to implement a functionality, testers write scripts to test it. Similarly, testers also have to be careful in writing these scripts to maintain the standard of the code. Often it happens that a script running for the first time does not run the second time or shows failed test cases in later runs. To overcome this situation in automation testing, there are several goof-ups that can be avoided in the first place.

These are suggested guidelines while starting with selenium scripts:-

1. Use Base URLs
Set a standard Base URL for the script. Multiple environments are there where the developer or tester do testing such as development, QA, UAT, and production; you may wish to configure your Base URL with an environment-specific variable.

2. Locator Order: id -> name -> CSS -> xpath
An ‘id’ is a better methodology than ‘xpath’ in Selenium to locate an element on a web page.

As the ‘id’ will be unique on a web page, it is like an Employee Id or an Account Number which will never get changed. If some changes are being made in the application, the ‘xpath’ of the respective web element will get change, yet its ‘id’ will stay same. There are numerous situations when an element does not have an ‘id’ around; then you need to approach the developer to include ‘id’ for that individual element.

public void login(){

    WebElement em = driver.findElement(“email”));
    WebElement pwd=driver.findElement(“pass”);


3. Avoid using Thread.sleep() Method, Prefer Wait
Avoid using sleep method because it pauses test execution for specified amount of time, and during this time, the thread keeps the lock it has gained consequently not permitting the other thread to execute the same function. Rather than this, we should use implicit wait or explicit wait.

  • Implicit wait time is applied to all elements in your script. It should be preferred over thread.sleep() however, implicit wait slows down the execution process of your test script if the application is responding normally. The default value of implicit wait is zero.
  • Explicit wait time is applied only to a specific element.

Instead of Thread.sleep() method
public void clickOnButton() {

    WebElement bt = driver.findElement(“submit”));;

Prefer Explicit Wait
public void clickOnButton() {

    WebDriverWait wait =new WebDriverWait(driver, 20);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(“submit”)));


4. Use Page Object Model
POM is just a design pattern not a framework.
In this model try to design the test cases in such a way that it is easy to understand. If there are any UI changes in the website than we have to update the code in ‘object repository’ classes only not in the ‘test’ classes.

Why use Page Objects?

  • Readable: Can be easily interpreted by the business shareholder.
  • Writable: Simple to compose, stays away from superfluous duplications.
  • Extensible: Usefulness can be included without breaking contracts and existing usefulness.
  • Maintainable: By letting the usage points of interest alone for experiments, you are all around protected against changes to the [automated test].

WebDriver driver;
public void launchBrowser(String url){

    driver = newFirefoxDriver();

public voidtestContactUsPage(String firstname, String lastname, String email, String phoneNo){



5. TestNG: Tester’s Choice
There are several framework for writing the test cases like JUnit, TestNG, etc. As JUnit is a unit testing framework which is commonly used by the developers for sanity testing, whereas in TestNG we can do unit testing, integration testing, functional testing, etc.

But TestNG framework is preferred more than JUnit.

  • In JUnit there is a restriction that we have to declare @BeforeClass and @AfterClass but not in the TestNG.
  • TestNG annotations are simple to interpret and implement.
  • Parallel execution of test cases is possible with the TestNG.

These are the guidelines test engineer ought to take after. I hope you will have an easy time writing your next automation script.


Leave a comment -