mercredi 25 mai 2016

Java Reflection error trying to convert Field into WebElement

I'm struggling with i believe is a reflection problem.

Here is the thing, On my automation project I'm trying to create a helper class to validate Page Labels from my web application, to that i create a @annotation which contain just a label field.

It looks like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ExpectedLabel{
    String label() default "";
}

At my Page Class i have the field:

@ExpectedLabel(label="Faça seu Login")
@FindBy(how = How.XPATH, using = "//*[@id='login']/form/h3")
private WebElement facaSeuLoginLabel;

And here is my helper class method to validate labels:

@SuppressWarnings("rawtypes")
public boolean validateLabels(Class validationClass){
    for (Field field: validationClass.getDeclaredFields()) {
        if(field.getType() ==  WebElement.class && field.isAnnotationPresent(ExpectedLabel.class)){
            field.setAccessible(true);
            ExpectedLabel expectedLabel = field.getAnnotation(ExpectedLabel.class);
            if (StringUtils.isNotBlank(expectedLabel.label())){
                try {
                    WebElement element = (WebElement) field.get(WebElement.class);
                    if(!StringUtils.equals(element.getText(), expectedLabel.label())){
                        LoggerUtil.warn("O campo "+field.getName()+" da Page "+validationClass.getSimpleName()+" tem um label invalido! Esperado : ("+expectedLabel.label()+") Obtido: ("+element.getText()+")!", validationClass.getSimpleName());
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return true;
}

The problem is, i have to call WebElement.getText() to have acess to the Real Page label and then compare to that one in the annotation @ExpectedLabel.label. Whatever I tried, just cant convert field into WebElement.

I already tried to create an implementation of WebElement called WebElementImpl and done the conversion, but still doesn't worked. Other approach that doesn't worked was trying to call the getText() method directly from Field, getting his public methods, but i don't think that is possible.

Here is the exception i get when tried to convert the objects:

java.lang.IllegalArgumentException: Can not set org.openqa.selenium.WebElement field com.org.pages.LoginPage.facaSeuLoginLabel to java.lang.Class
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:379)
    at com.org.helpers.LabelValidatorUtil.validateLabels(LabelValidatorUtil.java:20)
    at com.org.telas.LoginPageTest.loginPageValidationTest(LoginPageTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)

Obs: WebElement is an interface, so how can i make the conversion? New approaches to solve the problem are welcome.





Aucun commentaire:

Enregistrer un commentaire