java.lang.ClassCastException: In Web-DriverWait when using PageFactory with POM

元气小坏坏 提交于 2019-12-12 02:24:47

问题


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)

回答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();
}


来源:https://stackoverflow.com/questions/43314243/java-lang-classcastexception-in-web-driverwait-when-using-pagefactory-with-pom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!