【问题标题】:How to add explicit wait in PageFactory in PageObjectModel?如何在 PageObjectModel 的 PageFactory 中添加显式等待?
【发布时间】:2019-01-12 09:13:19
【问题描述】:

我在下面的代码中添加了硬代码等待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();
}

}

【问题讨论】:

    标签: selenium selenium-webdriver pageobjects webdriverwait page-factory


    【解决方案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;
          }
      
      }
      

    您可以在How to use explicit waits with PageFactory fields and the PageObject pattern找到详细讨论

    【讨论】:

      【解决方案2】:

      你有两个选择:

      1- 您可以在初始化驱动程序时使用隐式等待。

      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      

      2- 仅对用户名字段使用显式等待:

      WebDriverWait wait = new WebDriverWait(driver,30);
      WebElement element = wait.until(
                          ExpectedConditions.visibilityOf(By.id(identifier)));
      

      【讨论】:

      • WebElement element = wait.until( ExpectedConditions.visibilityOf(By.id(identifier)));这不是在 pageobject 模型中的做法。它会抛出错误
      • 什么错误?或者你也可以使用ExpectedConditions.elementToBeClickable(email)
      猜你喜欢
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      • 2022-11-28
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      相关资源
      最近更新 更多