简体   繁体   中英

Run multiple tests with Selenium DataProvider

I am currently using testng + selenium to automate my tests, and I have the following scenario:

I need to read from an excel file, transform each row in an object and run 1 test for each of them. I am trying to use annotation @DataProvider to return an Array of objects, however it is only able to return Iterators and Objects[][]. Is there any workaround I can use to return an array of Cliente objects from the DataProvider? I have tried the following code, however it only prints the data from Client2:

public class TestDataProvider 
{
    Cliente cliente;

    @DataProvider(name = "test1")
    public static Object[][] dataMethod() {     
        return new Object[][] { { new Cliente("Client1", "1111111111") },
                                { new Cliente("Client2", "2222222222") }};
    }

    @Test(dataProvider = "test1")
    public void testMethod(Cliente cliente) {
        System.out.println(cliente.getNome() + " " + cliente.getCartao());
    }
}

Edit1: Cliente class:

public class Cliente {
    private static String name;
    private static String card;

    //Construtor method
    public Cliente(String name, String card){
        setname(name);
        setCartao(card);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        Cliente.name = name;
    }

    public String getCard() {
        return card;
    }

    public void setCard(String card) {
        Cliente.card = card;
    }
}

Values that are printed in the console:

Client2 2222222222
Client2 2222222222

So...

Your prerequisites:

  • excel file, each row - one dataset
  • run test for each dataset

What can you do:

  1. Create @DataProvider which return Iterator<Object[]> with your datasets where each Object[] is your row from excel. (the easiest one)
  2. Use @Factory to manually iterate through your datasets and call test methods.
  3. Use @DataProvider to provide data for @Factory and do as above. The 2nd and 3rd options are complicated, but have some benefits if you has other parameters, except datasets, to run tests.

thanks for the help. With RocketRacoon's third suggestion I managed to resolve my problem. Below is a simple example:

public class ProvidedTest 
{
    private static nome;
    private static cpf;
    private static cartao;

    @DataProvider
    public static Object[][] dataProviderMethod() {     
        return new Object[][] { {"Client1", "111111111", "444444444"},
                                {"Client2", "222222222", "555555555"},
                                {"Client3", "333333333", "666666666"}};

    }

    @Factory (dataProvider="dataProviderMethod")
    public ProvidedTest(String nome, String cpf, String cartao){        
        this.nome = nome;
        this.cpf = cpf;
        this.cartao = cartao;

    }

    @Test
    public void testCase(){
        System.out.println(cliente.getNome());
        System.out.println(cliente.getCpf());
        System.out.println(cliente.getCartao());
    }
}

First Way-

Multiple classes can be run with the help of @factory Annotation.

public class Testing_Factory {
@Factory
public Object[] getTestClasses()  
{  
Object tests[]=new Object[2];  
tests[0]=new Testing_Ignore();  
tests[1]=new Testing_Enabled();  
return tests;  
}  

}

Second Way-

We can add condition in data provider or write without condition base on your requirement.

public class Testing_dataProviderSwitchIF {

@DataProvider(name="number")
public Object[][] dataProviderMethod(Method m)
{
    switch(m.getName())
    {
    case "mult":
        return new Object[][] {{ 5,3,1 },{ 7,8,9 }};
    case "add":
        return new Object[][] {{ 1,2,3 },{ 4,5,6 }};
    
    }
    return null;        
}

@Test(dataProvider="number")
public void mult(int a, int b, int c)
{
    System.out.println(a*b*c);
}

@Test(dataProvider="number")
public void add(int a, int b, int c)
{
    System.out.println(a+b+c);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM