
Gherkin est une syntaxe spéciale utilisée dans le BDD (Behavior Driven Development) pour écrire les critères d’acceptation.Gherkin utilise un ensemble de mots-clés pour donner une structure et une signification aux spécifications.
Les fichiers utilisés pour stocker Gherkin sont appelés feature , car chaque fichier est généralement utilisé pour tester une fonctionnalité.
Gherkin utilise des mots-clés spécifiques comme : Feature, Scenario, Example, Given, When, and Then.
Voici un exemple écrit en Gherkin, chaque ligne commence par un mot-clé suivi d’un texte descriptif :
Feature: Google Searching
As a web surfer, I want to search the best football team in france Google.
Background:
Given a web browser is on the Google page
Scenario: Simple Google search
Given a web browser is on the Google page
When the search phrase "olympique de marseille" is entered
Then results for "olympique de marseille" are shown
And the related results include "champions league 1993"
But the related results do not include "Paris Saint-Germain"
Feature, utilisé pour décrire le but du scenario.Background permet d’ajouter un contexte aux scénarios, et il s’exécute avant chaque scénario.Given utilisé pour décrire le contexte initial du système.When décrit un événement ou une action.Then utilisé pour décrire le résultat attendu.And , utilisée pour combiner plus d’une action/résultat.But, utilisée très rarement pour écrire les résultats négatifs du scénario.Given, une seule When et une seule Then. And maximum.Cucumber est un framework de tests BDD, basé sur la syntaxe Gherkin, le but c’est écrire des tests d’acceptation avec une syntaxe compréhension.
Eclipse marketplace.Maven .Cucumber, JUnit et Selenium dans pom.xml.src/ | main/ | test/ | | | java/ | | | | base/ | | | | | -BaseUtil.java | | | | runners/ | | | | | -RunCucumberTests.java | | | | steps/ | | | | | -Steps.java | | | resources/ | | | | - Login.feature .classpath .project pom.xml
Tous les fichiers .feature doivent être placés dans le dossier resources.
Voici un exemple d’un fichier .feature :
Feature: Login Functionality
In order to do internet banking
As a valid Para Bank customer
I want to login successfully
@Login
Scenario: Login Successful
Given I am in the login page of the Para Bank Application
When I enter valid credentials
Then I should be taken to the Overview page
Exemple :
public class Steps {
private WebDriver driver;
@Given("I am in the login page of the Para Bank Application")
public void i_am_in_the_login_page_of_the_Para_Bank_Application() {
System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");
// instance de driver google chrome
driver = new ChromeDriver();
driver.get("http://parabank.parasoft.com/parabank/index.htm");
}
@When("I enter valid credentials")
public void i_enter_valid_credentials() {
driver.findElement(By.name("username")).sendKeys("tautester");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.name("username")).submit();
}
@Then("I should be taken to the Overview page")
public void i_should_be_taken_to_the_Overview_page() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='rightPanel']/div/div/h1")));
driver.findElement(By.xpath("//*[@id='rightPanel']/div/div/h1")).isDisplayed();
driver.findElement(By.linkText("Log out")).click();
driver.quit();
}
}
Pour exécuter les scénarios, nous devons créer la classe Runner :
package com.siraj.runners;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(glue={"com.siraj.steps"}, features = "src/test/resources", plugin = { "pretty", "html:target/site/cucumber-pretty",
"json:target/cucumber.json" })
public class RunCucumberTests {
}
Ensuite lancez la commande pour éxecuter les scénarios :
mvn verify
Il existe de nombreux plugins pour générer des rapports, cucumber-reporting est parmi les plugins les plus utilisés :
Le rapport sera généré sous format html dans le dossier Target du racine

Liens rapides
Politique