How to set priority in TestNG

How to set priority in TestNG

TestNG is a testing framework. It’s designed to cover different phases and categories of tests − unit, functional, end-to-end, integration, regression etc.

What’s new in Selenium 3

In this step-by-step guide, we will be using Selenium 3 to showcase the example. If you have not yet installed selenium 3, please check the following article:

How to install and setup Selenium 3

Test case example

Typically test case will have multiple steps and steps are to be executed in a specific order. In this case, let’s take a test case comprising of ‘Yahoo Search’.

Test case: Search ‘Techcanvass Courses Selenium’ on yahoo page and verify techcanvass in the search result page. Test case will have following steps. For test case to pass all steps need to be executed in a specific sequence.

Step 1: Open Browser

Step 2: Open Yahoo Home Page

Step 3: Enter search string and click on search button

Step 4: Verify title of search result page

Step 5: Verify string ‘techcanvass’ on search result page

Step 4: Close browser

To make code reusable, we will be creating separate methods for each step. Let’s look at code for this example:

package com.tc.testngbasic;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

public class YahooSearch {

WebDriver driver;

@Test ( description =”Open Browser”)

public void openBrowser()

{

System.setProperty(“webdriver.gecko.driver”, “D:\\ drivers\\geckodriver.exe”);

 //Replace the driver path to your machine path

driver = new FirefoxDriver();

}

@Test  ( description=”Open Yahoo homePage”)

public void openYahooPage() {

driver.navigate().to(“http://www.yahoo.com”);

}

@Test    (  description=”Enter search string and click search button”)

public void peformSearch() throws InterruptedException {

driver.findElement(By.id(“UHSearchBox”)).clear();

driver.findElement(By.id(“UHSearchBox”)).sendKeys(“Techcanvass Courses Selenium”);

driver.findElement(By.id(“UHSearchWeb”)).click();

Thread.sleep(3000);

}

@Test   ( description=”Verify search result page title”)

public void verifyTitle() {

if(driver.getTitle().contains(“Techcanvass Courses Selenium – Yahoo India Search Results”))

System.out.println(“Page title contains \”Selenium – Yahoo India Search Results\” “);

else

System.out.println(“Page title doesn’t contains \”Selenium – Yahoo India Search Results\” “);

}

@Test   ( description=”Verify search result page. Check if result page has techcanvass or not”)

public void verifySearchResult() {

WebElement result = driver.findElement(By.xpath(“//div/span/b”));

if (result != null && result.getText().contains(“techcanvass”)){

System.out.println(result.getText());

System.out.println(“Search result contains \”techcanvass\” “);

} else

System.out.println(“Search result doesn’t contains \”techcanvass\” “);

}

@Test   ( description=”Close Browser”)

public void closeBrower() {

driver.close();

}

}

To execute this program Run-> Run As->TestNG Test.

Test NG test case Execution

Output and sequence of test execution will be displayed on the console. Lets take a look at output.

Test NG Program output

We have written method in the class in the order in which they need to be executed. However, test case “close” is executed before even opening of browser, hence it’s failed.

Reason of failure: Default execution order in TestNG @Test method is alphabetical order.

How to change the default test case execution order

TestNG gives an option of adding ‘priority‘ parameter to @Test. Parameters are keywords to provide additional information for execution of @Test and can modify the annotation’s default function. ‘priority’ is one such parameter for @Test annotation. It takes numerical value and modifies execution order. If priority is specified, methods will be executed in increasing order. Please note syntax is case sensitive, hence need to be specified in lower case otherwise you will get compilation error.

Let’s look at same test case with priority set.

package com.tc.testngbasic;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class YahooSearch {

WebDriver driver;

 

@Test     (priority = 1, description =”Open Browser”)

public void openBrowser() {

System.setProperty(“webdriver.gecko.driver”, “D:\\ drivers\\geckodriver.exe”);

driver = new FirefoxDriver();

}

 

@Test    (priority = 2, description=”Open Yahoo homePage”)

public void openYahooPage() {

driver.navigate().to(“http://www.yahoo.com”);

}

 

@Test    (priority = 3, description=”Enter search string and click search button”)

public void peformSearch() throws InterruptedException {

driver.findElement(By.id(“UHSearchBox”)).clear();

driver.findElement(By.id(“UHSearchBox”)).sendKeys(“Techcanvass Courses Selenium”);

driver.findElement(By.id(“UHSearchWeb”)).click();

Thread.sleep(3000);

}

 

@Test (priority = 4, description=”Verify search result page title”)

public void verifyTitle() {

if(driver.getTitle().contains(“Techcanvass Courses Selenium – Yahoo India Search Results”))

System.out.println(“Page title contains \”Selenium – Yahoo India Search Results\” “);

else

System.out.println(“Page title doesn’t contains \”Selenium – Yahoo India Search Results\” “);

}

 

@Test (priority = 5, description=”Verify search result page. Check if result page has techcanvass or not”)

public void verifySearchResult() {

 

WebElement result = driver.findElement(By.xpath(“//div/span/b”));

if (result != null && result.getText().contains(“techcanvass”)){

System.out.println(result.getText());

System.out.println(“Search result contains \”techcanvass\” “);

} else

System.out.println(“Search result doesn’t contains \”techcanvass\” “);

}

 

@Test (priority = 6, description=”Close Browser”)

public void closeBrower() {

driver.close();

}

}

This time methods run in the order of priority specified as shown below:

testng-program-output-proper-order

Learning TestNG

Techcanvass conducts Selenium courses, which includes TestNG module. We have two Selenium courses:

Selenium Professional & Advance Training

Selenium Training Course

Selenium Certification Course (CP-SAT)

Selenium Certification Course

Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *