Cucumber

1 - About

Cucumber is a Behavior Driven Design (BDD) framework that takes “stories” or scenarios written in human readable languages such as English and turns those human readable text into a software test. This makes what is called an “Executable Specification”.

3 - Example

Example based on Selenium

Scenario: Finding some cheese
  Given I am on the Google search page
  When I search for "Cheese!"
  Then the page title should start with "cheese"

Translate into:

package com.example;
 
public class ExampleSteps {
    private final WebDriver driver = new FirefoxDriver();
 
    @Given("^I am on the Google search page$")
    public void I_visit_google() {
        driver.get("https://www.google.com");
    }
 
    @When("^I search for \"(.*)\"$")
    public void search_for(String query) {
        WebElement element = browser.findElement(By.name("q"));
        // Enter something to search for
        element.sendKeys(query);
        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();
    }
 
    @Then("^the page title should start with \"(.*)\"$")
    public void checkTitle() {
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
          public Boolean apply(WebDriver d) {
          return d.getTitle().toLowerCase().startsWith("cheese");
          }
        });
        assertThat(driver.getTitle(), startsWith("cheese"));
        // Should see: "cheese! - Google Search"
    }
 
    @After()
    public void closeBrowser() {
        driver.quit();
    }
}
code/test/cucumber.txt · Last modified: 2017/11/18 19:23 by gerardnico