WebDriver在Scala中显式等待

时间:2011-08-18 05:46:06

标签: scala selenium webdriver

如何在Scala中编写Java explicit wait example here

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
      return d.findElement(By.id("myDynamicElement"));
    }});

2 个答案:

答案 0 :(得分:6)

看起来非常相似。某些类型和括号可以省略。但是只要没有使用某些Scala功能的特殊Scala API,它就会看起来像这样(也许更多的点也可以被调用):

val driver: WebDriver = new FireFoxDriver
driver.get("http://somedomain/url_that_delays_loading")
val myDynamicElement = new WebdriverWait(driver, 10).until(
  new ExpectedCondition[WebElement] {
    override def apply(d: WebDriver) = d.findElement(By.id("myDynamicElement"))
  })

答案 1 :(得分:0)

我有一个帮助类,用于Scala中更短更干净的代码:

case class SeleniumHelper(timeOut: Int) {
  def waitFor(driver: WebDriver, f: (WebDriver) => WebElement) : WebElement = {
    new WebDriverWait(driver, timeOut).until(
     new ExpectedCondition[WebElement] {
      override def apply(d: WebDriver) = f(d)
    })
  }

  def test() = {
  val driver: WebDriver = new FirefoxDriver
  driver.get("http://www.abksystem.com")
  waitFor(driver, _.findElement(By.className("module module-Menu")))
  }
}