Wednesday, 27 July 2016

Selenium and Scala Test


Scala Test contains brilliant integration with Selenium to help write BDD tests. The DSL provided by Scala is easy to read, even for a non programmer. I have created a template project to get you started writing automated browser tests with scalatest, selenium and maven.

The code can be checked out from:  https://github.com/rizvn/Scalatest-Selenium

To run all the tests simply run (uses mac osx chrome driver, change for your os, link in ReadMe):
 mvn test

Below is one of the example spec (aka test) that automates a google search to get to this site. The full code is in under src/test/java/web/ExampleTest.scala

"Google search for blog.rizvn.com" should "display blog page as first link" in {
    //1. go to page
    go to ("https://www.google.co.uk")

    //2. fill search field name is q
    textField("q").value = "blog.rizvn.com"

    //3. submit form by pressing enter
    pressKeys(Keys.ENTER.toString)

    //4. repeately check for up to 10 seconds for the ajax results to be returned
    eventually(timeout = timeout(Span(10, Seconds))){

      //click on link that has text Code and Stuff on the results page
      click on partialLinkText("Code and Stuff")
    }

    //assert the the title of the page is Code and Stuff
    pageTitle should be ("Code and Stuff")
  }

No comments:

Post a Comment