Behavioural Driven Testing using Selenium and Scalatest

Hi All, wish you a very happy new year. It’s been a long time since my last post and this is the first one of 2013. I had been doing some interesting stuff over past few weeks on Behaviour Driven Automation testing and will share with a simple example on how to do the same with selenium. The catch here is with the Java library.

First let me explain what behavioural testing is. It is something like you write a story for a feature and based on the story you do your testing. You construct your stories on how the system would behave. Let’s consider http://www.google.com. If we had to write a story for it’s search functionality, the story in most of the cases would be as below.

“A person open google home page by typing the URL as http://www.google.com in the browser. Then he enters the search keyword (let’s assume ‘selenium’) and submits the page. Now something happens inside the system and the user is presented with links related to the keyword selenium”

Now let’s break it down to a behavioural specification. For the example taken, the specification may look as below.

scenario("A person searches for a keyword Selenium in Google") {
    given("A user is in google home page")
    open "http://www.google.com/"

    when("User does a search for the keyword Selenium")
    find "Search box"
    enter "Selenium"
    perform "Submit"

    then("Selenium link is listed in the search result")
  }

Now the question is how can we test this scenario with the help of selenium. This can be achieved through an opensource framework called ScalaTest. Below is an example code for achieving the same using ScalaTest and seleniumimport org.scalatest.FeatureSpec

import org.scalatest.GivenWhenThen
import org.openqa.selenium._
import org.openqa.selenium.firefox._
import java.util.concurrent.TimeUnit

class FirstScalaTest extends FeatureSpec with GivenWhenThen{
  private val driver = new FirefoxDriver
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)
  scenario("A person searches for a keyword Selenium in Google") {
    given("A user is in google home page")
    driver.get("http://www.google.com/")

    when("User does a search for the keyword Selenium")
    val inputElement = driver.findElement(By.name("q"))
    inputElement.sendKeys("Selenium")
    val goButton = driver.findElement(By.id("gbqfb"))
    goButton.click()

    then("Selenium link is listed in the search result")
    assert(driver.findElementByPartialLinkText("Selenium").isDisplayed())
  }

}

When you run this class using sbt, your test result is published as a story. For example for the test given above the output would be as below.

Screen Shot 2013-01-05 at 7.32.07 PM

Behavioural driven technique is great way to do your acceptance testing. Keep testing, keep rocking 🙂

2 thoughts on “Behavioural Driven Testing using Selenium and Scalatest

  1. I was just wondering why not just echo it? like

    System.out.println(“scenario(“A person searches for a keyword Selenium in Google”)
    System.out.println(” given(“A user is in google home page”)”);

Leave a reply to Andrey Cancel reply