lundi 4 avril 2016

How to cast java.lang.reflect.Field to org.openqa.selenium.WebElement to use for Keyword Driven in Page Factory Framework

A snippet of the LoginPage -

Class LoginPage {
public LoginPage() throws Exception {
    eDriver = BrowserDriver.getDriverInstance();
    PageFactory.initElements(eDriver, this);
}
@FindBy(name = "rememberMe")
public WebElement rememberMe_checkbox;

}

The current framework is using page factory and page object. But, I also want to be keyword driven.

The keywords in the excel file is defined as follows -

    class       element_name            action
    LoginPage   rememberMe_checkbox     click

In the keyword action class, I am able to get the LoginPage class object. I can get 'rememberMe_checkbox' WebElement field. But I couldn't get it to cast to WebElement.

I'm getting this error:

java.lang.ClassCastException: Cannot cast java.lang.reflect.Field to org.openqa.selenium.WebElement

Here is what I have so far -

public class ActionKeywords {
public void enter_Text(String className, String elementName, String textValue) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException {
ClassLoader classLoader = ActionKeywords.class.getClassLoader();
Class<?> containerClass = classLoader.loadClass(className);
Object theElm = containerClass.getDeclaredField(elementName);
WebElement theWebElm = WebElement.class.cast(theElm);  <-- Error

theElm.getType() => interface org.openqa.selenium.WebElement
theElm.getClass() => class java.lang.reflect.Field

How can I cast theElm to WebElement so I can just do the following as you would in the page factory?

theWebElm.sendKeys(textValue);





Aucun commentaire:

Enregistrer un commentaire