1

I have a test which creates a record on a registration page by giving Name, Phone Number, Primary mode of contact. The goal is to test for the same phone number and mode of contact, but for different variations of name ( eg: for 3 names). Test should run 3 times. Name1 with phone number and mode of contact, Name2 with phone number and mode of contact, Name3 with phone number and mode of contact

I am trying to use a data provider to provide data to my test. But I am not sure how to provide the list of names in the data provider and return this to my test.

public List<String> names;

@DataProvider(name="TestData")
public object[][] getTestData() {
    names = new ArrayList<String>();
    names.add("Name1");
    names.add("Name2");
    names.add("Name3");
    Object[][] returnNames = new Object[names.size()][];
    for(int i=0;i<names.size(); i++) {
        returnNames[i] = new Object[]{names.get(i)};
    }

    return new Object[][] {
    {returnNames, "1234567890", "Phone"} // (->Problem 1:I am not sure if this is right)
    };
}

@Test(dataprovider="TestData")
public void testMethod(List<String> names, String phone, String modeOfContact) {
// (-> Problem 2: Is the parameter initialization right in @Test?)

//Code here

}
Lahiru Jayathilake
  • 601
  • 2
  • 10
  • 28
Ronnie
  • 251
  • 1
  • 3
  • 17

1 Answers1

1

Probably the easiest way is to just initialize the data array inline:

@DataProvider(name="TestData")
public static Object[][] getTestData() {
    String phoneNumber = "123456789";
    String modeOfContact = "Phone";

    return new String[][] {
            {"Name1", phoneNumber, modeOfContact},
            {"Name2", phoneNumber, modeOfContact},
            {"Name3", phoneNumber, modeOfContact}
    };
}
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156