Home > Back-end >  Call methods from string
Call methods from string

Time:12-01

I have an excel with scenarios and flag values. Based on flag value respective method should be called.

enter image description here

Please take a look at below snippet.

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;

public class WebAutomation{
  public static void main(String[] args) throws InterruptedException, IOException {
        String projectpath = System.getProperty("user.dir");
        XSSFWorkbook workbook = new XSSFWorkbook(projectpath "\\Data\\Driver.xlsx");
        XSSFSheet sheet = workbook.getSheet("Driver");
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int i = 1; i < rowCount; i  ) {
                //i am looking for a snippet here to call the respective method
              }
        workbook.close();
 }
  public static void MethodOne() {
   System.out.println("Calling First method"); 
  }
  public static void MethodTwo() {
   System.out.println("Calling Second method"); 
  }
}


I implemented above approach by using QTP long before and trying the same in Selenium as well but couldn't succeed. By using Eval command in VBscript i am able to call functions and now looking for something like that.

Thanks in advance...

CodePudding user response:

You will need to use reflection to achieve this. this will allow to add other method later in column 'Scenario' of you Excel file (e.g.: MethodThatCouldBeCalledLaterFromExcel()) so that they can be called by reflection. You will find more resources about Java reflection here: https://www.oracle.com/technical-resources/articles/java/javareflection.html

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WebAutomation {

    public WebAutomation() {
        super();
    }

    /**
     * Call method(s) depending on parameter 'flagParameter' value
     * 
     * @param flagParam The flag that will determine the method(s) to be called
     * @throws IOException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public void executeMethod(String flagParam)
            throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        System.out.println("Testing parameter '"   flagParam   "' ---------------------------");
        String projectpath = System.getProperty("user.dir");
        XSSFWorkbook workbook = new XSSFWorkbook(projectpath   "\\Driver.xlsx");
        XSSFSheet sheet = workbook.getSheet("Driver");
        int rowCount = sheet.getPhysicalNumberOfRows();
        String scenario = "";
        String flag;
        // Iterate on row in Excel file
        for (int i = 1; i < rowCount; i  ) {
            try {
                // Get the value of cell in column 'Scenario'
                scenario = sheet.getRow(i).getCell(0).getStringCellValue();
                // Get the value of cell in column 'Scenario'
                flag = sheet.getRow(i).getCell(1).getStringCellValue();
                // If 'flagParameter' value equals 'flag' value, the method which name is defined
                // by 'scenario' value will be called
                if (flag.equals(flagParam)) {
                    //Using reflection to get method to be called
                    Method m = WebAutomation.class.getDeclaredMethod(scenario);
                    //If method is 'private', make it accessible
                    m.setAccessible(true);
                    //Invoke method
                    m.invoke(this);
                }
            } catch (NoSuchMethodException | SecurityException e) {
                e.printStackTrace();
            }
        }
        workbook.close();
    }

    private void CreateNewOrder() {
        System.out.println("Calling CreateNewOrder");
    }

    private void UpdateOrder() {
        System.out.println("Calling UpdateOrder");
    }

    private void DeleteOrder() {
        System.out.println("Calling DeleteOrder");
    }

    private void CancelOrder() {
        System.out.println("Calling CancelOrder");
    }

    private void MethodThatCouldBeCalledLaterFromExcel() {
        System.out.println("Calling MethodThatCouldBeCalledLaterFromExcel");
    }

    private void ReturnOrder() {
        System.out.println("Calling ReturnOrder");
    }

    public static void main(String[] args) {
        WebAutomation automation = new WebAutomation();
        try {
            String[] params = { "Yes", "No" };
            for (String param : params) {
                automation.executeMethod(param);
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IOException e) {
            e.printStackTrace();
        }
    }
}
  • Related