How to set up Cucumber with Selenium for Automation testing?

We have learned all the aspects of Cucumber and Behaviour Driven Development and Testing as well. Today our prime focus is the implementation of Cucumber with Selenium. Cucumber is itself a kind of framework which makes testing processes efficient and systematic. So, if we combine Cucumber and Selenium altogether then automation testing becomes very powerful and it also improves the performance of the automation test pack.

Cucumber can be integrated not only with Selenium, but also, we use Cucumber with Watir and Capybara. If we talk about real-time application, then Cucumber is often used with Selenium Testing tool only. Thus, the agenda of this tutorial is understanding the use of Cucumber along with Selenium.

Let’s dive into the knowledge flow.

How to configure Cucumber with Selenium?

We firstly need to configure Cucumber and Selenium both to make compatible for each other. You just need to add some external JARs to your Selenium project. Here is the list of those JAR files-

  • Cobertura
  • Cucumber reporting
  • Mockito all
  • Selenium server standalone
  • Cucumber Junit
  • Junit

We have already discussed the steps to add these JARs. These Jar files are available to download from Maven repository site and SeleniumHq site.

If you are using Maven then you can create dependencies of above JAR files in a pom.xml file, instead of downloading and adding JARs to your Selenium project.

Click on below link to learn how to configure Cucumber in your project.

Steps to configure Cucumber with Selenium

You can also place these dependencies in your pom.xml

<dependencies>
           <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.11</version>
        </dependency>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit-dep</artifactId>
                 <version>4.10</version>
             </dependency>
             <dependency>
                 <groupId>info.cukes</groupId>
                 <artifactId>cucumber-core</artifactId>
                 <version>${cucumber.version}</version>
             </dependency>
             <dependency>
                 <groupId>info.cukes</groupId>
                 <artifactId>cucumber-java</artifactId>
                 <version>${cucumber.version}</version>
             </dependency>
             <dependency>
                 <groupId>info.cukes</groupId>
                 <artifactId>cucumber-junit</artifactId>
                 <version>${cucumber.version}</version>
             </dependency>
             <dependency>
                 <groupId>org.seleniumhq.selenium</groupId>
                 <artifactId>selenium-java</artifactId>
                 <version>2.31.0</version>
             </dependency>
         </dependencies>

You need to add some plugins too-

<plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>1.6</source>
                    <complianceLevel>1.6</complianceLevel>
                    <verbose>false</verbose>
                    <showWeaveInfo>false</showWeaveInfo>
                    <target>1.6</target>
                    <weaveDependencies>
                        <weaveDependency>
                            <groupId>info.cukes</groupId>
                            <artifactId>cucumber-junit</artifactId>
                        </weaveDependency>
                        <weaveDependency>
                            <groupId>info.cukes</groupId>
                            <artifactId>cucumber-java</artifactId>
                        </weaveDependency>
                    </weaveDependencies>
                </configuration>
            </plugin>
		</plugins>

How to create Feature file and Step definition for Selenium?

In our previous tutorial, we discussed Feature files creation and Step definition in the Cucumber framework. So, I would like to first suggest you read the previous tutorial-

Tutorial on Cucumber feature file creation and Step definition development

Feature file creation and Step definition are similar in Selenium as well.

Cucumber-Selenium Folder Structure

Here we need to understand the hierarchy of folder first. At first, we create a new package where all the Test steps class will be present. In our project, we created a package named Steps. Next, we create another folder where all the Cucumber feature files will be stored, and we name it as Resources/Features. Lastly, we create a Cucumber runner class where the command to integrate feature files and step definition will be written with respect to Selenium. Below image highlights the folder structure of Cucumber with Selenium.

Cucumber with Selenium folder structure

Test Scenario

We have a scenario where we first open Google and first validate the title of the page then we check whether search box is present or not.

Let’s convert above scenario to feature file with the help of plain English and Gherkin syntax.

Feature File

SearchEngine.feature

#Author: Avinash Mishra

Feature: Title and Search box validation
  I want to check title and presence of search form

  Scenario: Validate title and search box
    Given User opens Google site
    Then the title is Google
    And Search box is present

Now coming to its Step definition.

Step Definition

SearchEngineStepDefinition.class

package Steps;


import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

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

public class SearchEngineStepDefinition {
	WebDriver driver;

	@Before
	public void setUp() throws Exception {
		System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  	 
	  	  driver  = new ChromeDriver();
	  		  
	  	  driver.manage().window().maximize();
	}

	@After
	public void tearDown() throws Exception {
		
		driver.close();
		  
		driver.quit();
	}

	@Given("^User opens Google site$")
	public void user_opens_Google_site() {
		
		driver.get("https://www.google.com/");
	}

	@Then("^the title is Google$")
	public void the_title_is_Google() {
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.getTitle();
	}

	@Then("^Search box is present$")
	public void search_box_is_present() throws InterruptedException {
		Thread.sleep(3000);
		driver.findElement(By.xpath("//*[@name='q']")).sendKeys("");
	}

}

Cucumber Runner Class

CucumberRunner.class

package Utilities;

import org.junit.runner.RunWith;

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

@RunWith(Cucumber.class)
@CucumberOptions(features="Resources/Features/SearchEngine.feature" ,
glue="Steps/SearchEngineStepDefinition.java")


public class CucumberRunner {
	
}

Run this runner class as JUnit Test.

This is all about cucumber set up with Selenium. You can ask your doubts via the comment form below.

 
Join Inviul fb group

Leave a Reply