Felipe
Felipe

Reputation: 41

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

Upvotes: 3

Views: 2284

Answers (2)

RocketRaccoon
RocketRaccoon

Reputation: 2609

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.

Upvotes: 3

Felipe
Felipe

Reputation: 41

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());
    }
}

Upvotes: 1

Related Questions