![](/img/trans.png)
[英]how to give wait to driver.get() explicitly in selenium using java
[英]How to explicitly wait while using page factory in Selenium?
我將在Selenium組織一次明確的等待,如下所示:
WebDriverWait = new WebDriverWait(driver,30);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(locator));
問題是我的類中沒有驅動程序,因為我使用了PageFactory,而不是測試類中的構造函數:
MyClass myform = PageFactory.InitElements(driver, MyClass.class)
在這種情況下,組織明確等待的好決定是什么?
我建議您按預期使用PageFactory,並為您的類提供一個構造函數,您希望使用顯式等待。 在腳本和頁面對象之間進行分離可以使將來更容易使用。
public class MyClass {
WebDriverWait wait;
WebDriver driver;
@FindBy(how=How.ID, id="locatorId")
WebElement locator;
// Construct your class here
public MyClass(WebDriver driver){
this.driver = driver;
wait = new WebDriverWait(driver,30);
}
// Call whatever function you want to create
public void MyFunction(){
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
// Perform desired actions that you wanted to do in myClass
}
然后在您的測試用例中使用代碼來執行測試。 在您的示例中,等待包含在頁面內。
public class MyTestClass {
public static void main (string ... args){
WebDriver driver = new FireFoxDriver();
MyClass myForm = PageFactory.initElements(driver,Myclass.class);
myForm.MyFunction();
}
}
這個例子是按照Selenium WebDriver實用指南中的例子建模的,可以在這里找到
我認為如果您從其調用者Test類中傳遞頁面類中的驅動程序,則會有更好的解決方案。 請參閱以下實施以獲得更清晰。
頁面類:
public class YourTestPage {
private WebDriver driver;
private WebDriverWait wait;
@FindBy(xpath = "//textarea")
private WebElement authorField;
public YourTestPage(WebDriver driver) {
this.driver = driver;
wait = new WebDriverWait(driver, 15, 50);
PageFactory.initElements(driver,this);
}
public String getAuthorName() {
wait.until(ExpectedConditions.visibilityOf(authorField)).getText();
}
}
測試類:
public class YourTest{
private YourTestPage yourTestPage;
private WebDriver driver;
@BeforeTest
public void setup() throws IOException {
driver = WebDriverFactory.getDriver("chrome");
yourTestPage = new YourTestPage(driver);
}
@Test
private void validateAuthorName() {
Assert.assertEquals(yourTestPage.getAuthorName(),"Author Name");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.