2

A snippet of the LoginPage -

Class LoginPage extends BaseClass {
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);
Chris
  • 21
  • 3
  • Why won't you pass the instance of class itself rather than just a string ( `String className`) to your `enter_text` function? You can create a parent class that all your pages will inherit from and then you can pass the class like `enterText(? extends ParentClass, ...)` – Eugene S Apr 05 '16 at 05:57
  • @Eugene S - Thank you for your response! Sorry I failed to include in my original code that LoginPage is extended from a BaseClass. But that class only has the WebDriver and test environment setup. Can you elaborate on how it would work? Thanks. – Chris Apr 05 '16 at 15:34

0 Answers0