dimanche 14 octobre 2018

How to invoke methods of a class where each method has different arguments at run time using java reflections?

I am working on keyword driven framework for selenium. I have written my methods in a seperate class. The following is the class that contains action methods for opening home page, entering username and password and clicking on login button.

package actions;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class adminlogin {
    WebDriver driver;
    public adminlogin(WebDriver driver){
        this.driver = driver;
    }
    public void adminopenhomepage() {
        driver.get("http://localhost/carrental/admin/");
    }
    public void adminenterusername(WebElement username) {
        username.sendKeys("admin");
    }
    public void adminenterpassword(WebElement password) {
        password.sendKeys("Test@12345");
    }
    public void adminclickloginbutton(WebElement loginbutton) {
        loginbutton.click();
    }
    public void adminclosebrowser() {
        driver.close();
    }
}

I have the keywords in a list and I iterate through the keywords and call the above methods using reflections. One way I would like to do is get the parameter types and number of parameters at run time so that I can pass the parameters accordingly. I am trying to get the method using getDeclaredMethod(keyword) but this is working only for those methods that don't have parameters like adminopenhomepage() and giving exception no such method for all others as they accept parameters. Can anyone tell me how to solve this?

for(String str : originalkeywords) {
    String keyword = str;
    String actioncl = keywordvsac.get(keyword);
    String objectcl = keywordvsor.get(keyword);

    Class<?> cls = Class.forName("actions."+actioncl);
    Method methodcall = cls.getDeclaredMethod(keyword);

    Parameter[] parameters = methodcall.getParameters();        
    System.out.println(Arrays.toString(parameters));
}

Thank you.





Aucun commentaire:

Enregistrer un commentaire