Soumya Ranjan Das
Soumya Ranjan Das

Reputation: 23

How to use dataProvider against multiple testng method using selenium testng code

could you please suggest what could be the issue with my code , After using dataprovider my test method Test2 and Test3 both are looks like unreachable,can you provide me the solution that how i could fix this issue without putting all the code into single Test method

  1. while using data provider i'm only able to reach Test1 code means after executing my code i am getting below output (see below output)

OUTPUT

Before Test case

Print Test1

where my expected output should be (see below out put)

OUTPUT

Before Test case

Print Test1

Print Test2

Print Test2

public class Test extends DriverConfig {
    
    @DataProvider
    public Iterator<String> getTestData() throws Exception {
        driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
        ArrayList<String> getclientProduct = sftpCon.clientProduct("Client Type");
        System.out.println("getclientProduct----------" + getclientProduct);
        System.out.println("Before Test case");
        return getclientProduct.iterator();
    
    }
    
    @Test(priority = 1, retryAnalyzer = com.brcc.tool.RetryFailedTestCases.RetryTestCases.class,dataProvider = "getTestData")
    public void A1(String clientName,String clientAddress) throws Exception {

        wait = new WebDriverWait(driver, 10);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        System.out.println("Print Test1");
    }
    @Test(priority = 2, retryAnalyzer = com.brcc.tool.RetryFailedTestCases.RetryTestCases.class)
    public void A2() throws Exception {
        wait = new WebDriverWait(driver, 80);
        System.out.println("Print Test2");
        
    }

    @Test(priority = 3, retryAnalyzer = com.brcc.tool.RetryFailedTestCases.RetryTestCases.class)
    public void A3() throws Exception {
        wait = new WebDriverWait(driver, 10);
        System.out.println("Print Test3");
        
    }

}

Upvotes: 1

Views: 758

Answers (2)

Ravi Pratap
Ravi Pratap

Reputation: 17

You can use @Factory annotation for these type of scenarios. Below is the code snippet

public class Test{
        
    String data;
    
    @DataProvider(name="dataProvider")
    public static Iterator<String> getTestData() throws Exception {
        ArrayList<String> data = new ArrayList<String>();
        data.add("Test Data");
        return data.iterator();
    
    }
    
    @Factory(dataProvider="dataProvider")
    public Test(String data) {
        this.data = data;
    }
    
    @Test(priority = 1)
    public void A1() {
        System.out.println("Print Test 1: " + data);
    }
    @Test(priority = 2)
    public void A2() {
        System.out.println("Print Test 2: " + data);
    }

    @Test(priority = 3)
    public void A3()  {
        System.out.println("Print Test 3: " + data);
    }
        
}

OUTPUT:

Print Test 1: Test Data
Print Test 2: Test Data
Print Test 3: Test Data

===============================================
Single Test
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Upvotes: 0

Janesh Kodikara
Janesh Kodikara

Reputation: 1821

Name of of the data provider was not defined in the code. Name of the data provider should match with the name used within @Test

@DataProvider (name = "getTestData")

Following should work with the expected order of execution.

public class Test extends DriverConfig{


@BeforeClass
public void beforeClass(){
    driver = new ChromeDriver();
    System.out.println("Before Test case");
}

@DataProvider (name = "getTestData")
public Iterator<String> getTestData() throws Exception {
   
    ArrayList<String> getclientProduct = sftpCon.clientProduct("Client Type");
    System.out.println("getclientProduct----------" + getclientProduct);
   
    return getclientProduct.iterator();

}

@Test(priority = 1, retryAnalyzer = com.brcc.tool.RetryFailedTestCases.RetryTestCases.class,dataProvider = "getTestData")
public void A1(String clientName,String clientAddress) throws Exception {

    System.out.println("Print Test1");
}
@Test(priority = 2, retryAnalyzer = com.brcc.tool.RetryFailedTestCases.RetryTestCases.class)
public void A2() throws Exception {
 
    System.out.println("Print Test2");

}

@Test(priority = 3, retryAnalyzer = RetryFailedTestCases.RetryTestCases.class)
public void A3() throws Exception {
    System.out.println("Print Test3");

}

}

Upvotes: 0

Related Questions