Use visibilityOfElementLocated with pagefactory
我正在使用页面工厂编写自动化脚本,我想将 visibilityOfElementLocated 与页面工厂一起使用,而不是使用 visibilityOf
我尝试使用 visibilityOf 但有时它不适用于我的元素
visibilityOfElementLocated 采用 By 参数的问题,我有 WebElment
1 2 | @FindBy(id ="test") WebElement locator; |
您不能直接将它与
之前运行的方法中调用它
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public abstract class BasePage { protected WebDriverWait wait; public BasePage(WebDriver driver) { wait = new WebDriverWait(driver, 10); assertInPage(); PageFactory.initElements(driver, this); } public abstract void assertInPage(); } public class DerivedPage extends BasePage { @FindBy(id ="test") WebElement locator; public DerivedPage(WebDriver driver) { super(driver); } @Override public void assertInPage() { wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("test"))); } } |
之前执行
visibilityOfElementLocated(按定位器)
可见性(WebElement 元素)
当在 PageObjectModel 中使用 PageFactory 时,如果您希望元素是动态的并通过一些 JavaScript 加载并且可能已经不存在于页面上,您可以将显式等待支持与普通定位器工厂一起使用,如下所示:
-
代码块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35import org.openqa.selenium.By;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestPage {
WebDriver driver;
//constructor
public TestPage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
//Locator Strategy
@FindBy(id ="test")
WebElement locator;
//Test Method
public void testMethod()
{
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(TestPage.getWebElement()));
//perform your desired action here e.g element.click()
}
public WebElement getWebElement()
{
return locator;
}
}
You can find a detailed discussion in How to add explicit wait in PageFactory in PageObjectModel?