Create BDD Project with Selenium + Cucumber

How to Design Selenium Cucumber Framework

Behavior Driven Development (BDD) is one of the trending automation frameworks in Software Industries. BDD framework is supports agile development methodology. Due to better readability and easy design, it’s preferred among Business Analyst, Functional Tester and project stakeholder. 

This tutorial help you set up your first BDD Project using Selenium, Cucumber and Java. In this tutorial we will explain each step with snapshots and descriptions. Refer below for step by step:

Pre requisite: 

  1. Java should be installed on machine( if not then follow our step by step tutorial- link)
  2. Maven should be set up on machine( if not then follow our step by step tutorial- link)
  3. Eclipse or Intellij should be set up on machine ( if not then click here for Eclipse Download and Link for Intellij Download)

Create Maven Project:

  • Step 1:  Create New Maven Project

Open Eclipse and go to File> New >Project

Create Project in Eclipse IDE
  • Step 2: Select Maven Project and click on Next and Next:
Create Maven Project
  • Step 3: Select Maven archType “maven-archtype-quickstart” and click Next
maven-archtype-quickstart
  • Step 4: Enter Project artifacts as per below snapshot and click on “Finish”.
Maven project artifacts
  • Step 5: Click on Eclipse “Help” and  go to Eclipse “Eclipse Marketplace”. Install Cucumber Plugins. Refer below for snapshots:
  1. Cucucmber Eclipse Plugin
  2. Natural 0.0.7.6
Eclipse marketplace
Cucumber plugin & Natural plugin in Eclipse marketplace

Maven Project structure before Cucumber & Natural plugin depedency

Add Cucumber Dependency at POM.XML

  • Step 6: Open POM.xml and add Cucumber dependencies:
  1. cucumber-java
  2. cucumber-junit
  3. junit

Add below dependencies in POM.xml- under dependencies tag

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
  • Step 7: Add WebDriver Manager dependency and Selenium Server in POM.xml. WebDriver Manager is used to manage Browser Binaries automatically and Selenium Server is used  to perform operation on Browser.
<dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>3.141.59</version>
</dependency>

After adding above dependencies your final POM.xml like below:

<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>thoughtCoders</groupId>
    <artifactId>bdd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>bdd</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>
</project>
  • Step 8: Update Project: Right Click on Project and go to Maven and then click on “Update Project”.
Update maven project
  • Step 9: After Project Update, again click on “Maven Install”. 
Maven install

Note:- After Maven Install – Console output should have Build success.

Build success after maven install

Now your project is configured with all the required Jars and Eclipse Plugins.

  • Step 10: BDD Project have three major components. First one is “Runner class”, “Feature File” and “Step Definition”. So you need to create three packages:
  1. resources Folder: to keep your feature files inside this folder.
  2. Runner Package: Runner class is created inside this package. This class is used to run feature file and customize runner options.
  3. stepDefs Package: All the test steps implementation code will keep in this package.
Components of bdd project
  • Step 11: Write first feature file. Go to resources folder and right click on it create “Login.feature” file and write test scenarios in Gherkin language:
Login.feature

Feature: Login 

Login should be done with correct credentials

Scenario: Login with correct credentials

Given Browser is opened 

When user is navigated to URL

And user entered their username “mercury” and password “mercury”

Then user successfully login into system

Scenario: Login with incorrect credentials

Given Browser is opened 

When user is navigated to URL

And user entered their username “mercury” and password “mercury1”

Then user should not login into system

Login.feature code
  • Step 12: Go to stepDef package and create testSteps.java class and implement all the test steps as per below:
package stepDefs;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.github.bonigarcia.wdm.WebDriverManager;
public class testSteps {
    
    public static WebDriver driver;
    
    @Given("Browser is opened")
    public void browser_is_opened() {
         WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        
    }
    
    @When("user is navigated to URL")
    public void user_is_navigated_to_URL() {
        // Write code here that turns the phrase above into concrete actions
        driver.get("http://newtours.demoaut.com/");    
    }
    @When("user entered their username \"(.*)\" and password \"(.*)\"$")
    public void user_entered_their_userName_and_password(String userName, String password) {
        driver.findElement(By.xpath("//input[@name='userName']")).sendKeys(userName);
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password);
        driver.findElement(By.xpath("//input[@name='login']")).click();
    }
    @Then("user successfully login into system")
    public void user_successfully_login_into_system() {
        String winTitle = driver.getTitle();
        Assert.assertEquals(winTitle, "Find a Flight: Mercury Tours:");
        driver.close();
    }
    @Then("user should not login into system")
    public void user_should_login_into_system() {
        String winTitle = driver.getTitle();
        Assert.assertEquals(winTitle, "Sign-on: Mercury Tours");
        driver.close();
    }
}
  • Step 13: Go to Runner Package and create TestRunner.java class and implement below code:

With @CucumberOptions we can pass parameters in following purposes:

  1. features= “src/test/java/Resources”— Path of feature File.
  2. glue = {“stepDefs”}- Path of step Defination Folder
  3. plugin = {“pretty”,”html:report”,”json:report//cucumber.json”,”junit:report//cucumber.xml”- This is used for report customization, path and output Directory.
  4. dryRun =false  – dryRun is used to check whether all the steps are implemented or not. By default it is false. But when you want to check missing steps of feature then make it “true”. It will give missing test steps methods.
  5. Monochrome= true-  is used to format console output from Cucumber in formatted manner(Readable). If you want well formatted console message then you have to set “true”. By default it is false.
  6. Strict =false: For example if you want to skip undefined steps from execution, then you have set “false”. By default value is false.
package Runner;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
        features= "src/test/java/Resources",
        glue = {"stepDefs"},
        plugin = {"pretty","html:report","json:report//cucumber.json","junit:report//cucumber.xml"},
        dryRun =false,
        monochrome =true
         
        )
public class TestRuner {
    
}

Maven Project Structure with Cucumber and Natural Plugin

  • Step 14: Go to TestRunner class and run test. 
  • Step 15: After test execution, right click on Project and Refresh. Again go to Report Folder and open HTML report. You will find cucumber report. 
Cucumber html report

Above report is default cucumber report. There are many open source plugin are available to create attractive report. Few popular plugins to generate report are Allure Report, Master Thought, Extent Report and Testng Report. To configure these reports check our blogs.

Hope above steps helps you to set up first BDD Cucumber project and execute. Feel free to share your feedbacks and refer other post for more tutorials on Automation. Also contact us for more details on our Services.

3.3 3 votes
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Suriya
Suriya
2 months ago

I have used the same dependency in pom.xml file but i am getting the eeror in Runner class
–>Class<Cucumber> cannot be resolved to a type
–>Cucumber Options cannot be resolved to a type

2
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact