# Behavior Driven Development

# What is Behavior Driven Development(BDD)

Behavior Driver Development (BDD) is a development approach based on Test Driven Development (TDD). In order to facilitate better communication between technical and non-technical teams a shared language was developed. Test are written in a common language that should be understood by everyone involved, (for example English). For more about BDD refer here

# Using BDD with Gondola

Feature files and step definition files are required for BDD with Gondola

# Features

Feature files are written in Gherkin. They are stored in the src/features folder in the root of the project. If it is not already there you must create it. In addition, the following must added to the gondola.json file.

  "gherkin": {
      "features": "./features/**/*.feature"
    }

Here is a sample feature file:

Feature: Car Reservation
    Car Reservation Feature

Scenario: Common Reservation
    Given I am logged in as "john"
    When I rent a "Ford Mustang Coupe" for 2 day
    And I select option "N/A"
    Then I should see the total payment is 310.00

Scenario: Reservation with Infant Seat
    Given I am logged in as "john"
    When I rent a "Toyota Prius" for 1 day
    And I select option "Infant Seat"
    Then I should see the total payment is 140.40

# Step Definition

Step definition files are typescript files which map the Given-When-Then in the feature files to test functions. They are stored in the src/step_definitions folder. If it is not already there you must create it. For more on writing steps refer here Step definition files must be added to gondola.json.

"gherkin": {
      "features": "./features/**/*.feature",
      "steps": [
        "./built/step_definitions/loginSteps.js",
        "./built/step_definitions/reservationSteps.js"
      ]
    }

Here is a sample step definition file:

import {gondola} from "gondolajs";
import loginPage from "../pages/loginPage";
import * as users from "../../data/users.json";
import * as carReservations from "../../data/carReservations.json";
import homePage from "../pages/homePage";
import orderPage from "../pages/orderPage";

Given("I am logged in as {string}", async (userName: string) => {
    const user = users.default[userName];
    if (user) {
        await loginPage.login(userName, user.password);
    } else {
        throw new Error(`Test data doesn't contain user '${userName}'`);
    }
});

When("I rent a {string} for {int} day", async(carName: string, rentedDays: number) => {
    await homePage.openNewOrder();
    await orderPage.enterPickUpInfo(
                carReservations.default.default.location.pickup);
    await orderPage.enterReturnInfo(
                carReservations.default.default.location.return);
    await orderPage.selectCar(carName);
    await orderPage.setRentedDays(rentedDays);
    await orderPage.enterCustomerInfo(
                carReservations.default.default.customer.firstName,
                carReservations.default.default.customer.lastName,);
});

When("I select option {string}", async(option: string) => {
    await orderPage.selectOptions(option);
    await orderPage.selectPrice();
});

Then("I should see the total payment is {float}", async(price: number) => {
    await orderPage.checkTotalPrice(price.toFixed(2));
});

# Data Files

To use a json file for automated testing with BDD you must import it in the header of the step definition file like so

import * as users from "../../data/users.json";

These files are stored in the data folder in the project root. If it is not there, you must create the folder. Here is an sample data file:

{
    "john":{
        "firstName":"john",
        "lastName":"doe",
        "password":"",
        "driverLicense":"CA123456"
    },
    "invalid":{
        "password":"invalid"
    }
}
Last Updated: 7/25/2019, 8:19:11 AM