Automation and BDD Framework using Cucumber

     In this article, I would like to share the concept of configuring BDD (behavior-driven development) and running of Selenium using Cucumber. 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.

Configuration and Implementation

Seven Steps to configure BDD (behavior-driven development) and running of Selenium or Appium using Cucumber – JVM:

1. First create a maven eclipse project.

2. Create pom.xml and add all the dependendcies

3. Create a feature file and put into “resources” folder.

4. Create step definition file for above scenario and implement WebDriver (for Web based applications) or AppiumDriver (for native applications) automation code. So Create a java file to keep the step definition and put this file under respective package of source folder” src/main/java”.

5. Create Cucumber runner java class which defines cucumber-jvm configuration and put under respective package of source folder ” src/test/java”.

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

7. You should see test will execute and after execution report is generated under directory “\target\cucumber-html-report”. Open index.html file to see the report.

Following are the detailed explanations:

Step 1:  First create a maven eclipse project.

+—src

¦   +—main

¦   ¦   +—java

¦   ¦       +—com

¦   ¦           +—projectname

¦   ¦               +—tests

¦   +—test

¦       +—java

¦           +—com

¦               +—projectname

¦                   +— cucumberrunner

Step 2:  maven pom.xml file has below code:

<project xmlns=http://maven.apache.org/POM/4.0.0&#8221; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

       xsi:schemaLocation=http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&#8221;>

       <modelVersion>4.0.0</modelVersion>

       <groupId>CucumberTestNG1</groupId>

       <artifactId>CucumberTestNG1</artifactId>

       <version>0.0.1-SNAPSHOT</version>

       <packaging>jar</packaging>

       <name>CucumberTestNG1</name>

       <url>http://maven.apache.org</url&gt;

       <properties>

              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

       </properties>

       <build>

              <plugins>

                     <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-surefire-plugin</artifactId>

                           <version>2.18.1</version>

                           <configuration>

                                  <suiteXmlFiles>

                                         <suiteXmlFile>resources/testng.xml</suiteXmlFile>

                                  </suiteXmlFiles>

                           </configuration>

                     </plugin>

              </plugins>

       </build>

       <dependencies>

              <dependency>

                     <groupId>org.seleniumhq.selenium</groupId>

                     <artifactId>selenium-java</artifactId>

                     <version>2.50.0</version>

              </dependency>

              <dependency>

                     <groupId>io.appium</groupId>

                     <artifactId>java-client</artifactId>

                     <version>3.3.0</version>

              </dependency>

              <dependency>

                     <groupId>org.testng</groupId>

                     <artifactId>testng</artifactId>

                     <version>6.9.10</version>

              </dependency>

              <dependency>

                     <groupId>info.cukes</groupId>

                     <artifactId>cucumber-testng</artifactId>

                     <version>1.2.4</version>

              </dependency>

              <dependency>

                     <groupId>info.cukes</groupId>

                     <artifactId>cucumber-java</artifactId>

                     <version>1.2.4</version>

                     <scope>test</scope>

              </dependency>

              <dependency>

                     <groupId>info.cukes</groupId>

                     <artifactId>cucumber-jvm</artifactId>

                     <version>1.2.4</version>

                     <type>pom</type>

              </dependency>

              <dependency>

                     <groupId>info.cukes</groupId>

                     <artifactId>cucumber-jvm-deps</artifactId>

                     <version>1.0.5</version>

              </dependency>

              <dependency>

                     <groupId>net.masterthought</groupId>

                     <artifactId>cucumber-reporting</artifactId>

                     <version>1.2.0</version>

              </dependency>

              <dependency>

                     <groupId>info.cukes</groupId>

                     <artifactId>gherkin</artifactId>

                     <version>2.12.2</version>

              </dependency>

              <dependency>

                     <groupId>org.mockito</groupId>

                     <artifactId>mockito-all</artifactId>

                     <version>2.0.2-beta</version>

              </dependency>

              <dependency>

                     <groupId>com.google.guava</groupId>

                     <artifactId>guava</artifactId>

                     <version>18.0</version>

              </dependency>

              <dependency>

                     <groupId>org.hamcrest</groupId>

                     <artifactId>hamcrest-all</artifactId>

                     <version>1.3</version>

              </dependency>

       </dependencies>

</project>

Step 3: Create a feature “googlesearch.feature” file and put into “resources” folder. This file contain below scenario:

Feature: Google Search

  Scenario: Advance Search in Google

    Given user is on google search page

    When enter “journey of quality” text into search field

    And click on search button

    Then verify google logo is “true”

Step 4: Now need to create step definition file for above scenario and implement WebDriver (for Web based applications) or AppiumDriver (for native applications) automation code. So Create a java file “GoogleSearchTest.java” and put under package “com.projectname.tests” of source folder” src/main/java” and write below code:

package com.projectname.tests;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import cucumber.api.java.After;

import cucumber.api.java.Before;

import cucumber.api.java.en.Given;

import cucumber.api.java.en.Then;

import cucumber.api.java.en.When;

public class GoogleSearchTest {

       public static WebDriver driver;

       // Not TestNG annotation and it related to cucumber.api

       @Before

       public void setUp() {

              System.setProperty(

                           “webdriver.chrome.driver”,

                           “E://Softwares//Important tools//selenium-2.44.0//Old Drivers//chromedriver.exe”);

              driver = new ChromeDriver();

              driver.manage().window().maximize();

       }

       // Not TestNG annotation and it related to cucumber.api

       @After

       public void tearDown() {

              // driver.quit();

       }

       // This annotation related to precondition in test execution like

       // @BeforeTest/@BeforeMethod/@BeforeClass

       @Given(“^user is on google search page$”)

       public void user_is_on_google_search_page() throws Throwable {

              driver.get(“https://www.google.co.in/&#8221;);

       }

       // This annotation same as @Test of TestNG, here we are doing actual

       // operation

       @When(“^enter \”([^\”]*)\” text into search field$”)

       public void enter_text_into_search_field(String arg) throws Throwable {

              driver.findElement(By.id(“lst-ib”)).sendKeys(arg);

       }

       // This annotation same as @Test of TestNG, here we are doing actual

       // operation

       @When(“^click on search button$”)

       public void click_on_search_button() throws Throwable {

              driver.findElement(By.id(“sbds”)).click();

       }

       // This annotation related to postcondition in test execution like

       // @AfterTest/@AfterMethod/@AfterClass

       @Then(“^verify google logo is \”([^\”]*)\”$”)

       public void verify_google_logo_is(boolean msg) throws Throwable {

              WebElement message = driver.findElement(By

                           .xpath(“.//*[@id=’logo’]/img”));

              Assert.assertEquals(message.isDisplayed(), msg);

       }

}

Step 5: Now create “Cucumberrun.java” which defines cucumber-jvm configuration and put under package “com.projectname.cucumberrunner” of source folder” src/test/java”.

package com.projectname.cucumberrunner;

import cucumber.api.CucumberOptions;

import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(features = “resources/googlesearch.feature”,

glue = “com.projectname.tests”, format = {

              “html:target/cucumber-html-report”,

              “json:target/cucumber-json-” + “report.json” })

public class Cucumberrun extends AbstractTestNGCucumberTests {

}

Step 6. Open command prompt and goto project directory and run command:

mvn install (or you can directly right click on “pom.xml” and run “Maven install”

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

Capture

Enjoy your development……………