Pages

Wednesday, October 1, 2014

Behavior Driven Development with Cucumber

While searching a better automation solution for web applications, I could find a great automation framework which is called “Cucumber”. Simply, Cucumber is a BDD supported testing tool that executes automated tests. It is easy & flexible solution that supports java-selenium development. Unlike selenium robot framework, this BBD solution has a huge technical support on internet.

What is BDD?
BDD (Behavior Driven Development) is a good extension to the conventional TDD (Test Driven Development).


TDD is widely using by developers to write their unit tests. Add a test, run entire test suit and write some code on newly added test, rerun all tests and refactor is the main concept of TDD.

BDD has a different approach. Its mainly focus on the behavior (input and outcome) of the functionality. In other words, BDD is a combination of user story (a short introduction of tests) and acceptance criteria.


Example :
          As a [role] I want [feature] so that [benefit]
          Given [initial context]
          When [input event]
          Then [outcome]

Cucumber is used to execute tests written in this format. The language that Cucumber understands is called 'Gherkin'. Gherkin is just a combination of four keywords (given, when, then, and). It's very easy to write tests with Gherkin. also, even non-technical people can understand the tests and scenarios. Check below example which is a test written in Gherkin.

Feature: Search on Google
  As an user
  I want to search on Google
  So that I can see results

  Scenario: results are shown
    Given the page is open "http://www.google.com"
    When I search for "Cucumber"
    Then a browser title should contains "Cucumber"

Now, let’s have some fun with cucumber!

Setup your first cucumber project
  1. Download sample cucumber-java-selenium project from here - https://github.com/michalvich/cucumber-jvm-selenium-example

    Note 1 : Check the directory structure. You can see ‘pom.xml’ in the root directory of the project. ‘pom.xml’ is the fundamental unit of work in Maven. It is contains information about the project and configuration details used by Maven to build the project. 
  2. Note 2 : You can see the .feature file at '..\src\test\resources\com\michalvich\cucumber\selenium\example'. That is the cucumber test case document which is written in Gherkin.
    Note 3 : 
    You can see java code at '..\src\test\java\com\michalvich\cucumber\selenium\example', which is the code we should write to execute our cucumber test cases.


  3. Install Java, create a user variable “JAVA_HOME” and set it to jdk installation directory (example - C:\Program Files\Java\jdk1.7.0_21)

  4. update/create the Path environment variable in the user variables and append the %JAVA_HOME%\bin

  5. Check java is installed successfully. ( type “java –version” in command prompt)

  6. Download eclipse IDE (https://www.eclipse.org/downloads/)

  7. Download maven (http://maven.apache.org/download.cgi)

  8. Read the installation instruction at http://maven.apache.org/download.cgi#Installation or follow these steps.

    1. Add the M2_HOME environment variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.2.1
    2. Add the M2 environment variable in the user variables with the value %M2_HOME%\bin
    3. update/create the Path environment variable in the user variables and append the value %M2% 

  9. Check maven is installed successfully. (type “mvn –version” in command prompt)

  10. Note : Make this small change before proceed. Open the 'pom.xml' file. find this set of codes
    <dependency>
           <groupId>org.seleniumhq.selenium</groupId>
           <artifactId>selenium-java</artifactId>
           <version>2.28.0</version>
    </dependency>
    Change the version no "2.28.0" to "2.41.0"

  11. Go to the place which pom.xml located and type,
    1.  “> mvn package” (to build the project)
    2. “> mvn install” (optional)
    3. “> mvn eclipse:eclipse” (to generate the eclipse project files from your POM)

  12. Open eclipse and set the maven repo directory (in eclipse - windows - preferences - java - built path -> add M2_REPO --- maven repo directory (C:/Users/Test/.m2/repository))

  13. Open eclipse and import the project ( file – import – general - existing project in to workspace)

  14. Run the project as a junit (run – run configurations – browse the project and the class)
How to write your own code?
  • First, write a cucumber test case document, save as a .feature and put it in the same directory which locate ‘GoogleSearch.feature’.
  • Then, Create a empty java class with the same name and just run it. You can see the code snippets print in your eclipse console window.
  • Finally, Copy and paste the snippets to implement missing steps. Write few lines of codes for test. you can have any help from internet.

Look! How easy it is! Software automation is not a nightmare anymore!


No comments:

Post a Comment

Blogger news

Blogroll

What's Hot