【问题标题】:Use visibilityOfElementLocated with pagefactory将 visibilityOfElementLocated 与 pagefactory 一起使用
【发布时间】:2019-09-08 12:20:25
【问题描述】:

我正在使用页面工厂编写自动化脚本,我想将 visibilityOfElementLocated 与页面工厂一起使用而不是 visibilityOf 我尝试使用 visibilityOf,但有时它不适用于我的元素

visibilityOfElementLocated 采用 By 参数的问题,我有 WebElment

@FindBy(id = "test")
WebElement locator;

【问题讨论】:

  • 你为什么不使用visibilityOfjavadoc
  • 我想检查该元素是否存在于页面的 DOM 上并且可见。但是visibilityOf 方法只检查元素的可见性

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


【解决方案1】:

您不能直接将它与@FindBy 一起使用,但您可以从将在PageFactory.initElements 之前运行的方法中调用它

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

DerivedPage 中的assertInPage() 将在PageFactory.initElements 之前执行。

【讨论】:

    【解决方案2】:

    visibilityOfElementLocated(按定位器)

    visibilityOfElementLocated(By locator) 将 __By 定位器_ 作为参数,并期望检查元素是否存在于页面的 DOM 上并且可见。


    visibilityOf(WebElement 元素)

    visibilityOf(WebElement element)WebElement 作为参数,并期望检查已知存在于页面 DOM 上的元素是否可见。


    当在 PageObjectModel 中使用 PageFactory 时,如果您希望元素是动态的并通过某些 JavaScript 加载并且可能已经不在页面上,您可以使用Explicit Wait 对普通定位器工厂的支持如下:

    • 代码块:

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

    您可以在How to add explicit wait in PageFactory in PageObjectModel?找到详细讨论

    【讨论】:

    • 这个答案提供了 Guy 昨天没有提供的什么?
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 2017-06-21
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多