java - How to run a method which belongs to various classe -


good day experts,

first let me present code:

public class commonlogin { public static void comlogin(string tc, string address, string test_data, string common_data_tab, string tc_data_tab, string test_result ) throws interruptedexception, ioexception{     string tc = tc;     string url = address;     string td = test_data;     string cdt = common_data_tab;     string tdt = tc_data_tab;     string tr = test_result;     string username;     string password;     string browser;     fileinputstream fis = new fileinputstream(td);           xssfworkbook wb = new xssfworkbook(fis);     xssfsheet sheet = wb.getsheet(cdt);     system.out.println("no. of rows : " + sheet.getlastrownum());     for(int count = 1;count<=sheet.getlastrownum();count++){         xssfrow row = sheet.getrow(count);         system.out.println("running item no. " + row.getcell(0).tostring());         tc = row.getcell(0).tostring();                    username = row.getcell(1).tostring();         password = row.getcell(2).tostring();                browser = new exception().getstacktrace()[1].getclassname();                     system.out.println(browser + " calling");         if(browser.contains("firefox")){             webdriver login = new firefoxdriver();                       login.get(address);             //login.get("https://2015.qa.int.www.mol.com/");             login.findelement(by.xpath("//*[@id='lorem']/img")).isdisplayed();             login.findelement(by.xpath("//*[@id='popupfoot']/a/i")).click();             login.findelement(by.linktext("log in")).click();                 thread.sleep(2000);                      login.findelement(by.xpath("//*[@id='emailaddress']")).sendkeys(username);             login.findelement(by.xpath("//*[@id='password']")).sendkeys(password);                     thread.sleep(2000);             login.findelement(by.xpath("//*[@id='loginform']/footer[1]/table/tbody/tr/td[2]/button")).click();                     thread.sleep(2000);               acctdetailcaptchafirefox.runtest(td,login);         }         else{             webdriver login = new chromedriver();              system.setproperty("webdriver.chrome.driver", "c:\\users\\lam chio meng\\desktop\\work\\chromedriver_win32\\chromedriver.exe");             login.get(address);             //login.get("https://2015.qa.int.www.mol.com/");             login.findelement(by.xpath("//*[@id='lorem']/img")).isdisplayed();//wait safety tips pop display             login.findelement(by.xpath("//*[@id='popupfoot']/a/i")).click(); //click "x" close popup             login.findelement(by.linktext("log in")).click();    //click log in             thread.sleep(2000);                      login.findelement(by.xpath("//*[@id='emailaddress']")).sendkeys(username);             login.findelement(by.xpath("//*[@id='password']")).sendkeys(password);                     thread.sleep(2000);             login.findelement(by.xpath("//*[@id='loginform']/footer[1]/table/tbody/tr/td[2]/button")).click();    //cick on login button                 thread.sleep(2000);                acctdetailcaptchachrome.runtest(td,login);         }     } } 

i draw attention the syntax if(browser.contains("firefox")){ , proceed line acctdetailcaptchachrome.runtest(td,login); line send arguments td , login method runtest in class acctdetailcaptchachrome. however, got few other classes has runtest method , classes name being passed via comlogin method variable tc (refer variable initialization part). so, instead of acctdetailcaptchachrome.runtest(td,login);, have tc.runtest(td,login) tc can class name being passed main method.

some advice on how appreciated. thank you

you can use reflection. here's simple example started.

 package test.oop;   import java.lang.reflect.invocationtargetexception;  import java.lang.reflect.method;  public class reflection {  public static void main(string[] args) throws nosuchmethodexception, securityexception, illegalaccessexception, illegalargumentexception, invocationtargetexception {     // todo auto-generated method stub     class xyclz = xy.class;     class zxclz = zx.class;      method method = xyclz.getmethod("print", string.class);      xy xy = new xy();     method.invoke(xy, "greetings xy");//this calls xy.print      method = zxclz.getmethod("print", string.class);      method.invoke(zx.class, "greetings zx");//this calls zx.print }   }      class xy{  public void print (string s){     system.out.println(s); }    }      class zx{  public static void print (string s){     system.out.println(s); }    } 

the output

  greetings xy   greetings zx 

Comments