Cucumber is a software testing tool which can be used to test the functionality. It run automated acceptance test written in behavioral driven language. It was initially written in Ruby but now a days different language support Cucumber.

Cucumber test framework require two different files

.feature file

Feature:I want to test Account Creation

@Test1

Scenario: Open Account Page

Given navigate to "https://test.salesforce.com"

Given username "PUT YOUR USERNAME HERE" and password "PUT YOUR PASSWORD HERE"

Then select "/001/o"

Scenario file

Feature file is combination of five attributes

  1. Feature
  2. Scenario
  3. Given
  4. When
  5. Then

 

  1. Feature : It is whole functionality which we need to test, we can map this with Test Class in Salesforce
  2. Scenario: It is equivalent to Test method of Salesforce so every single scenario will cover one logic.
  3. Given: What we already know
  4. Then: What we are going to do or what we are expecting.

Let’s get started how we can test this with eclipse IDE

Step 1: Setup Eclipse IDE:

Download Eclipse IDE EE

Step 2: Install Maven for Eclipse

If you have eclipse install please click on Help-> Eclipse Marketplace and click install.

Eclipse marketplace

Step 3: Restart your eclipse.

STEP 4 : INSTALL CUCUMBER JVM

STEP 5 : INSTALL CUCUMBER ECLIPSE PLUGIN

Step 6: Create new Maven Project.

new project

Step2

step3

Your Project explorer will look like this.

project explorer

Step 7: Download and Import Jar files

Download following Jars

add-jar

Step 8: Verify Maven install by Right click on your project -> Run match this screen

confirm-maven

Step 9: Create Feature file:

Right click on /src/main/resources – > New File and save it as .feature extension.

Copy following code and paste it into the file.

Feature to test : We are going to test whether Account is created into the system.

Feature:Test Account Creation
@TestAccountCreation
Scenario: Create Account Page
Given navigate to "https://login.salesforce.com"
Given username "PUR YOUR USERNAME HERE" and password "PUT YOUR PASSWORD HERE"
Then select "/001/o"
Then response should contain "Temp User"

Step 10:

Copy following file and paste in your pom.xml fiel

<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"&gt;
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.sfdc.sec</groupId>
 <artifactId>com.sfdc.sec</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <dependencies>
 <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-java</artifactId>
 <version>1.2.4</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 </dependency>
 <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-core</artifactId>
 <version>1.2.4</version>
 </dependency>
 <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-jvm-deps</artifactId>
 <version>1.0.5</version>
 </dependency>

 <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-junit</artifactId>
 <version>1.2.4</version>
 </dependency>
 <dependency>
 <groupId>org.seleniumhq.selenium</groupId>
 <artifactId>selenium-firefox-driver</artifactId>
 <version>2.48.2</version>
 </dependency>
 </dependencies>
</project>

Step 11:

Create a new Java class with following code


package com.sfdc.sec;

import java.util.concurrent.TimeUnit;

import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

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

@CucumberOptions(tags={"@TestAccountcreation"})
public class CucumberRunner {
 private WebDriver driver;
 private String baseUrl;
 private boolean acceptNextAlert = true;
 private StringBuffer verificationErrors = new StringBuffer();

@Given("^navigate to \"([^\"]*)\"$")
 public void navigate_to(String baseUrl) throws Throwable {
 driver = new FirefoxDriver();
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 driver.get(baseUrl + "/");
 
 }
 
 @Given("^username \"([^\"]*)\" and password \"([^\"]*)\"$")
 public void username_and_password(String username, String password) throws Throwable {
 driver.findElement(By.id("username")).clear();
 driver.findElement(By.id("username")).sendKeys(username);
 driver.findElement(By.id("password")).clear();
 driver.findElement(By.id("password")).sendKeys(password);
 driver.findElement(By.id("Login")).click();
 }
 
 @Then("^select \"([^\"]*)\"$")
 public void select(String arg1) throws Throwable {
 driver.findElement(By.id("Account_Tab")).click();
 driver.findElement(By.name("new")).click();
 driver.findElement(By.id("acc2")).clear();
 driver.findElement(By.id("acc2")).sendKeys("Temp User");
 driver.findElement(By.name("save")).click();
 
 //driver.get("/" + arg1);
 }
 
 @Then("^response should contain \"([^\"]*)\"$")
 public void response_should_contain(String arg1) throws Throwable {
 if (!driver.getTitle().contains("Temp User"))
 throw new AssertionError();
 driver.close();
 }

}

Step 10 :

Right click any where in feature file and Select Run As-> Cucumber Feature