如何使用Reflection获取WebElement名称

时间:2017-12-26 14:36:59

标签: java selenium-webdriver reflection

最终目标是获取WebElement变量名称以用于报告目的。

我尝试过:

private String getVariableName(String callingClassName, WebElement element) {
    try {
        Class<?> cls = Class.forName(callingClassName);
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            if (field.getType() == WebElement.class) {
                field.setAccessible(true);
                WebElement fieldElement = WebElement.class.cast(field.get(cls));
                logger.debug("Field: \"" + field.getName() + "\""
                    + " Value: \"" + fieldElement.toString() + "\"\n"
                    + "Element: \"" + element.toString() + "\".");
                if (fieldElement.equals(element))
                    return field.getName();
            }
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } 
    return element.toString();
}

生成错误堆栈跟踪:

java.lang.IllegalArgumentException: Can not set org.openqa.selenium.WebElement field pages.google.GoogleHomePage.searchBox to java.lang.Class
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at pages.common.BasePageObject.getVariableName(BasePageObject.java:127)
at pages.common.BasePageObject.formatWord(BasePageObject.java:148)
at pages.common.BasePageObject.typeText(BasePageObject.java:327)
at pages.common.BasePageObject.typeText(BasePageObject.java:259)
at pages.google.GoogleHomePage.performGoogleSearch(GoogleHomePage.java:42)
at testcases.google.GoogleSearchTest.performSearch(GoogleSearchTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

字段&#39; callingClassName and 'element是参数,element是给定的WebElement和&#39; callingClassName is just that and based off of 'element

我尝试了WebElement fieldElement = (WebElement)field.get(cls)WebElement fieldElement = (WebElement)field,两者都给出了类似的错误。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

请看下面的代码,希望这对您有所帮助:

public WebElement getWebElementByObjectAndName(Object object, String fieldName) {
    WebElement webElement = null;
    Class<?> validationClass = object.getClass();
    Field[] fields = validationClass.getDeclaredFields();
    for (Field field : fields) {
        if (field.getType() == WebElement.class) {
            field.setAccessible(true);
            if (field.getName().equals(fieldName)) {
                try {
                    webElement = (WebElement) field.get(object);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }

        }
    }
    return webElement;