Selenium and Cucumber

What is Cucumber?
Cucumber is a tool invented based on BDD concept: Software development/testing should be managed by both business interests and technical insight.

To make a tool easy to understand by non-technical people, we need to add a layer or an interface for product/business people so that they can contribute. Thus the born of Cucumber.

Cucumber allows expected software behaviors to be specified in a logical language(Gherkin) that customers, product team and technical team can all understand. Thus makes it possible for all parties of a project to participate/understand testing.

How Cucumber Work?

Cucumber has 2 layers:

  • business-facing Feature file, which is written in simple plain-English like syntax called Gherkin, and can be easily understood by business and product people in the project. It is at this layer that all parties, including non-technical people , can contribute to the testing.
  • Technical-facing step definition where the actual automation coding happens.

business-facing: Feature file
Feature file spells out feature description, scenarios, and steps in Gherkin syntax using ‘Given … When … Then …” keywords.

Here is a sample feature file:

Feature: Search
Scenario: User shall be able to search the content they are interested
Given I open the browser
When I go to automeditator.com
Then I can search by inputting keyword there

All parties in the project, namely product team, dev team and QA team, can all contribute to this component, and this file is easier for management to understand and trace what are tested and how test is done.

Technology-facing: Step Definitions
This is where actual automation coding is written. The Step Definitions essentially convert the simple English “Steps” specified in the feature file into code that can be executed to against the Application under Test.

It is exactly here where Selenium kicks in to play a role in cucumber automation. All the actual dirty work can be done via selenium framework.

How Selenium works together with Cucumber?
Cucumber’s step Definitions can utilize Selenium framework to interact with the web application under test, and Association of step definition with the actual automation code is done through Tagging as following:

@When “^user logs in using Username as \”([^\”]*)\” and Password as \”([^\”]*)\”$”
public void I_enter_Username_as_and_Password_as(String arg1, String arg2) {
driver.findElement(By.id(“email”)).sendKeys(arg1);
driver.findElement(By.id(“pass”)).sendKeys(arg2);
driver.findElement(By.id(“u_0_v”)).click();
}
@Then”^login should be unsuccessful$”
public void validateRelogin() {
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();
}