BDD is a popular concept in agile testing nowadays. BDD uses natural language to describe test scenarios, and members of various roles in a team communicate with each other through scenes described in natural language, thus improving the efficiency of collaboration between teams. Cucumber is an excellent FRAMEWORK based on BDD.

As a popular test automation framework, Cucumber uses an easy-to-understand syntax and supports a variety of natural languages to describe test cases. This article explains “How Cucumber works” by introducing the core architecture and implementation details of Cucumber. That’s the question.

Cucumber Project structure

The structure of a Cucumber project is roughly as follows:

It includes the following parts:

Feature file (Script file)

The running files of test items are in the features directory, and the script files ending with.feature are script files. A script file can contain multiple scenarios, and a scenario contains multiple operation steps.

Here is a simple sample feature file

# language: zh-CNFunction: Search function Searches for specified keywords, and the search results should contain these keywords. Scenario: Bing search if the browser navigates to"http://www.bing.com"

Copy the code

The feature file contains a “Bing search” scenario, which describes the general operation process of bing search. According to the documentation, anyone can understand what to do.

The business is put together using the.feature playbook file to illustrate the business of the test with scenario descriptions and steps.

Step Definitons

For the service steps described in the. Feature file to run, the operation behavior needs to be defined according to the service scenario. These automation scripts are defined in the Step_Definitions directory. For example, the steps are to generate a pair of Cucumber automation code samples according to this scenario:

Given(/^ Browser navigation to"([^"] *)"$/, async function(url) { await driver.get(url); });Copy the code

With these code samples, you just need to automate each of these operations. These automated codes are typically stored in the Step_definitions directory.

Support Code

During the execution of automatic scripts, for example, the driver mentioned above, as the browser driver, needs to be abstracted and stored in a separate directory, namely support. In this directory, you can also define the timeout period for each operation step and the runtime Settings before and after the scenario execution.

Cucumber Test Kit

The following is the mapping between the Cucumber test suite feature file and the automation script file, starting with (1) and containing scenario (2) and step (3). (4) Define the call steps of the scene, and each operation step corresponds to an automated implementation code.

The figure above is an example of a simple Cucumber.js automation test in a CukeTest development environment, where the base code in this step definition is JavaScript and the automation library is Selenium-WebDriver. CukeTest is an integrated development environment for developing cucumbe.js. One important reason for the wide adoption of Cucumber. Js is the widespread popularity of Node.js. Another reason is that JavaScript is fully supported by a wide range of automation tools, especially Node.js (such as Appium, Selenium, Puppeteer, etc.). These tools cover automation for a wide variety of applications, including Web, Android, iOS, API, Windows, and more.

perform

Cucumber’s play files and corresponding step definitions are located in a specific folder structure, as shown below

Cucumber (Play files) are in the Features folder and step definition files are in the step_Definitions folder.

Cucumber is a command line tool and to perform tests you must navigate to the parent folder (the “demo folder” in the above example) and then perform tests by referencing locations in the “Features” folder. If you want to edit and debug Cucumber scripts in the development environment, you can use the visual editing tool CukeTest, which provides a visual feature editing interface, one-click generation of automated code samples, various test reports, and can be integrated with various continuous integration tools.

$ cd Desktop/Demo_Folder/
$ ./node_modules/.bin/cucumber.js features/google_Test.feature
Copy the code

This will execute the feature file and the corresponding code in the step definition file. If the step is executed without error, Cucumber proceeds to the next step in the scenario. If it reaches the end of the scene without any steps raising an error, the scene is marked as passed.

If any step in the solution fails, Cucumber flags the solution as a failure and moves on to the next solution. As the scene runs, Cucumber displays the results, allowing you to see exactly how each step is performing.

conclusion

Cucumber is now being used by more teams and Cucumebr is being documented in the community. Cucumber code editing tools are getting better and better. If you are good at English, you can read Cucumber’s official documentation at github.com/cucumber/cu… Learn more about Cucumber.