一尘不染

如何在PageObjectModel的PageFactory中添加显式等待?

selenium

thread.sleep()在下面的代码中添加了硬代码等待。如何使用显式等待。我想等到“用户名”
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();
}

}


阅读 349

收藏
2020-06-26

共1个答案

一尘不染

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;
        }

    }

您可以在如何对PageFactory字段和PageObject模式使用显式等待中找到详细的讨论。

2020-06-26