Behavior Driven Development with easyB, Gradle and Selenium

As automated testing has became one of the central process in Agile application development, developers and testers realized than automated test are not only tests but also executable behavior of the systems expressed via test cases. This realization has created a new approach to testing and development – behavior driven development. This is not a new or revolutionary idea. It continuation of test-Driven development practice, I would say evolutional progress in TDD. Tests should be written in a form of acceptance criteria, or customer expectation that get executed directly against application. It solve one important problem – documented specification get out of date quite quickly as system evolved. It is hard to keep all changes in a separate documents. This is not possible for executable specifications: If they don’t specify what the application does accurately, they will raise an exception to that effect when run.
Specification often written in domain specifil languages, DSL and support concepts of stories and scenarios.
This is perfect for writing acceptance and even smoke tests, because they are business facing, which means they should verify that your application delivers value to its users. Those test should be called from deployment pipeline during acceptance testing phase as well as smoke testing phase. Analysts define acceptance criteria for stories — criteria that must be fulfilled for the story to be recognized as done. Usually stories are written in given, when, then manner.

  • Given some initial context
  • When an event occurs
  • Then there are some outcomes.

In terms of your application,

  • “given” represents the state of your application at the beginning of the test case.
  • The “when” clause describes an interaction between a user and your application.
  • The “then” clause describes the state of the application after this interaction has completed.

The job of your test case is to get the application into the state described in the “given” clause, perform the actions described in the “when” clause, and verify that the application’s state is as described in the “then” clause.
There are many tools that support this notion – Cucumber, JBehave, Concordion, Twist, easyB and FitNesse.
It this post I explore easyB to create acceptance test using easyB, Groovy and Selenium.
Let say our application, bing search engine should satisfy following business rules, written by our system analyst.

description "This story is about keyword search on Bing search engine"
scenario "search on bing for picasso "
 given "use arrived at bing home page"  
when "user type in search text box term 'picasso' and submit" 
then "search result should contain Pablo Picasso - Wikipedia, the free encyclopedia on page"

Step 1. Configure gradle and groovy on your system.
Step 2. Create a project according to maven project layout.

Step 3. In gradle.build file create dependency for easyB.
apply plugin: 'java'
dependencies {
    testRuntime("org.easyb:easyb-core:1.4") { transitive = true }
    testRuntime("org.seleniumhq.selenium:selenium-java:2.13.0")

}

repositories {
    mavenCentral()
}

check << {

    ant.taskdef(name: "easyb", classname:"org.easyb.ant.BehaviorRunnerTask", classpath: sourceSets.test.runtimeClasspath.asPath)

    ant.easyb( classpath: sourceSets.test.runtimeClasspath.asPath, failureProperty:'easyb_failed' ) {
        report( location:"${project.testResultsDir}/story.txt", format:"txtstory" )
        behaviors( dir: "src/test/stories" ) {
            include( name:"**/*.story" )
        }
   }

    ant.fail( if:'easyb_failed', message: 'Failures in easyb stories' )
}

Step 4. create an executable specification for our story using groovy and selenium webDriver.
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.By
import org.openqa.selenium.WebElement


def driver
description "This story is about keyword search on Bing search engine"

before "start selenium", {
 given "selenium is up and running", {
   // The Firefox driver supports javascript
  driver = new FirefoxDriver()

 }
}

scenario "search on bing for picasso ", {
 given "use arrived at bing home page"  , {
     driver.get("http://www.bing.com/")
 }
 when "user type in search text box term 'picasso' and submit" ,{
      // get input text element on the page
      element=driver.findElement(By.name("q"))
      element.sendKeys("picasso")
      element_go=driver.findElement(By.name("go"))
      element_go.click()

 }

 then "search result should contain Pablo Picasso - Wikipedia, the free encyclopedia on page", {
     

     // use xpath to search for wikipedia link

     WebElement link = driver.findElement(By.partialLinkText("Wikipedia, the free encyclopedia") )
     link.text.shouldBe "Pablo Picasso - Wikipedia, the free encyclopedia"


     // close the driver
     driver.close()
 }

}

Step 5. Execute te test:  > gradle check
Instead of assert, easyB DSL (Domain specific Language) support wide range of should syntax:
sshouldBe
shouldEqual
shouldBeEqual
shouldBeEqualTo
easyb supports the negative of the above phrases as follows (same rules apply as above):

shouldNotBe
shouldNotEqual
shouldntBe
shouldntEqual
What’s more, easyb allows you to verify object types, such as value.shouldBeAn Integer. Both positive and negative phrases are supported:

shouldBeA
shouldBeAn
shouldNotBeA
shouldNotBeAn
You can compare values with the should syntax as well:

shouldBeGreaterThan
shouldBeLessThan
shouldStartWith
shouldEndWith
Lastly, easyb supports verifying objects in a collection or properties of objects via the shouldHave method.
shouldHave
shouldNotHave

This is not all easyB has to offer and I encourage you to read documentation and use it in your projects.
Please share your experience with different tools for behavior-driven development in comment sections.

step 6. Learn more about easyB from documentation.

Enhanced by Zemanta

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>