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)
答案 0 :(得分:1)
当变量categories
已经是WebElement
时,您正在将其变为WebElement
。
更改此行
waitClick((WebElement) categories);
到这个
waitClick(categories);
它应该摆脱异常。
您将遇到的另一个问题是,waitClick()
函数正在向WebElement
投放By
。你不需要那里的演员阵容。更好的是,您应该在单击之前等待元素可点击。我会像下面一样重写它。
public void waitClick(WebElement element)
{
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element)).click();
}
此外,Selenium的创造者,Simon Stewart,recommends not using Page Factory。我会将定位器存储在类的顶部,然后根据需要使用它们。我会重写整个事情如下。
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();
}