我如何从主类java调用另一个类中的方法

时间:2016-04-20 19:57:17

标签: java excel selenium methods invoke

我正在使用Selenium而且我是Java的新手。我有三个类ATSmoke(),它是主类。我在Excel工作表中的所有方法名称都在另外两个类Profile()和Schedule()中。现在我使用POI库来获取单元格值(即方法名称)。在这里,我遇到了如何在另一个类Profile()中调用这些方法(edit_contact_info)。如果他们在同一个班级,我可以使用相同的班级名称来引用。但不能为另一个班级做。还有另一个名为ATTestDriver的类,我有所有实用工具方法,比如选择webdriver,浏览器等。

公共类ATSmoke {

public static void main(String[] args){
    Profile profileDriver = new Profile(Browsers.CHROME);
    XSSFWorkbook srcBook = null;
    try {
        srcBook = new XSSFWorkbook("./TestData/Testcase_data_v1.xlsx");
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    XSSFSheet sourceSheet = srcBook.getSheet("Testcases");
    int rowCount = sourceSheet.getLastRowNum();
    for (int i=1; i<=rowCount; i++){
        int rownum=i;
            XSSFRow testcaserow=sourceSheet.getRow(rownum);
            XSSFCell testcase_Name= testcaserow.getCell(1);
            String flagState=testcaserow.getCell(2).getStringCellValue();
        if (flagState.equals("yes")) {

        if (testcase_Name != null) {
            try {
                Method myMethod = ATSmoke.class.getMethod(testcase_Name.getStringCellValue());
                myMethod.invoke(new ATSmoke());
            } catch (NoSuchMethodException | SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("");
        } 
    }
    }
    try {
        srcBook.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public class Profile扩展ATTestDriver {

public Profile(Browsers browser) {
    super(browser);
}

public void edit_contact_info() {
    WebElement pageopened =this.waitForElement(By.cssSelector(".qualifications p b b"));
    System.out.println("you have " +pageopened.getText());

    driver.findElement(By.cssSelector("contact-information button")).click();

    }

}

2 个答案:

答案 0 :(得分:0)

要在另一个类中调用方法,首先必须实例化它:

Collectors.counting()

或者在您的具体案例中:

MyClass myClass = new MyClass();
myclass.mymethod();

答案 1 :(得分:0)

您可以使用java反射动态执行该方法。

try {
    Class<?> c = Class.forName(args[0]);
    Object t = c.newInstance();

    Method[] allMethods = c.getDeclaredMethods();
    for (Method m : allMethods) {
    String mname = m.getName();
    if (!mname.startsWith("test")
        || (m.getGenericReturnType() != boolean.class)) {
        continue;
    }
    Type[] pType = m.getGenericParameterTypes();
    if ((pType.length != 1)
        || Locale.class.isAssignableFrom(pType[0].getClass())) {
        continue;
    }

    out.format("invoking %s()%n", mname);
    try {
        m.setAccessible(true);
        Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));
        out.format("%s() returned %b%n", mname, (Boolean) o);

    // Handle any exceptions thrown by method to be invoked.
    } catch (InvocationTargetException x) {
        Throwable cause = x.getCause();
        err.format("invocation of %s failed: %s%n",
               mname, cause.getMessage());
    }
    }

    // production code should handle these exceptions more gracefully
} catch (ClassNotFoundException x) {
    x.printStackTrace();
} catch (InstantiationException x) {
    x.printStackTrace();
} catch (IllegalAccessException x) {
    x.printStackTrace();
}

资料来源:[https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html][1]

相关问题