Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / Java
Tip/Trick

Getting started with Cucumber using Selenium

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
22 Dec 2015CPOL5 min read 12.3K   3  
To get started with cucumber using selenium with an example.

Introduction

In my previous article I have explained about how to get started with Cucumber framework.In this article I will go little ahead with how to Write a feature file and Step Definitions file using selenium in order to work with it by an example.

Background

Before moving ahead please read my previous article which is based on the brief introduction about cucumber framework here http://www.codeproject.com/Articles/1056664/Getting-started-with-Cucumber-Framework-for-automa .

Using Selenium with Cucumber

Let us consider the example of login page.In order to login into an application you need "username" and "password" and have to click on "submit" or "Login" button.

So here My feature is to login.Let us be specific with an application by taking Facebook as our application.
In order to do something in Facebook,you need to login ,right? That's exactly what is  a feature.

So in cucumber how we write this feature in gherkin language is :

Feature: Logging into Facebook
In order to do something in Facebook
As an user
I want to login

This is a feature for which there may be different scenarios.Scenario might be +ve/-ve scenario like if you put all username and password correctly and click on submit button it will become a +ve scenario.

Similarly if you put wrong username/password or do not put anything on username/password fields and click on Login button that will become a -ve scenario.

So now question arise how to handle this? No need to worry. In Cucumber it allows to write scenario in plain english just more or less like feature file written above.All scenarios related to a particular feature must be written in the same file in order to make user understand easily.

So here is the brief description how a scenario can be written.But before proceeding to that let me explain some gherkin keywords of cucumber which will be used in scenarios.It's quite simple and easy to understand.

Here is the list of Gherkin keywords:
Feature,Background,Scenario,Given,When,Then,And,But, Data Tables,Scenario Outline,Examples.

Feature: This is the begining of the feature file. It is not mandatory however it is recommended to use this. It will describe the features.

Scenario: Scenario can be added with all steps by just mentioning the keyword.

Given, When, Then, And, But, *

Given step is used to set the context or preconditions
When step is the action/ interaction or conditions
Then step for representing the outcome or result
And step for extending the previous step 
But step for more readability

Background: If a feature file contains multiple scenario and each scenario needs one particular step/scenario to be executed then a background keyword can be added and that particular step/scenario must be added so that you dont need to write the same step/scenario in each scenario.

DataTable: To pass a table of data,this can be used.

Scenario Outline: If the scenarios are varying with multiple set of data.Then at that time we use parameter and send data to the steps.This keyword is very helpful in such cases.

Examples:
If a scenario to be executed with different set of datas,then to send those data we use Examples.

Now its time to write a scenario inside a Feature File.Lets us write the +ve scenario to login into the Facebook application.

@Scenario: Login to Facebook
Given I navigate to Facebook
When I put username as "(your username)"
And I put password as "(your password)"
And I click on Login button.
Then I should be on Home Page with Title "(expected title after login)"

Now Let us write a step definition for this.

Code:

public class stepDefinitions{

// Initiate the driver instance
WebDriver driver = new FirefoxDriver();

@Given("^I navigate to Facebook$") 
public void I_navigate_to_Facebook(){

    
    driver.navigate("www.facebook.com");

}
@When("^I put username as \"([^\"]*)\"$") 
public void I_put_username_as(String value){

    driver.findElement(By.xpath("//*[@id='email']")).sendKeys(value);

}

@And("^I put password as \"([^\"]*)\"$") 
public void I_put_password_as(String value){

    driver.findElement(By.xpath("//*[@id='pass']")).sendKeys(value);

}
@And("^I click on Login button$")
public void I_click_on_Loginbutton(){

    driver.findElement(By.xpath("//*[@id='loginbutton']")).click();

}
@Then("^I should be on Home Page with Title \"([^\"]*)\"$") 
public void I_put_username_as(String expectedTitle){
    
    String actualTitle = driver.getTitle();
    Assert.assertEquals(expectedTitle,actualTitle);

}

driver.close();

}

 

This is a simple example to write feature file with step definitions in selenium.
Feature file can be kept anywhere in the same package where the step definitions files are present.Any number of feature files can be written and so with the step definitions files.

if you look into the above example step 2 and step 3 seems to be same except words "username" and "password" in each of the steps.So here cucumber comes with a good feature which is called parameterization in the step. Think,if you can send the "username" or "password" as a parameter then you dont need to write the same steps twice.It will automatically call the method by taking the parameter.So this can be achieved by passing "username" or "password" as a parameter as written below.

@Scenario: Login to Facebook
Given I navigate to Facebook
When I put "username" as "(your username)"
And I put "password" as "(your password)"
And I click on Login button.
Then I should be on Home Page with Title "(expected title after login)"

Steps for the above scenario:

public class stepDefinitions{

// Initiate the driver instance
WebDriver driver = new FirefoxDriver();

@Given("^I navigate to Facebook$") 
public void I_navigate_to_Facebook(){

    
    driver.navigate("www.facebook.com");

}

@And("^I put \"([^\"]*)\" as \"([^\"]*)\"$") 
public void I_put_(String value){

    driver.findElement(By.xpath("//*[@id='pass']")).sendKeys(value);

}
@And("^I click on Login button$")
public void I_click_on_Loginbutton(){

    driver.findElement(By.xpath("//*[@id='loginbutton']")).click();

}
@Then("^I should be on Home Page with Title \"([^\"]*)\"$") 
public void I_put_username_as(String expectedTitle){
    
    String actualTitle = driver.getTitle();
    Assert.assertEquals(expectedTitle,actualTitle);

}

driver.close();

}

Look at the step 2 in the stepDefinitions class.It will do the task for both the step 1 and step 2 in the scenario.Cucumber helps to reuse the code.wow!!! That seems to be cool,right? yes,it is. :)

How a feature file looks like using 'Scenario Outline' , 'Data Table' and 'Example' is :

Feature: Logging into Facebook
In order to do something in Facebook
As an user
I want to login

@login
Scenario Outline: Login to Facebook

Given I go to "LoginUrl" on "<browser>"
|  State           |  City            |
|  Karnataka   |  Bangalore  |
|  Odisha        |  BBSR          |
When I enter username as "<username>" in "LoginUsername"
And I enter password as "<password>" in "LoginPassword"
And I click on "LoginButton" button
Then I am in "Home" page with title "Login | name of the user"

Examples:
|  username      |  password       |  browser  |
|  username1    |  password1     |  Firefox    |
|  username2    |  password2     |  Chrome  |

 

This is all about getting started with selenium with Cucumber for BDD framework. Thanks for reading it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --