0

I am using Page factory model in selenium to initialise the web elements. I have wait operations in my code, while passing the web element to my wait operations, it throws "ClassCastException". I am not able to find solution, any leads will be great. Please suggest me some ways to cast the Page factory' object to WebElement' object.

@FindBy(how = How.XPATH, using = "//*[@id='menu-posts']/div[3]/div/ul/li[3]/a") public WebElement categories;

    public void menus() {
            try {
                loginTest();
                menuPosts.click();
                waitClick((WebElement) categories);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                driver.quit();
            }
        }
    public void waitClick(WebElement element) {
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOfElementLocated((By) element));
        element.click();
    }


**Exception trace:**

    java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to org.openqa.selenium.By
        at com.pageObject.categories.waitClick(categories.java:75)
        at com.pageObject.categories.menus(categories.java:54)
        at SeleniumFramework.com.framework.AppTest.viewPost(AppTest.java:37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        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:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
3
  • What is element ? Commented Apr 10, 2017 at 3:03
  • You need to change wait.until(ExpectedConditions.visibilityOfElementLocated((By) element)); to wait.until(ExpectedConditions.visibilityOf(element)); Commented Apr 10, 2017 at 5:45
  • Thanks a lot. It helped. Commented Apr 10, 2017 at 13:41

1 Answer 1

1

You are casting the variable categories to a WebElement when it's already a WebElement.

Change this line

waitClick((WebElement) categories);

to this

waitClick(categories);

and it should get rid of the exception.

Another problem you are going to run into is that your waitClick() function is casting a WebElement into a By. You don't need the cast there. Better yet, you should be waiting for the element to be clickable before you click it. I would rewrite it like the below.

public void waitClick(WebElement element)
{
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element)).click();
}

Further more, the creator of Selenium, Simon Stewart, recommends not using Page Factory. I would instead store locators at the top of the class and then use them, as needed. I would rewrite the whole thing as below.

public By categoriesLocator = By.xpath("//*[@id='menu-posts']/div[3]/div/ul/li[3]/a");

public void menus()
{
    try
    {
        loginTest();
        menuPosts.click(); // why aren't you using waitClick() here?
        waitClick(categoriesLocator);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        driver.quit();
    }
}

public void waitClick(By locator)
{
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(locator)).click();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.