Create a Cucumber Project by Integrating maven-cucumber-selenium-eclipse

This blog will explain the steps to follow for creating a simple Cucumber project.

Prerequisite: following should be installed:

  • Java
  • Eclipse
  • Maven
  • maven-eclipse-plugin
  • Cucumber-eclipse-plugin

Step 1) Lets create a new project in eclipse by following steps:

Click on new –> Other –> Maven –> Maven Project — > Next

1

Step 2) Now click on Simple project and keep the default workspace location.

2

Step 3) Provide details as Artifact id, Group id, name and description. and click on Finish.

3

Step 4) This should create a basic maven project. Now we need to add Cucumber and Selenium Dependencies in pom.xml.

Step 5) After adding dependencies of Selenium and Cucumber, this is how pom.xml should look like:

<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>Cucumber-book-blog</groupId>
    <artifactId>Cucumber-book-blog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Cucumber-book-blog</name>
    <description>Cucumber-book-blog</description>

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>
    </dependencies>
</project>

Step 6) We will create a simple project structure, to keep files at logical places. We need to create few packages, and please see below screenshot for better understanding.

5

Step 7) We will specify Junit Configuration class to define the location of features, step definitions etc. Create a class RunCukeTest.java in StepDefinitions package and code should look like:

package com.cucumber.StepDefinitions;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/com/cucumber/features",
        glue = "com.cucumber.StepDefinitions",
        plugin = { 
                    "pretty",
                    "html:target/cucumber",
                } 

        )
public class RunCukeTest {
}

Step 8) We will write a feature file for GitHub login. URL for test is: https://github.com/

Step 9) We will create a github_login.feature file in feature package. ( for creating .feature file, select “File” option in new menu and name the file with <<file_name>>.feature). Content of feature file should look like:

Feature: github login

  Scenario: login without username and password
    Given user is on github homepage
    When user clicks on Sign in button
    Then user is displayed login screen

PS: keeping the feature file simple to focus on creating the project and not focusing on Cucumber.

Step 10) Next step is to add Step Definitions for this feature file. How to do it?? Very Simple. Let Selenium take care of it. Right click on feature file and click Run As –> Cucumber Feature

8

Step 11) Check the console output for Step Definition errors.

9

Step 12) Create a GithubLoginSD.java file in package StepDefinitions. Copy Cucumber suggestions from console output, paste them to GithubLoginSD.java file.

package com.cucumber.StepDefinitions;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class GithubLoginSD {

    @Given("^user is on github homepage$")
    public void user_is_on_github_homepage() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^user clicks on Sign in button$")
    public void user_clicks_on_Sign_in_button() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^user is displayed login screen$")
    public void user_is_displayed_login_screen() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
}

Step 13) replace all the Throws code, with something meaningful selenium code, which will be executed when the test case is run. So we will write some selenium code now.

Step 14) Create SeleniumFunctions.java class in Selenium package. It should code for selenium invocation and tear down along with application specific code.

PS: I am keeping all the code in one selenium files for keeping things simple( you can implement Page objects also)

package com.cucumber.Selenium;

import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumFunctions {

public static WebDriver driver = null;
public static WebDriverWait waitVar = null;

public static String baseURL = "https://github.com/";

public void createDriver() throws MalformedURLException,
InterruptedException {

driver = new FirefoxDriver();

driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

driver.get(baseURL);

waitVar = new WebDriverWait(driver, 15);
}

public void teardown() {
driver.quit();
}

public void ishomepageDisplayed() {
waitVar.until(ExpectedConditions.presenceOfElementLocated(By
.linkText("Sign in")));

driver.findElement(By.linkText("Sign in")).isDisplayed();
driver.findElement(By.className("marketing-section-signup"))
.isDisplayed();
}

public void clickSigninLink() {
driver.findElement(By.linkText("Sign in")).click();
}

public void isloginsectionDisplayed() {
waitVar.until(ExpectedConditions.presenceOfElementLocated(By
.className("auth-form-body")));
waitVar.until(ExpectedConditions.presenceOfElementLocated(By
.name("commit")));
}

}

Step 15) Now we need to update the StepDefinitions with the selenium code. Here is how the code looks like:

package com.cucumber.StepDefinitions;

import java.net.MalformedURLException;

import com.cucumber.Selenium.SeleniumFunctions;

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

public class GithubLoginSD {

    SeleniumFunctions sf = new SeleniumFunctions();

    @Given("^user is on github homepage$")
    public void user_is_on_github_homepage() throws MalformedURLException,
            InterruptedException {
        sf.createDriver();
        sf.ishomepageDisplayed();
    }

    @When("^user clicks on Sign in button$")
    public void user_clicks_on_Sign_in_button() {
        sf.clickSigninLink();
    }

    @Then("^user is displayed login screen$")
    public void user_is_displayed_login_screen() {
        sf.isloginsectionDisplayed();
        sf.teardown();
    }
}

Step 16) So we are all set and we can now run our cucumber test case. There are two ways to do it.

First: Run the feature from eclipse. by right clicking the feature file –> Run as –> Cucumber feature ( we have seen this already)

Second: we can run this framework from command line as well. Open command prompt. CD till project root directory. And Run following command:

mvn clean test

This is the output:

11

I hope after reading the blog, you should be familiar with creating a simple cucumber-eclipse-project and knows how to run the cucumber scenarios ( eclipse or maven)

Advertisements

6 thoughts on “Create a Cucumber Project by Integrating maven-cucumber-selenium-eclipse

  1. Thanks Shankar. It’s a really good post and very helpful.
    But I need to change
    driver.findElement(By.className(“marketing-section-signup”)).isDisplayed();
    by
    driver.findElement(By.className(“homepage-hero-signup”)).isDisplayed();
    in order to work ok.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s