0

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

 }
Silverbullet
  • 144
  • 1
  • 12
  • Create a dataobject for ClientSearchData having getter and setter methods. Set the data in excel access code using setters. From the dataprovider return new Object[][] - {{ new ClientSearchDO(name,id...) },{ new ClientSearchDO(name,id...)}}. In Test method change parameter to type ClientSearchDO. Access relevant data through getter methods. Check this too -- http://testng.org/doc/documentation-main.html#parameters-dataproviders – Grasshopper Nov 08 '17 at 17:47

2 Answers2

1

You can model your dataprovider based on the method calling it. What I would do is probably write logic based on method name. Pass the Method object to your dataprovider, based on the name of the method, create your Object[][].

say

public Object[][] dp(Method m) {
key = m.getName.replace("verifyClientSearchBy","");
//From excel data, just fetch key column's values or put
//logic here whatever is convenient
//Build your Object[][] with only one value
niharika_neo
  • 8,441
  • 1
  • 19
  • 31
1

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]);

  }
user1207289
  • 3,060
  • 6
  • 30
  • 66