mardi 26 décembre 2017

How to get the WebElement Name Using Reflection

The ultimate goal is to get the WebElement variable name for reporting purposes.

To which, I tried:

 private String getVariableName(String callingClassName, WebElement element) {
    try {
        Class<?> cls = Class.forName(this.getClass().getCanonicalName());
        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();
}

Which generates the error stack trace:

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)

The fields 'callingClassNameand 'element are parameters, the element is the given WebElement and the 'callingClassNameis just that and based off of 'element.

I have tried 'WebElement fieldElement = (WebElement)field.get(cls)andWebElement fieldElement = (WebElement)field`, both give similar errors.

Any help is appreciated.

Thank you





Aucun commentaire:

Enregistrer un commentaire