I am new to Page-Object model automation using selenium and java. I am using the Page Object model and have each page as a single class and the actions in that page as methods.Using excel to keep read test data. I have a test for searching for client using various parameters like client number, policy number, surname, firstname, webrefernce, email eand many more...... Now I have to provide all parameters in method signature otherwise test is failing with dataprovider mismatch error. I have a GetData method which provide string array from excelsheet specified.
Is it possible to make parameters optional so that I can specify only the parameters required for that particular test in the test method's signature.? In actual test there are 15 parameters and additional combinations. (If this is not possible, I have to split the data in to 16 different tab and define data providers for each tests separately). Or any other way to achieve this? Thanks
Current code:
@DataProvider(name="ClientSearchData")
public String[][] getTestData() {
String[][] testRecords = getData("TestData_igo4.xlsx","ClientSearch");
return testRecords;
}
@BeforeTest
public void setUp() {
init();
}
@Test(dataProvider="ClientSearchData")
public void verifyClientSearchByClientNumber(String clientnumber, String policynumber, String surname, String webreference, String email) {
//code for search by clientnumber
}
@Test(dataProvider="ClientSearchData")
public void verifyClientSearchByPolicyNumber(String clientnumber, String policynumber, String surname, String webreference, String email) {
//Code for search by policynumber
}
I want something like the following to avoid unnecessary parameters for each tests..
@DataProvider(name="ClientSearchData")
public String[][] getTestData() {
String[][] testRecords = getData("TestData.xlsx","ClientSearch");
return testRecords;
}
@BeforeTest
public void setUp() {
init();
}
@Test(dataProvider="ClientSearchData")
public void verifyClientSearchByClientNumber(String clientnumber) {
//code for search by clientnumber
}
@Test(dataProvider="ClientSearchData")
public void verifyClientSearchByPolicyNumber(String policynumber) {
//Code for search by policynumber
}
I think what you are looking is Varargs. You can simply do like below
@DataProvider(name = "testData")
public static Object[][] testDataProvider() {
return new Object[][] {new String[]{"a","b","c"}};
}
@Test(priority=3,dataProvider = "testData")
public void test1(String... str1) {
System.out.println("first string"+" "+str1[0]);
}
@Test(priority=4,dataProvider = "testData")
public void test2(String... str2) {
System.out.println("second string"+" " + str2[1]);
}
The above prints
first string a
second string b
In above code just adjust data provider according to your getTestData
Three dots ...
is the key here
EDIT:
You can actually do it without Varargs. The below also prints same
@Test(priority=3,dataProvider = "testData")
public void test1(String str1[] ) {
System.out.println("first string"+" "+str1[0]);
}
@Test(priority=4,dataProvider = "testData")
public void test2(String str2[]) {
System.out.println("second string"+" " + str2[1]);
}