使用@FindBy时如何实现'wait'函数?



当我使用页面对象模型时,我使用@FindBy方法,但我如何实现以下语句?

wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Logout")))

您可以使用ExpectedConditions.elementToBeClickable来实现:-

@FindBy(linkText = "Logout")
WebElement logOut;
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(logOut));

可以设置每个页面的等待时间

PageFactory.initElements(new AjaxElementLocatorFactory(driver, TIMEOUT), this);

你可以创建这样一个方法:

private void waitForVisibility(WebElement element) throws Error{
       new WebDriverWait(driver, 30)
            .until(ExpectedConditions.visibilityOf(element));
}

然后在需要时调用该方法:

waitForVisibility(element);//find by element

似乎到目前为止,大多数解决方案都是不完整的,因为您不能使用ExpectedConditions类提供的整个static方法。

一个开始说明和一些额外的信息:示例是c#的,因为我不知道Java语法,但我希望它是相同的。

  • [FindsBy]属性正在获得参数2值。第一个是指定如何查找元素(id、xpath、css选择器等)的Enum,第二个(对于本例来说更重要)是接收常量字符串的Usingconst字符串用于指定实际元素idxpath等。
  • 大多数ExpectedConditions静态方法作为参数接收By元素

记住上面的语句,接下来我去确定页面上需要使用显式等待的元素并构建我的代码:

const string loginButtonXpath = "xpath_here";
[FindsBy(How = How.Xpath, Using=loginButtonXpath)]
private IWebElement LoginButton {get;set;}
private By LoginButtonBy
{
    return By.Xpath(loginButtonXpath);
}

以这种方式设置,我有IWebElement按钮,我需要点击,我有By定位器,我可以在显式等待中使用,我也有方法在一个地方识别页面上的元素,当发生任何变化时,像以前一样容易维护我的自动化解决方案。

我很好奇其他人是如何在不丢失内置显式等待方法的情况下处理这个问题的。

最新更新