Java WebDriver BDD framework using Cucumber-JVM

In this post I will show you how to create BDD framework in webdriver selenium using cucumber-jvm. Cucumber –JVM is based on cucumber framework which allows to writes feature file in plain text using Gherkin language, This feature is supported by step definitions file which has implemented automation code of Webdriver.
This post cover maven based java webdriver BDD framework using cucumber for Google advance search scenario, Following are the steps to write scenario for BDD framework:
1.  First create a maven eclipse project.
WebdriverBDD
                    src/main/java                 
                   
                    src/test/java
                    ******** com/maven/test
                       
                    src/test/resources
                    ******** com/maven/test
                    pom.xml


2. maven pom.xml file has below code:

3. Create a feature “GoogleScenario.feature”  file and put into package “com/maven/test” of source folder “src/test/resources”. This file contain below scenario.
Feature: Google Search

Scenario: Advance Search in Google
            Given user is on google search page
            When enter "testing" text into search field
            And click on search button
            And click on advance search icon
            And click on advance search link
            And click on advance search button
            Then verify first link text "testing"

4. Now need to create step definition file for above scenario and implement webdriver automation code. So Create a java file “GoogleStep.java” and put under into package “com/maven/test” of source folder” src/test/java” and write below code:
package com.maven.test;

import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.*;
import cucumber.annotation.en.*;  

public class GoogleStep {

      protected WebDriver driver;

      @Before
      public void setUp() {
            driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().window().maximize();
      }

      @After
      public void tearDown() {
            driver.close();
      }
           
      @Given("user is on google search page")
      public void The_user_is_on_google_search_page() {                      
            driver.get("https://www.google.co.in/");
      }
           
      @When("enter \"([^\"]*)\" text into search field")
      public void User_enters_text_into_field(String text) {
            driver.findElement(By.id("gbqfq")).sendKeys(text);
      }
           
      @And("click on search button")
      public void user_click_on_search_button() {
            driver.findElement(By.id("gbqfb")).click();
      }    
           
      @And("click on advance search icon")
      public void click_on_advance_icon() {
            driver.findElement(By.id("abar_button_opt")).click();
      }
           
      @And("click on advance search link")
      public void click_on_advance_link() {
            driver.findElement(By.xpath("//div[text()=\"Advanced search\"]")).click();
      }
           
      @And("click on advance search button")
      public void click_on_advance_button() {
            driver.findElement(By.xpath("//input[@value='Advanced Search']")).click();
      }
           
      @Then("verify first link text \"([^\"]*)\"")
      public void verify_first_link_text(String msg)
     {
       WebElement message = driver.findElement(By.xpath("//*[@id='rso']/li[1]/div/h3/a"));
       Assert.assertTrue(message.getText().contains(msg));
      }    
           
}

5. Now create “RunCukesTest.java” which defines cucumber-jvm configuration and put under package “com/maven/test” of source folder” src/test/java”.
package com.maven.test;

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber-htmlreport","json-pretty:target/cucumber-report.json"})

public class RunCukesTest {
}

6. Open command prompt and goto project directory and run command:
mvn install

7. You should see test will execute and after execution report is generated under directory “\target\cucumber-htmlreport”. Open index.html file, report should be as below:

7 comments:

  1. Wonder full very clear steps.

    ReplyDelete
  2. visit the below link for more details. You will get trained on Examples and practical implementation
    http://seleniumeasy.com

    ReplyDelete
  3. Hi, I am getting the below error. Please Help !

    2 Scenarios ( [31m1 failed [0m, [32m1 passed [0m)
    10 Steps ( [31m1 failed [0m, [36m6 skipped [0m, [32m3 passed [0m)
    0m0.067s

    java.lang.NullPointerException
    at test.cucumber.Browser.The_user_is_on_google_search_page(Browser.java:38)
    at ?.Given user is on google search page(test/cucumber/BrowserTest.feature:4)


    ReplyDelete
  4. you may have initialised the driver on the browser.java aswell. you need to give the full code, on the assumption you may have done this

    public class Browser extends Somebaseclass {

    Private RemoteWebDriver driver;

    public Browser(RemoteWebDriver driver) {

    super(driver);
    }

    public class Somebaseclass {

    protected RemoteWebDriver driver;
    public Somebaseclass(RemoteWebDriver driver) {

    Somebaseclass.driver = driver;
    }

    ReplyDelete
  5. Wow. Awesome explanation. Good Job

    ReplyDelete
  6. I would like to know what happens if i have a scenario which calls multiple step definition method, in that case how the browser driver will be share the session across multiple stepdefinition classes?

    ReplyDelete
  7. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.
    When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    thanks a lot for taking a time to share a wonderful article.

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution