如何解决等待直到使用PageFactory
我试图在项目中仅使用PageFactory,而不使用By类型的字段。而且我正在寻求实现这样的东西:
@FindBy(className = "loading-container")
private WebElement loadingElement;
public LoadingPage(WebDriver driver) {
PageFactory.initElements(driver,this);
this.waitDriver = new webdriverwait(this.driver,20);
}
public void waitLoadingToFinish() {
this.waitDriver.until(ExpectedConditions.elementNotExists(loadingElement));
}
是否有一种方法可以为此实现自定义预期条件?或任何其他方式来实现这一目标? (不使用“按字段”,仅使用页面工厂)。
解决方法
据我了解,您只有在页面上没有某些元素(例如等待轮)时,才认为页面上有一些元素可供使用。
Selenium中有一个特殊的定位器类,称为AjaxElementLocator
。您需要做的是通过在初始化页面时更改isElementUsable
方法来扩展该类型,以便该页面上使用的所有控件都将首先检查条件。这是示例:
package click.webelement.pagefactory.conditions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocator;
public class LoadablePage {
@FindBy(id = "cntrl-id")
WebElement control;
public LoadablePage(WebDriver driver){
PageFactory.initElements(field -> new AjaxElementLocator(driver,field,10){
@Override
protected boolean isElementUsable(WebElement element) {
return driver.findElements(By.xpath("//WHEEL_XPATH")).size() == 0;
}
},this);
}
}
您可以在此找到more information。
,硒有这种方法
ExpectedConditions.InvisibilityOfElementLocated
期望检查元素是否不可见 出现在DOM上。
输入您的代码
public void waitLoadingToFinish() {
this.waitDriver.until(ExpectedConditions.invisibilityOfElementLocated(loadingElement));
}
另外,您可能会尝试添加javascript执行程序以等待页面加载完毕
public static void waitForScriptsToLoad(WebDriver driver) {
WebDriverWait(driver,30).until((ExpectedCondition<Boolean>) wd ->
((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
}
然后您的页面构造函数就变成了
public LoadingPage(WebDriver driver) {
PageFactory.initElements(driver,this);
this.waitDriver = new WebDriverWait(this.driver,20);
waitForScriptsToLoad(driver);
}
,
If you create wait method in your program that is easy and customize in selenium framework
private static WebElement waitForElement(By locator,int timeout)
{
WebElement element=new WebDriverWait(driver,timeout).until(ExpectedConditions.presenceOfElementLocated(locator));
return element;
}
//If you want wait for id following code should use
waitForElement(By.id(""),20);
Here 20 is miliseconds
,您可以使用任何网络元素等待
,要模拟elementNotExists
之类的ExpectedConditions,可以在invisibilityOfElementLocated()
或invisibilityOf()
中使用。
invisibilityOfElementLocated()
invisibilityOfElementLocated()是一种期望的实现,用于检查元素在DOM上是否不可见或不存在。定义如下:
public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated(By locator)
An expectation for checking that an element is either invisible or not present on the DOM.
Parameters:
locator - used to find the element
Returns:
true if the element is not displayed or the element doesn't exist or stale element
-
代码块:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.WebDriverWait; public class fooPage { WebDriver driver; public fooPage(WebDriver driver) { PageFactory.initElements(driver,this); } //you don't need this //@FindBy(className = "loading-container") //private WebElement loadingElement; public void foo() { Boolean bool = new WebDriverWait(driver,20).until(ExpectedConditions.invisibilityOfElementLocated(By.className('loading-container'))); //other lines of code } }
您也可以使用invisibilityOf()
方法,如下所示:
invisibilityOf()
invisibilityOf()是用于期望检查元素不可见的实现。定义如下:
public static ExpectedCondition<java.lang.Boolean> invisibilityOf(WebElement element)
An expectation for checking the element to be invisible
Parameters:
element - used to check its invisibility
Returns:
Boolean true when elements is not visible anymore
-
代码块:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.ui.WebDriverWait; public class fooPage { WebDriver driver; public fooPage(WebDriver driver) { PageFactory.initElements(driver,this); } @FindBy(className= 'loading-container') WebElement loadingElement; public void foo() { Boolean bool = new WebDriverWait(driver,20).until(ExpectedConditions.invisibilityOf(fooPage.getWebElement())); //other lines of code } public WebElement getWebElement() { return loadingElement; } }
您可以在How to use explicit waits with PageFactory fields and the PageObject pattern
中找到详细的讨论
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。