-1

I have written a POM for test automation of a web UI using Selenium Webdriver using java.

In the POM, I have created a repository of methods of all the page fields. Basically the web page has more than 100 fields. So I am using an excel sheet for data with the help of apache poi.

The problem is, I want to call all the method in the test script and that particular method should be executed only if the excel sheet has data for that method. I am a beginner to coding so please help with this.

(I am reading the data from the excel sheet using column name and the excel template has a column for all the 100+ fields.)

Now I need to write test script for each test cases. But I want my program to run depending on the data from the excel sheet, ignoring the method for which there is no data in the excel sheet.

Below is a similar example. I have two classes POM and TestCase.

public class POM(){
   public method1(String a){
       sop("1st method"+a)
   }

   public method2(String a){
       sop("2nd Method"+a)
   }

   public method2(String a){
       sop("3rd Method"+a)
   }
}

Below is the test case class, which will call the methods of class POM. The data for the methods is provided by excel file.

public class TestCase(){
  main(){
    POM obj = new POM();
    obj.method1(poi.getDataFromExcel("column name", row_number));
    obj.method2(poi.getDataFromExcel("column name", row_number));
    obj.method3(poi.getDataFromExcel("column name", row_number));
    }
 }

So now again my question is, I don't have any data for method2 in my excel sheet. How can I skip that method?

  • Is you task structured like this one: https://stackoverflow.com/questions/4247933/loop-through-array-each-element-a-junit-test ? – belwood Sep 13 '19 at 18:23
  • No, in that they are talking about if one of the data fails in those given arrays, but my question is about if there is no data from the Excel sheet for my method how can I skip that particular method. – Manjunath Chinnanagoudar Sep 13 '19 at 18:29
  • I was referring to the "structrue" of your task, not the goal - i.e. repeatedly calling a method with different data. If yours is like that, then in the loop, could you simply have an IF statement that doesn't execute the test if there is no data? – belwood Sep 13 '19 at 18:34
  • Should I use if(null) or if(text==null) – Manjunath Chinnanagoudar Sep 13 '19 at 18:37
  • 1
    Can you post the code for the method in question? I'm not clear what you are asking. – Greg Burghardt Sep 13 '19 at 19:13
  • Added a sample code for better understanding. – Manjunath Chinnanagoudar Sep 14 '19 at 05:26
  • Changed the code. I'm getting data from the Excel sheet using poi by calling the method getDataFromExcel(), which takes column name and row number as the parameters and returns the data from that particular cell. Hope I am making sense – Manjunath Chinnanagoudar Sep 15 '19 at 05:40

2 Answers2

1

According your description you wants calling the obj.method* in your TestCase only if there is a not empty string returned from poi.getDataFromExcel("column name", row_number).

That would look like so:

...
POM obj = new POM();
String cellValue = poi.getDataFromExcel("column name", row_number);
if (cellValue != null && !cellValue.isEmpty()) {
    obj.method1(cellValue);
}
...
Axel Richter
  • 56,077
  • 6
  • 60
  • 87
0

While reading the data why can't you just skip the function if there is no data for the function.

Something like this,

if(data != null){ //Then call the function ..... }