selenium 如何将WebDriverWait用于Selify JUnit中的各种方法?

4ioopgfo  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(122)

我的代码中有多种方法需要使用WebDriverWait。通常我们在main方法中声明它,如下所示:

WebDriverWait mywait = new WebDriverWait(driver, 25)

在Selify中使用JUnit时,没有任何Main方法。如何在代码中的各种方法中使用WebDriverWait for Webelements?

t5zmwmid

t5zmwmid1#

从Main调用JUnit函数的方式

以下是关于如何:*How do I run JUnit tests from inside my Java application?*的一些帮助

也可以简单添加Thread.sleep(毫秒)

try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

它必须在Java(Eclipse)中工作。
但您也可以使用(我认为它更适合测试):

FluentWait fluentWait = new FluentWait<>(webDriver) {
  .withTimeout(30, TimeUnit.SECONDS)
  .pollingEvery(200, TimeUnit.MILLISECONDS);
  .ignoring(NoSuchElementException.class);
}

WebElement element = (new WebDriverWait(driver, 10))
   .until(ExpectedConditions.elementToBeClickable(By.id("someid")));

来源:如何让它等到元素出现在 selenium 中?

相关问题