InvocationCount in TestNG

HOME

InnvocationCount is one of the feature available in TestNG. InvocationCount is used when we want to run the same test multiple times.  If we want to run single @Test 10 times at a single thread, then invocationCount can be used. To invoke a method multiple times, the below syntax is used.

@Test(invocationCount = 3)

In this example, the @Test method will execute for 3 times each on a single thread.

In this tutorial, we will illustrate how to get the current invocation count.

Step 1 − Create a TestNG class, TestInvocationCount.

Step 2 − Write two @Test methods in the class TestInvocationCountas shown in the programming code section below. Add invocationCount=3 to method verifyLinkedIn and 2 to validLoginTest.

Step 3 − Create the testNG.xml as given below to run the TestNG classes.

Step 4 − Now, run the testNG.xml or directly TestNG class in IDE or compile and run it using command line.

Step 5 − In the output, the user can see a total of 1 thread running sequentially for all invocations of @Test.

import static org.testng.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class TestInvocationCount {
	
	WebDriver driver;
	
	@BeforeMethod
    public void setup() throws Exception {
        
       driver = WebDriverManager.firefoxdriver().create();

       driver.get("https://opensource-demo.orangehrmlive.com/");
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.manage().window().maximize();
    }
	
	@Test(invocationCount = 3)
    public void verifyLinkedIn() {

		System.out.println("Test Case 1 with Thread Id - "+Thread.currentThread().getId());
    	
        driver.manage().window().maximize();
        Boolean linkedInIcon = driver.findElement(By.xpath("//*[@id='social-icons']/a[1]/img")).isEnabled();
        assertTrue(linkedInIcon);
    }

    
    @Test(invocationCount = 2)
    public void validLoginTest() throws InterruptedException {
    	
    	System.out.println("Test Case 2 with Thread Id - "+Thread.currentThread().getId());

        driver.findElement(By.name("txtUsername")).sendKeys("Admin");
        driver.findElement(By.name("txtPassword")).sendKeys("admin123");
        driver.findElement(By.id("btnLogin")).click();
        String expectedTitle = driver.findElement(By.xpath("//*[@id='content']/div/div[1]/h1")).getText();
        Assert.assertTrue(expectedTitle.contains("Dashboard"));
    }

	    @AfterMethod
	    public  void closeBrowser() {
	    	
	    	driver.quit();
	    			  
	    }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Invocation Test">
    <classes>
      <class name="com.example.listeners.TestInvocationCount"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

The output of the above program is

We can add threadPoolSize to the @Test.

threadPoolSize – It defines the size of the thread pool for any method. The method will be invoked from multiple threads, as specified by invocationCount.

@Test(invocationCount = 3, threadPoolSize)

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Leave a comment