Getting started with Cucumber

Step 1: Create Feature file
  • Create a package name as ‘cbPack’ under src/test/java folder
  • Right click on the ‘bcPack’ and select ‘New’ > ‘File’
  • Provide the file name as ‘search.feature’
  • Clear all the stuff which got auto-generated in the feature file if any
  • Create a Feature File with various keywords like Feature, Scenario, Given, When, And, Then and But keywords as shown below in simple English
Feature: Searching of various categories should be possible

Scenario: Search for the products under books category
Given I visit the website as guest user
When I select the books option from the dropdown
And I click on search icon
Then I should see the books page upload
And I should see books at amazon as heading
But I should not see the products from other category products 

Scenario: Search for the products under baby category
Given I visit the website as guest user
When I select the baby option from the dropdown
And I click on search icon
Then I should see the baby page loaded 
And I should see the baby store as heading
But I should not see the other category products 
Step 2: Create Step definitions class
  • Right click on the ‘bcPack’ and select ‘New’ > ‘Class’
  • Provide step-definitions for all the steps that are provided in feature file as shown below
package cbPack;

import cucumber.api.java.en.And;
import cucumber.api.java.en.But;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class Search {

	@Given("^I visit the website as guest user$")
	public void i_visit_the_website_as_guest_user() throws Throwable {
		System.out.println(">>Given: I visit the website as guest user");
	}

	@When("^I select the books option from the dropdown$")
	public void i_select_the_books_option_from_the_dropdown() throws Throwable {
		System.out.println(">>When: I select the books option from the dropdown");

	}

	@And("^I click on search icon$")
	public void i_click_on_search_icon() throws Throwable {
		System.out.println(">>And: I click on search icon");

	}

	@Then("^I should see the books page upload$")
	public void i_should_see_the_books_page_upload() throws Throwable {
		System.out.println(">>Then:I should see the books page upload ");

	}

	@And("^I should see books at amazon as heading$")
	public void i_should_see_books_at_amazon_as_heading() throws Throwable {
		System.out.println(">>And:I should see books at amazon as heading");

	}

	@But("^I should not see the products from other category products$")
	public void i_should_not_see_the_products_from_other_category_products() throws Throwable {
		System.out.println(">>But:I should not see the products from other category products");
		System.out.println("-------------------------------------");

	}

	@When("^I select the baby option from the dropdown$")
	public void i_select_the_baby_option_from_the_dropdown() throws Throwable {
		System.out.println(">>When: I select the baby option from the dropdown ");

	}

	@Then("^I should see the baby page loaded$")
	public void i_should_see_the_baby_page_loaded() throws Throwable {
		System.out.println(">>Then:I should see the baby page loaded ");

	}

	@And("^I should see the baby store as heading$")
	public void i_should_see_the_baby_store_as_heading() throws Throwable {
		System.out.println(">>And:I should see the baby store as heading ");

	}

	@But("^I should not see the other category products$")
	public void i_should_not_see_the_other_category_products() throws Throwable {
		System.out.println(">>But: I should not see the other category products");
		System.out.println("-------------------------------------");
	}
}
Step 3: Create Runner class
  • Right click on the ‘bcPack’ and select ‘New’ > ‘Class’ < ‘Runner’
  • Provide @RunWith(Cucumber.class) on the top of the class as shown on below code
  • Run as ‘Runner’ Class with JUnit and see the console and JUnit results tab
package cbPack;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class Runner {
//Making this class as a runner class with the help of @RunWith annotation and it is part of Junit Api
		
}
Step 4: Install Ansi-escape-console (Not Mandatory)

We will discuss more in details it is just overview how we are going to work with Cucumber. Keep reading !!

Next

Leave a comment

Design a site like this with WordPress.com
Get started