Cucumber Series Tutorial 1




Running & Setup Cucumber BDD based Test



What is BDD?

Behavior-driven development is a collaborative approach to software development that bridges the communication gap between business and IT. In this, we are defining the behavior of Test Cases.
Gherkin language is used to define the Behaviour in BDD.  It is a simple feature language.


What is Cucumber?

A cucumber is a tool that supports Behaviour-Driven Development(BDD).
JBehave is also another tool for BDD Framework.

Components of Cucumber:


  1. Feature File: extension is .feature. It's an executable specification written in plain text In this file Gherkin keywords like given, when, then, As, But, etc used. We create a Scenario for a specific feature.
  2. Step definition FileStep definitions map (or “glue”) each Gherkin step to programming code to carry out the action that should be performed by the step. We will be writing code. Using Java and Selenium.
  3. Test Runner: To run your feature, to generate Report we use TestRunner class

Setup Cucumber in Eclipse:

Step1: Create Maven Project
New->Project->Maven add cucumber archetype in maven.

Do not check "Create a simple project"

Click next.




Make sure cucumber-archetype is present. If not add it.



Step2: add the following dependencies. The sample POM.xml is as follows.



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>hellocucumber</groupId>
<artifactId>hellocucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<cucumber.version>4.2.6</cucumber.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</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.0.0</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>


Step3: Add "Natural" Plugin in eclipse. This is an eclipse plugin for BDD.
You can download it from an eclipse Market place or you can get it from the following link.


You just need to drag and drop the install link into your current eclipse workspace. After installation restart eclipse.

Step4: Add feature file


Feature: Free CRM Login Feature
Scenario: Free CRM Login Test Scenario

Given User is already on Login Page
When Title of login page is Free CRM
Then User enters Username and password
And  User clicks on login button

And user is on home page


Step5: Add Test Runner File


package com.Runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/com/feature", //the path of the feature folder
glue={"com.stepdefination"} //the package name of the step definition files)

public class TestRunner {

}


Step6: Add Step Definition File


package com.stepdefination;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import junit.framework.Assert;

public class LoginStepDefination {
WebDriver driver;
@Given("^User is already on Login Page$")
public void user_is_already_on_Login_Page() {
System.setProperty("webdriver.chrome.driver", "chromedriver_mac");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://ui.cogmento.com/");
   
}
@When("^Title of login page is Free CRM$")
public void title_of_login_page_is_Free_CRM() {
    String title = driver.getTitle();
    System.out.println(title);
    Assert.assertEquals("Cogmento CRM", title);
}

@Then("^User enters Username and password$")
public void user_enters_Username_and_password() {
 driver.findElement(By.xpath("//input[@name='email']")).sendKeys("username");
          driver.findElement(By.xpath("//input[@name='password']")).sendKeys("password");
}

@Then("^User clicks on login button$")
public void user_clicks_on_login_button() {
driver.findElement(By.xpath(" //div[text()='Login']")).click();    
}

@Then("^user is on home page$")
public void user_is_on_home_page() {
  new WebDriverWait(driver,5000).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[text()='Deals Summary']")));
}


}

Step7: Run TestRunner.java Class as Junit Test. The sequence of execution will be as per instructions mentioned in the Feature file. Which is written in Gherkin language.

Project Structure























Hope You Like this blog and it's helpful to kick-start the automation with Cucumber. Please share your thoughts and Like it if it's helpful. Thanks...

Comments

Popular posts from this blog

Automate Mobile OTP Number with Selenium

Cucumber Series Tutorial 2- Make your Automation Data Driven by Parametrising Feature File

Cucumber Series Tutorial 3- Make your Automation Data Driven by Parametrising Feature File with Scenario Outline and Examples keyword.