Cucumber
Cucumber
Example
If we are developing a user authentication feature, then the following can
be few key test scenarios, which needs to get passed in order to call it a
success.
The user should be able to login with correct username and correct password.
The user should not be able to login with incorrect username and correct
password.
The user should not be able to login with correct username and incorrect
password.
How it Works
By the time the code is ready, test scripts are ready too. The code has to
pass the test scripts defined in BDD. If it does not happen, code
refactoring will be needed. Code gets freezed only after successful
execution of defined test scripts.
It is a very simple notion, but what we need in order to get this concept
implemented. The answer is, Behavior Driven Development (BDD)
Framework. Cucumber is one such open source tool, which supports
behavior driven development. To be more precise, Cucumber can be
defined as a testing framework, driven by plain English text. It serves as
documentation, automated tests, and a development aid – all in one.
Cucumber reads the code written in plain English text (Language Gherkin
– to be introduced later in this tutorial) in the feature file (to be
introduced later).
It finds the exact match of each step in the step definition (a code file -
details provided later in the tutorial).
This has become the reason for Cucumber's popularity over other
frameworks, like JBehave, JDave, Easyb, etc.
Ruby on Rails
Selenium
PicoContainer
Spring Framework
Watir
Features
A Feature can be defined as a standalone unit or functionality of a
project. Let’s take a very common example of a social networking site.
How does the feature of this product/project look like? Few basic features
can be determined as −
Create and remove the user from the social networking site.
User login functionality for the social networking site.
Sharing photos or videos on the social networking site.
Sending a friend request.
Logout.
The user should be able to login into the social networking site if the
username and the password are correct.
The user should be shown the error message if the username and the
password are incorrect.
Feature Files
The file, in which Cucumber tests are written, is known as feature files.
It is advisable that there should be a separate feature file, for each
feature under test. The extension of the feature file needs to be
“.feature”.
For Example −
The naming convention to be used for feature name, feature file name
depends on the individual’s choice. There is no ground rule in Cucumber
about names.
Example
Feature − User login on social networking site.
The user should be able to login into the social networking site when the
username and the password are correct.
The user should be shown an error message when the username and the
password are incorrect.
The user should be navigated to the home page if the username and the
password are correct.
Steps Definitions
We have got our feature file ready with the test scenarios defined.
However, this is not the complete job done. Cucumber doesn’t really
know which piece of code is to be executed for any specific scenario
outlined in a feature file.
driver.navigate().to("https://www.facebook.com/");
driver.findElement(By.id("email")).sendKeys(arg1);
driver.findElement(By.id("pass")).sendKeys(arg2);
driver.findElement(By.id("u_0_v")).click();
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test Pass");
} else {
System.out.println("Test Failed");
driver.close();
So with each function, whatever code you want to execute with each test
step (i.e. GIVEN/THEN/WHEN), you can write it within Step Definition
file. Make sure that code/function has been defined for each of the steps.
This function can be Java functions, where we can use both Java and
Selenium commands in order to automate our test steps.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Step 6 − Add dependency for Selenium − This will indicate Maven, which
Selenium jar files are to be downloaded from the central repository to
the local repository.
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
Step 7 − Add dependency for Cucumber-Java − This will indicate Maven,
which Cucumber files are to be downloaded from the central repository to
the local repository.
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
Step 9− Add dependency for JUnit − This will indicate Maven, which
JUnit files are to be downloaded from the central repository to the local
repository.
Create one more dependency tag.
Provide the following information within the dependency tag.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Feature: CucumberJava
package CucumberJava;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
driver.navigate().to("https://www.facebook.com/");
if(driver.findElement(By.id("u_0_v")).isEnabled()) {
System.out.println("Test 1 Pass");
} else {
System.out.println("Test 1 Fail");
driver.close();
}
package cucumberJava;
import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;
@RunWith(Cucumber.class)