一千萬個為什麽

搜索

硒,規格和斯卡拉

我正在尋找一個信息或一些項目用於scala規範的selenium測試。 如何從我的scala規範代碼中調用selenium測試?

最佳答案

我剛寫了一些可能有幫助的代碼。這還沒有使用任何scala DSL用於硒。

class EnrollTest extends FlatSpec with ShouldMatchers{

  var driver = new FirefoxDriver
  driver.manage.timeouts.implicitlyWait(10, TimeUnit.SECONDS)


  val beginnersPage = BeginnersPage.open(driver)

  "Enroll button" should "open enroll page" in {
    try {
      val timeOfFirstCourse = beginnersPage.enrollForTheFirstCourse
      val enrollPage = new EnrollPage(driver)

      enrollPage.shouldBeOpen
      enrollPage.courseDetails should include(timeOfFirstCourse)
    }
    finally{
      driver.quit
    }
  }

}

和頁面類:

object BeginnersPage{
  val url = "http://"+System.getProperty("testUrl", "www.2ndlanguage.co.uk")+"/beginners.action"

  def open(driver : WebDriver) : BeginnersPage = {
    driver.get(url)
    new BeginnersPage(driver)
  }

}

class BeginnersPage(driver : WebDriver){
  private var firstEnrollButton: WebElement = driver.findElement(By.xpath("//div[@class='newPriceBoxRight']/a/div"))
  private var firstCourseTime: WebElement = driver.findElement(By.xpath("//div[@class='newPriceBoxLeft']//span[@id='time']"))

  def enrollForTheFirstCourse : String = {
    val time = firstCourseTime.getText
    firstEnrollButton.click
    time
  }
}

class EnrollPage(driver : WebDriver){
  def isOpen = driver.getCurrentUrl startsWith "http://www.2ndlanguage.co.uk/offer.action"
  def courseDetails = driver.findElement(By.id("courseDetailsTextArea")).getText
}

轉載註明原文: 硒,規格和斯卡拉