Waltir
By: Waltir

What Is Behavior Driven Development (BDD)

Cover Image for What Is Behavior Driven Development (BDD)

Behavior-driven development (BDD) is a software development methodology that focuses on the behavior of a system, rather than its implementation. BDD tests are written in a natural language syntax, which makes them easy to understand and maintain. Nightwatch.js is a popular JavaScript-based BDD test framework that allows developers to write BDD tests for web applications.

The BDD syntax in Nightwatch.js is based on the Gherkin language, which is widely used in BDD frameworks such as Cucumber. Gherkin is a plain-text language that uses a specific syntax to define the behavior of a system. The basic structure of a Gherkin scenario is as follows:

Feature: [Feature name]
Scenario: [Scenario name]
Given [initial context]
When [event occurs]
Then [outcome is expected]

The "Feature" keyword is used to define the feature that the scenario belongs to, while the "Scenario" keyword is used to define the scenario itself. The "Given", "When", and "Then" keywords are called "Steps" and they describe the behavior of the system in a clear and concise manner.

For example, the following scenario describes a feature that allows users to log into a web application:

Feature: Login
Scenario: Successful login
Given I am on the login page
When I enter my username and password
Then I am redirected to the dashboard

In this scenario, the "Given" step sets the initial context, the "When" step describes the event that occurs, and the " Then" step describes the outcome that is expected.

Nightwatch.js allows developers to export test cases in multiple format, the most common one is the JSON format. The JSON format is a widely-used data interchange format that is easy to read and write. It is also easily parseable by most programming languages.

For example, the following is an example of a test case exported in JSON format:

{
    "name": "Login",
    "module": "login",
    "tests": [{
            "name": "Successful login",
            "steps": [
                {
                    "name": "I am on the login page"
                }, {
                    "name": "I enter my username and password"
                }, {
                    "name": "I am redirected to the dashboard"
                }
            ]
        }]
}

In this example, the test case has been exported with a "name" field, a "module" field, and an array of "tests" that contains the steps of the scenario.

In summary, BDD tests are written in a natural language syntax and use the Gherkin language to define the behavior of a system. Nightwatch.js is a popular BDD test framework for web applications that allows developers to write BDD tests in JavaScript. Nightwatch.js also allows developers to export test cases in multiple formats, including JSON. The BDD syntax is more human-readable and easy to understand, while the JSON format is more machine-readable and easy to parse.

More Posts

Cover Image for Blocking Ad Traffic In Nightwatch JS
Blocking Ad Traffic In Nightwatch JS
Waltir
By: Waltir

Example showing how you can block unwanted ad traffic in your Nightwatch JS tests....

Cover Image for Blocking Ad Traffic In Cypress
Blocking Ad Traffic In Cypress
Waltir
By: Waltir

Example showing how you can block unwanted ad traffic in your Cypress tests....

Cover Image for Three Ways To Resize The Browser In Nightwatch
Three Ways To Resize The Browser In Nightwatch
Waltir
By: Waltir

Outlining the three different ways to resize the browser in Nightwatch JS with examples....

Cover Image for Happy Path VS Sad Path Testing
Happy Path VS Sad Path Testing
Waltir
By: Waltir

As a test engineer it is crucial that both happy path and sad path use cases have been considered and fully tested...