Showing posts with label Run same test multiple times using TestNG. Show all posts
Showing posts with label Run same test multiple times using TestNG. Show all posts

Wednesday, January 17, 2018

Interview Question - How to run same test case multiple times

In this blog, We will see how to run same test case multiple times. We can run same test case multiple times with help of @Test attribute 'invocationCount'.

@Test(invocationCount=?):

This invocationCount determined how many times TestNG should run this particular test method.

Example:

@Test(invocationCount = 2)
public void repeatTest()
{
     System.out.println("Execute Test 2 times");
}

Output:

The repeatTest() method will run 2 times.

Execute Test 2 times
Execute Test 2 times

PASSED: repeatTest
PASSED: repeatTest

Now let's see how to use invocationCount in Selenium script with the example.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class RunSameTestMultipleTimes 
{
@Test(invocationCount = 2)
public void loadApplication()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println("Page Title is " +driver.getTitle());
Assert.assertEquals("Google",driver.getTitle());
driver.close();
driver.quit();
}
}

Output:

You will notice that Firefox browser will prompt out and close 2 times.

Page Title is Google
Page Title is Google

PASSED: loadApplication
PASSED: loadApplication