Chapter 10_3: set test priority in selenium

In this chapter we will study how to give priority to particular test cases.

We can run single or multiple test cases in TestNG code.

If test priority is not defined while running multiple test cases, TestNG assigns all @Test a priority as zero(0).while running, lower priorities will be scheduled first.

Demo of TestNG code without Priority:

Lets take an example where sequencing will be required in order to pass all test-cases.

 Scenario: write a code where you are required to perform a Google search with keyword as “mercury tours”. Now, verify that Browser title is changed to “mercury tours – Google Search”.

Note: for each step your code will be in separate methods

Method 1: Open Browser say chrome (openBrowser())

Method 2: Launch Google.com (launchGoogle())

Method 3: Perform a search using “mercury tours” (performSearchAndClick1stLink())

Method 4: Verify Google search page title (mercury tours PageTitleVerification())

Complete webdriver code:

package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class priorityinTestNG {
	WebDriver driver = null;
// Method 1: Open Browser 		
@Test
public void openBrowser() 
     {
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
driver = new ChromeDriver();		
driver.manage().window().maximize();
	}
// Method 2: Launch Google.com
@Test
public void launchGoogle() 
{
driver.get("http://www.google.co.in");
	}
// Method 3: Perform a search using "mercury tours"
@Test
public void peformSeachAndClick1stLink() {
WebElement search = driver.findElement
(By.cssSelector("input[class='gLFyf gsfi'][type='text'][title='Search']"));
search.sendKeys("mercury tours");
search.sendKeys(Keys.ENTER);
	}
// Method 4: Verify Google search page title.
@Test
public void PageTitleVerification() {
String pagetitle = driver.getTitle();
		System.out.println(pagetitle);
Assert.assertTrue(pagetitle.contains("mercury tours-Google Search"), "title is not matching");
	}
}

Explanation of code

we have created 4 test cases for performing each action in an self-directed methods.

  • The first method (openBrowser) states to initialize Chrome browser.
  • The second method (launchGoogle) states that launch Google.com.
  • The third method (peformSeachAndClick1stLink)states that perform a search in the search box with a search as mercury tours and
  • The fourth and last method (PageTitleVerification) states that click on search icon of Google and verify that browser title has been changed to mercury tours – Google Search.

Now run this code using TestNG you will find all the test cases are failing. The reason for failure: as there is a dependency of previous test case to pass, only than current running test case will be passed.

In that case

  • First method which is executed is openBrowser(). It got passed because it does not have any dependency.
  • Second method executed is TitleVerification(); it is failed because we are trying to click search button and verifying browser title.
  • You can see that if search activity is not process then how any other step can get passed. Hence, this is the reason my test cases are failing.

Demo of TestNG code without Priority in Alphabetical Order

If we don’t mention any priority, TestNG will execute the @Test methods based on alphabetical order of their method names irrespective of their place of implementation in the code.

package TestNG;
import org.testng.annotations.Test;
public class priorityinTestNGannotations 
{
    @Test
public void c_method(){
System.out.println("I'm in method E");
	}
    @Test
	public void b_method(){
	System.out.println("I'm in method B");
	}
   @Test
	public void a_method(){
	System.out.println("I'm in method C");
	}
 @Test
	public void e_method(){
	System.out.println("I'm in method D");
	}
 @Test
	public void d_method(){
	System.out.println("I'm in method A");
	}
}

The output of above code will be:

[RemoteTestNG] detected TestNG version 7.0.0
I'm in method A
I'm in method B
I'm in method C
I'm in method D
I'm in method E
PASSED: a_method
PASSED: b_method
PASSED: c_method
PASSED: d_method
PASSED: e_method

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0
===============================================

Here we have written the methods in a random way(E,B,C,D,A)TestNG execute this methods in a alphabetical order.

How to set Priority in TestNG

As we have seen in the above example that sequencing required in order to pass this scenario, so we will be modifying the previous code with Priority so that each test should run against to the priority assigned to them.

package TestNG;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class setpriority_in_selenium {
	WebDriver driver;
// Method 1: Open Browser 
@Test(priority = 1)
public void openBrowser() {
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
	}

// Method 2: Launch Google.com
@Test(priority = 2)
public void launchGoogle() {
driver.get("http://www.google.co.in");
	}
// Method 3: Perform a search using "Facebook"
@Test(priority = 3)
public void peformSeachAndClick1stLink() {
WebElement search = driver.findElement
(By.cssSelector("input[class='gLFyf gsfi'][type='text'][title='Search']"));
search.sendKeys("mercury tours");
search.sendKeys(Keys.ENTER);
	}
// Method 4: Verify Google search page title.
@Test(priority = 4)
public void PageTitleVerification() {
String pagetitle = driver.getTitle();
System.out.println(pagetitle);
Assert.assertTrue(pagetitle.contains("mercury tours-Google Search"), "title is not matching");
     }
}

assigned priority to each testcase, and run the above code using TestNG Here, we can see the test cases are prioritized. Test case having lower priority are executed first . Hence, all test cases are passed now.