Nowadays, the majority of applications written as web-based applications run on a browser. Test Automation uses software tools to run repeatable tests for these web applications. There are many advantages of test automation, including its ability to increase test speed and efficiency, and improve test accuracy. Test-driven development is the technique of using automated unit tests to implement test coverage. Behavior driven development (BDD) is a branch of Test-driven development (TDD) that uses human-readable language as user requirements for software tests. The Cucumber framework is one of the BDD frameworks in the market.
Cucumber is a testing framework that supports Behavior Driven Development (BDD). It is written in plain English text called Gherkin. It is defined as a scenario of inputs, actions and outcomes. Gherkin interprets human input into the software concept of input/process and actions. The following is how you write a Cucumber feature file in the Gherkin Language. Each test is written in a feature file and has one or more scenarios in it. Behavior driven development (BDD) is a good idea according to Agile methodology and User Acceptance Testing (UAT). In the following example, we show how to test a Facebook login functionality.
Feature: Each feature file begins with ‘Feature’ keyword. Every feature file should have a feature extension. It gives a summary of what you will be testing.
Scenario: Each feature file contains one or more multiple scenarios. Each test is called a scenario followed by three parts.
GIVEN: Defines precondition for the test. For example, if you want to verify that a company logo is displaying, the precondition to verify the logo is displaying would be that the user is at the Home Page.
WHEN: Defines that an action is performed by the user.
THEN: Defines the outcome of the previous steps.
AND: If you have multiple ‘WHEN,’ you can use ‘AND.’
Here is an example:
Feature: Home Page Test
Scenario: Facebook Login Functionality Test
Given I have a Firefox browser running
When I go to the Facebook.com
And I enter valid Email as “firstname.lastname@example.org”
And I enter valid password as “JohnD123”
And I should click login button
Then I should see the Facebook logo on the top left
Step definitions are Java methods with a set of Java code that links to one or more Gherkin feature files. When a feature file is executed, it looks for a matching “step definitions” step.
A Cucumber runner class is one of the many mechanisms used to run a Cucumber feature file. It uses the Junit runner class to run the files.
- Runner class acts as a link between the step definition class and the feature files. It is a class where you provide the path for both feature files and step definitions.
- With the Runner class you have the option to run either one single feature or multiple feature files.
- @RunWith annotations: This is a Junit annotation that specifies which runner has to run execute this class. Cucumber.class is a parameter. Junit knows that this is a Cucumber test.
- @CucumberOptions: This is an important annotation to run a Cucumber feature file. features is a parameter that provides the path of the Feature file. Glue, another parameter, provides the path of the step definitions class. plugin, generates an HTML report on the provided location.
When we execute Cucumber scenarios, it automatically generates a report for the passed and failed scenarios. It generates two types of reports: an HTML and a Json report. Also, by adding a feature called extent reports, Cucumber can generate good-looking HTML reports.
Cucumber is a very good BDD tool that provides testers a lot of flexibility and has large community support. It’s not helpful to use the Cucumber framework when step definitions grow a lot because it gets challenging to manage them.
In conclusion, using the Cucumber framework is a good idea since it allows functional validation in an easy and understandable format (plain English) to Business Analysts, Developers, Testers. Cucumber feature files can serve as good documentation for future references. Tune in again as we delve deeper into the uses of the Cucumber framework!