TestNG Tutorials

HOME

Chapter 1 Introduction to TestNG
Chapter 2 How to download and install TestNG in Eclipse
Chapter 3 TestNG Annotations
Chapter 4 Assertions in TestNG
Chapter 5 Hard Assert and Soft Assert
Chapter 6 How to create and run TestNG.xml of a TestNG class
Chapter 7 Run TestNG tests from Command Line
Chapter 8 Execute Testng.xml using batch file
Chapter 9 How to pass Parameters in TestNG
Chapter 10 Prioritizing Test Cases in TestNG: Complete Guide
Chapter 11 How to disable Selenium Test Cases using TestNG Feature – @Ignore
Chapter 12 How to Use dependsOnMethods() in TestNG for Selenium Test Case Dependency
Chapter 13 How to group Tests in Selenium
Chapter 14 InvocationCount in TestNG
Chapter 14 How to run Parallel Tests in Selenium with TestNG
Chapter 15 Cross Browser Testing using Selenium and TestNG
Chapter 16 Screenshot of Failed Test Cases in Selenium WebDriver
Chapter 17 TestNG Listeners in Selenium
Chapter 18 How to Retry failed tests in TestNG – IRetryAnalyzer
Chapter 19 DataProviders in TestNG
Chapter 20 DataProvider in TestNG using Excel
Chapter 21 Parallel testing of DataProviders in TestNG
Chapter 22 TestNG Interview Questions

Test Framework (Maven)

Chapter 1 Integration of REST Assured with TestNG
Chapter 2 Integration of Cucumber with Selenium and TestNG
Chapter 3 Integration Testing of Springboot with Cucumber and TestNG

Test Framework (Gradle)

Chapter 1 How to create Gradle project with Selenium and TestNG
Chapter 2 Gradle Project with Cucumber, Selenium and TestNG

Allure Report with TestNG

Chapter 1 Gradle – Allure Report for Selenium and TestNG
Chapter 2 Gradle – Allure Report for Cucumber, Selenium and TestNG
Chapter 3 Integration of Allure Report with Rest Assured and TestNG
Chapter 4 Gradle – Allure Report for Selenium and TestNG

ExtentReports with TestNG

Chapter 1 ExtentReports Version 5 for Cucumber 6 and TestNG
Chapter 2 PDF ExtentReport for Cucumber and TestNG
Chapter 3 ExtentReports Version 5 for Cucumber 7 and TestNG

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!!