Tuesday, May 5, 2015

BDD (Cucumber) with Selenium Webdriver

In the following post I am going to explain how to setup cucumber on Java with selenium to use for Behavior Driven Development.

I am going to use IntelliJ for writing this test cases, however you can choose eclipse or any other IDE as well.

How to create project.

I have also hosted the demo project on GitHub you can access that using following link 

  • Go to File >> New >> Project and select Maven as shown in the screenshot
  • Provide GroupId and ArtifactID
  • Provide the Project Name, After this step, IntelliJ will create new project for you. 

  • Add Maven Dependencies.

    We need to add Cucumber, Selenium and JUnit dependencies to pom.xml. Open pom.xml and add the following code to it.


    Automating Your First Test case

    I am going to automate following test case for this BDD (Cucumber) demo
    1. Open wikipedia.com website 
    2. Search for BDD 
    3. Result page should contain Behavior-driven development text. 
    Now before we begin writing test cases, let's create proper packages. I have created the packages in following, however you can create the way you like. 

    Just make sure that all your code (directly or indirectly) is under test package. 

    The first thing we need to do, to write cucumber tests, is to convert existing test case into Gherkin Syntax.  In the resources folder create one package and inside this package create one feature file, mine is called wikipedia_search.feature however you can give the name you want. 

    Feature: Verify WikiPedia Search
      Scenario: Verify Wikipdia for indirect posts
      When I search "BDD"
      Then I should get "Behavior-driven development" on page

    Feature:, is a keyword through which you can specify which feature you are testing
    Scenario:, is a keyword though which you can specify of which scenario following test cases belong to
    Your test cases start after the scenario line. When specifies the step we need to perform and Then specifies the part we need to verify.

    Now the next thing we need to do is, implement this test cases and write selenium code for it. however before that you need to write a Java class which runs your test cases. Create a java class under test\java\com.gaurang package and paste the following code.

    Please create one file with following code
    @CucumberOptions(plugin={"pretty", "html:target/cucumber-html-report"})
    public class RunAllTests {

    Now everything is in place, let's write the selenium code for Gherkin statement we wrote above. There are actually two way to do it.

    1. Run the file created in above step, and on the console you will be able to see something like below. copy this code and paste into the class file. 
    2. You can open the feature file and press ALT+Enter key combination, this will ask you to choose the file name and then it will create the empty implementation of all the steps in that file. 
    Once this is done, the only thing remain is to write corresponding selenium code. 


    Cucmber generates it's own HTML report, which look like this. 

    Cucumber HTML Report