java.lang.ClassCastException: 在将 PageFactory 与 POM 一起使用时在 Web-DriverWait 中
java.lang.ClassCastException: In Web-DriverWait when using PageFactory with POM
我在 selenium 中使用页面工厂模型来初始化 Web 元素。
我的代码中有等待操作,在将 Web 元素传递给我的等待操作时,它抛出 "ClassCastException"。
我找不到解决方案,任何线索都会很棒。请建议我一些将页面工厂对象转换为 WebElement 对象的方法。
@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)
当变量 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。我会改为将定位器存储在 class 的顶部,然后根据需要使用它们。我会重写整个事情如下。
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();
}
我在 selenium 中使用页面工厂模型来初始化 Web 元素。 我的代码中有等待操作,在将 Web 元素传递给我的等待操作时,它抛出 "ClassCastException"。 我找不到解决方案,任何线索都会很棒。请建议我一些将页面工厂对象转换为 WebElement 对象的方法。
@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)
当变量 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。我会改为将定位器存储在 class 的顶部,然后根据需要使用它们。我会重写整个事情如下。
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();
}