How to achieve Advanced BDD Test Automation
By Sourojit Das, Community Contributor - September 27, 2022
Software testing has become an extremely integral part of the SDLC over time. Modern processes like DevOps incorporate CI/CD pipelines that require new solutions to old problems. Reduction of turnaround times, shifting left, and creating easily understandable and replicable tests are the need of the hour.
Behaviour-driven development (BDD) is integrated extensively into the Agile method of software development and has even complimented existing TDD methodologies.
Current market research forecasts an increased adoption of BDD Tools all the way till 2029, and major companies like IBM, Wells Fargo, and CitiBank have become early and enthusiastic adopters of this philosophy.
Predicted adoption of BDD over the years
In such a scenario, it is imperative to leverage the power of BDD to achieve advanced test automation, which this article sets out to do.
- Achieving Advanced BDD Automation
- 1. Shift left for BDD Automation and write test scripts as early as possible
- 2. Write scenarios optimally for test automation
- 3. Use Platforms that allow framework collaboration
- 4. Make your test cases more modular and reusable
- 5. Boost the power of BDD Test Automation with parallelization
- 6. Leverage Data Tables to store data for automation and use a background in your scenarios
- 7. Mapping BDD Scenarios with ALM Tool Requirements
- 8. Generate automated test reports from BDD Test Automation
Achieving Advanced BDD Automation
There are no silver bullets to achieving advanced BDD Automation, just a few simple steps that need to be followed in a planned and consistent manner.
1. Shift left for BDD Automation and write test scripts as early as possible
Anyone with even the slightest idea of the SDLC can agree that the cost of fixing a bug increases with every step of the process. This is why requirements are KING when it comes to BDD automation. For a process so dependent on testing the “behaviour” of an application, it is imperative to understand the requirements as clearly as possible and to convert them into usable test cases. That is why the Shift Left Testing approach is highly recommended for BDD Automation.
Advanced BDD Test Automation requires closely coupled communication between three major stakeholder categories popularly known as the “Three Amigos”. And it is vital to clear potential misunderstandings at the very beginning.
- Business Team (BA): This team has the Product Owner or a Business Analyst who defines what is the problem to be solved through the product.
- Development Team (DevOps): This team has developers who provide solutions for the problem in question.
- QA Team: These are the testers who find bugs in the product and verify whether it works as expected, and identify any edge cases that can lead to product failure.
2. Write scenarios optimally for test automation
Once the requirements have been clearly understood, it is time to turn them into test scenarios. In BDD, test scenarios are written using the Gherkin language, which uses the “Given-When-And-Then-But” style for writing test cases.
However, since these scenarios are written in plain English, they may sometimes become too convoluted and too lengthy for easy conversion to automated test cases.
Scenario: As an existing user, I want to log in successfully Given the user is on the Home page When the user clicks on the login link on Home Page And the user can see the login screen And the user enters his username and password And the user is able to click on the login button Then the user is logged in successfully And the successful login message is displayed
A far better way is to write these scenarios in a more concise manner with fewer statements overall. This increases clarity and makes the automation steps easier to replicate.
Scenario: As an existing user, I want to log in successfully. Given the user is on the Home page When the user navigates to the Login page And the user enters the username and password Then the successful login message is displayed
3. Use Platforms that allow framework collaboration
Popular automation test frameworks like Selenium and Cypress are preferred for their accuracy and the test execution speed they provide to the overall development cycle. BDD test frameworks like Cucumber adds value to these by allowing for greater collaboration between technical and non-technical stakeholders.
This can be especially beneficial in User Acceptance Testing, which has test scenarios driven by user behaviour.
It is recommended to run Cucumber-Selenium tests on real browsers and devices. Browserstack’s Cloud Selenium Grid of 3000+ real browsers and devices offers unprecedented opportunities to test applications on real devices for greater accuracy.
It is important to tie in conventional BDD frameworks with popularly used automation test frameworks to achieve an ideal combination of speed, accuracy, and behaviour-driven validation.
4. Make your test cases more modular and reusable
Automation tests are often used for repetitive tasks, especially when it comes to regression testing. Thus it is important to make the cases as modular and reusable as possible.
For example, “Given: user navigate to the Shopping Cart” can be one common step that we need in every scenario. So we can reuse this step.
Also, it prevents confusion due to different letter-casing if we try to use it again and again manually.
Given (“Click on ‘Sign In link’ on the Home Page”) Given (“click on ‘Sign In link’ on the Home Page”)
5. Boost the power of BDD Test Automation with parallelization
Tools like BrowserStack Automate allow Cucumber-based Espresso tests to be run in parallel as well as sequentially. By default, the test-suite contents are executed in sequence, but these can be further split into different groups called shards for parallel execution.
This is especially helpful when there are a large number of automated test cases in the test suite.
For example, if the test suite has 500 test cases, and they take a minute to execute, the sequential test execution time will be approximately 500 mins However, splitting this into 10 shards of 50 test cases each can allow them to be run in parallel in just 50 mins.
Note: To use test sharding on Cucumber-based Espresso tests, check out the Cucumber-based Espresso test sharding on BrowserStack
6. Leverage Data Tables to store data for automation and use a background in your scenarios
It is best practice to put the steps that are commonly repeated in every scenario into the background as they are run before every scenario. Also, Gherkin provides for the use of Data Tables to store data for parameterization. This helps in easily creating automated test cases, especially for Regression Testing.
Feature: I want to login into the site with valid and invalid data Background: Given I navigate to the Website Scenario: Login as a new sign-up user with valid data When I entered a valid credential | email | validpassword | | qatubeupdate@yopmail.com | 12345 | When the user clicks on the sign-in button Then Validate the title after login Scenario: Login with invalid data by entering an invalid password When I entered an invalid credential | email | invalidpassword | | qatubeupdate@yopmail.com | 123456 | When the user clicks on the sign-in button Then Error message should display | errormessage | | Authentication failed |
7. Mapping BDD Scenarios with ALM Tool Requirements
For better automated test coverage, it is always important to ensure that all the requirements are mapped to test cases. This is commonly known as Requirements Coverage.
User Stories can be integrated by ID into BDD scenarios for the direct mapping of business requirements to test cases and subsequently into automated test scripts. Cucumber Scripts can map User Story ID from JIRA, as shown below.
Mapping User Stories to BDD Scripts
8. Generate automated test reports from BDD Test Automation
The CI/CD pipeline requires rapid feedback cycles in order to correct bugs and other potential issues and get a deployable product ready for production at the earliest. Leveraging the power of automated test reporting for BDD Test Automation goes a long way in ensuring this.
Tools like BrowserStack App Automate offers automated test reports for Cucumber-based Espresso tests, for example. The specific format of the report can be customized by the user in the plugins parameter and these reports can be downloaded in a PDF version as well.
To Sum Up
BDD Test Automation allows significant returns in terms of your Automation ROI and is fashioned in a manner that is convenient for all stakeholders to understand and stand over. Advanced BDD Automation Tests can be written with natural language constructs to define the behaviour of software for given scenarios while meeting the demand for accelerated product release and optimal quality.
Testing websites on a real device cloud is the only sure-shot way of ensuring complete test result accuracy. It is best to thus opt for real device testing to check cross-browser compatibility while executing a BDD Test Automation suite.