如何在 PageObjectModel 的 PageFactory 中添加显式等待?
How to add explicit wait in PageFactory in PageObjectModel?
我在下面的代码中添加了硬编码等待 thread.sleep()
。如何使用显式等待。我想等 "username" WebElement 出现。我的程序运行良好。我已经写过测试用例了。
package com.pol.zoho.PageObjects;
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 ZohoLoginPage {
WebDriver driver;
public ZohoLoginPage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
@FindBy(xpath=".//*[@id='lid']")
public WebElement email;
@FindBy(xpath=".//*[@id='pwd']")
public WebElement password;
@FindBy(xpath="//*[@id='signin_submit']")
public WebElement signin;
public void doLogin(String username,String userpassword)
{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
email.sendKeys(username);
password.sendKeys(userpassword);
signin.click();
}
}
你有两个选择:
1- 您可以在初始化驱动程序时使用隐式等待。
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
2- 仅使用显式等待用户名字段:
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(
ExpectedConditions.visibilityOf(By.id(identifier)));
在 PageObjectModel 中使用 PageFactory 时,如果您希望通过某些 JavaScript 加载元素,但它可能不是已经出现在页面上,您可以使用 Explicit Wait 支持和普通定位器工厂,如下所示:
代码块:
package com.pol.zoho.PageObjects;
import 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 ZohoLoginPage {
WebDriver driver;
public ZohoLoginPage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
@FindBy(xpath=".//*[@id='lid']")
public WebElement email;
@FindBy(xpath=".//*[@id='pwd']")
public WebElement password;
@FindBy(xpath="//*[@id='signin_submit']")
public WebElement signin;
public void doLogin(String username,String userpassword)
{
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(ZohoLoginPage.getWebElement()));
email.sendKeys(username);
password.sendKeys(userpassword);
signin.click();
}
public WebElement getWebElement()
{
return email;
}
}
您可以在 How to use explicit waits with PageFactory fields and the PageObject pattern
中找到详细的讨论
我在下面的代码中添加了硬编码等待 thread.sleep()
。如何使用显式等待。我想等 "username" WebElement 出现。我的程序运行良好。我已经写过测试用例了。
package com.pol.zoho.PageObjects;
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 ZohoLoginPage {
WebDriver driver;
public ZohoLoginPage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
@FindBy(xpath=".//*[@id='lid']")
public WebElement email;
@FindBy(xpath=".//*[@id='pwd']")
public WebElement password;
@FindBy(xpath="//*[@id='signin_submit']")
public WebElement signin;
public void doLogin(String username,String userpassword)
{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
email.sendKeys(username);
password.sendKeys(userpassword);
signin.click();
}
}
你有两个选择:
1- 您可以在初始化驱动程序时使用隐式等待。
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
2- 仅使用显式等待用户名字段:
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(
ExpectedConditions.visibilityOf(By.id(identifier)));
在 PageObjectModel 中使用 PageFactory 时,如果您希望通过某些 JavaScript 加载元素,但它可能不是已经出现在页面上,您可以使用 Explicit Wait 支持和普通定位器工厂,如下所示:
代码块:
package com.pol.zoho.PageObjects; import 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 ZohoLoginPage { WebDriver driver; public ZohoLoginPage(WebDriver driver) { PageFactory.initElements(driver, this); } @FindBy(xpath=".//*[@id='lid']") public WebElement email; @FindBy(xpath=".//*[@id='pwd']") public WebElement password; @FindBy(xpath="//*[@id='signin_submit']") public WebElement signin; public void doLogin(String username,String userpassword) { WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(ZohoLoginPage.getWebElement())); email.sendKeys(username); password.sendKeys(userpassword); signin.click(); } public WebElement getWebElement() { return email; } }
您可以在 How to use explicit waits with PageFactory fields and the PageObject pattern
中找到详细的讨论