In the last Selenium lesson, we presented Selenium Grid, a platform for distributed test execution that enhances the pace of test runs.
This time, concluding our in-depth Selenium training sequence, we will examine advanced Selenium testing and corresponding notions.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
We will bring to your attention Cucumber, a Behavior Driven Development (BDD) framework in the following tutorials that works in tandem with Selenium to carry out acceptance testing.
Tutorial Contents:
Cucumber’s Introduction
Using the Behavior Driven Development (BDD) framework, Cucumber is a tool that enables the creation of acceptance tests for web applications. It facilitates the automation of functional validation in a layout that is quickly readable and comprehensible to Business Analysts, Developers, Testers, and other stakeholders, and it resembles everyday English language.
Cucumber feature files can act as all-encompassing documentation for all parties involved. Other tools supportive of BDD such as JBehave also exist; however, Cucumber is extensively recognized for its adaptability. Initially devised in Ruby, Cucumber was afterward expanded to aid a Java framework. Both tools cater for native JUnit support.
Behavior Driven Development is an extension of Test Driven Development (TDD) that emphasizes the testing of the overall system behavior instead of discerning code components. Moving forward, we’ll discuss the concepts of BDD and demonstrate how BDD tests are written.
Furthermore, integration with other testing frameworks such as Selenium, Watir, and Capybara is possible with Cucumber. Moreover, Cucumber backs numerous programming languages including but not limited to Perl, PHP, Python, and .NET. However, this tutorial will concentrate on exploiting Cucumber with Java.
Key Concepts of Cucumber
To fully grasp Cucumber, we need to get to know its core functionalities and how they are utilized.
#1) Feature Files:
These critical components of Cucumber are used to write test automation steps or acceptance tests. They can also double as living documentation. The steps detailed in these files denote the application’s functionalities. Each feature file is required to have the .feature
extension.
Example of a feature file:
Feature: Login Feature In order to verify Login functionality, I intend to run the cucumber test Scenario: Login Given user is on SOFTWARETETINGHELP.COM When user logs in with Username as "USER" and Password "PASSWORD" Then login should be successful Scenario: Login Given user is on SOFTWARETETINGHELP.COM When user logs in with Username as "USER1" and Password "PASSWORD1" Then an error message should display
#2) Feature:
The Feature segment provides details about the high-level business functionality, as displayed in the previous sample. It determines the goal of the application targeted by the test. The objective of the feature file should be subsequently clear by reading the first Feature step. This section’s message should be succinct.
#3) Scenario:
A Scenario is a representation of specific functionality under testing. The intent behind the scenario and the objective of the test should be immediately comprehensible for the reader. Each scenario has to stick to the format of Given, When, Then. This distinct language style is labeled as “gherkin”.
- Given: It indicates preconditions or the known status of the system.
- When: It suggests the action to be executed. In the prior example, it pertains to the user attempting to log in with their username and password.
- Then: It points out the expected result or outcome. For example, verifying the successful login or showcasing an error message.
- Background: It includes steps that should be executed prior to each scenario. For instance, if a user is required to clear the database before each scenario, those steps should be placed in the Background section.
- And: It is used to combine multiple steps of the same type.
Example:
Feature: Login Feature Scenario: Login Given user is on SOFTWARETESTINGHELP.COM When user logs in with Username as "USER" And password as "password" Then login should be successful And User should see the Home page Example of Background: Background: Given user is logged in as database administrator And all the irrelevant values are cleared
#4) Scenario Outline:
Scenario outlines come into the picture when the same test needs to be done with differing data sets. A common example comes in the form of testing the login functionality with multiple pairs of usernames and passwords.
Feature: Login Feature To verify Login works, I intend to run the cucumber test Scenario Outline: Login Given user is on SOFTWARETESTINGHELP.COM When user logs in using Username as <username> and Password <password> Then login should be successful Examples: |username |password | |Tom |password1 | |Harry |password2 | |Jerry |password3 |
Note:
- The preceding example exemplifies providing column names as parameters to the When statement.
- Instead of the Scenario keyword, Scenario Outline is exploited.
- Examples are utilized to provide varying arguments in a tabular format. Vertical pipes are employed to divide diverse columns. A single example can encompass multiple columns.
#5) Tags:
By default, Cucumber executes all scenarios in all feature files. Nonetheless, in real-world projects, there might be hundreds of feature files that don’t need to be executed every time. A universal example of this is feature files linked with smoke tests that are not needed for each run. To counteract this, tags can be applied in feature files to define which tests should be executed.
Example implementing singular tags:
@SmokeTest Feature: Login Feature To verify Login works, I intend to run the cucumber test Scenario Outline: Login Given user is on SOFTWARETESTINGHELP.COM When user logs in using Username as <username> and Password <password> Then login should be successful Examples: |username |password | |Tom |password1 | |Harry |password2 | |Jerry |password3 |
Example demonstrating multiple tags:
In the succeeding example, the same feature file is utilized for both smoke tests and login tests. Either the @smokeTest or @LoginTest tag can instruct Cucumber to run distinct scenarios.
@SmokeTest @LoginTest Feature: Login Feature To verify Login works, I intend to run the cucumber test Scenario Outline: Login Given user is on SOFTWARETETINGHELP.COM When user logs in using Username as <username> and Password <password> Then login should be successful Examples: |username |password | |Tom |password1 | |Harry |password2 | |Jerry |password3 |
In the same vein, tags can be designated to execute particular scenarios within a feature file. The example that follows illustrates how to execute a specific scenario employing tags.
Feature: Login Feature To verify Login works, I intend to run the cucumber test @positiveScenario Scenario: Login Given user is on SOFTWARETETINGHELP.COM When user logs in using Username as "USER" and Password "PASSWORD" Then login should be successful @negaviveScenario Scenario: Login Given user is on SOFTWARETETINGHELP.COM When user logs in using Username as "USER1" and Password "PASSWORD1" Then error message should be displayed
Setting Up a Cucumber Project
The comprehensive guide on creating a Cucumber project is discussed separately in the upcoming lesson. Please refer to the tutorial that is Part 2 of our Cucumber training for more details on setting up the project. It’s worth mentioning that Cucumber does not necessitate any additional software installations.
Applying the Feature file:
In order to test the feature files, we must put the steps into practice in Java. This will involve developing a class containing the Given, When, and Then instructions. Cucumber is reliant on its annotations, and all the steps are entwined within these annotations. Every step opens with the caret symbol (^), gesturing for the dawning of the step, and closes with the dollar symbol ($). Different test data can be passed with regular expressions. How parameters are ordered depends on how they are transferred from the feature file. For more information about organizing the project and aligning feature files with Java classes, please consult the next lesson.
Example:
The next example displays how feature files can be implemented:
In this example, no Selenium API was employed. The goal was to demonstrate how Cucumber functions as a solitary framework. For integration with Selenium WebDriver, reference to the next lesson is recommended.
public class LoginTest { @Given("^user navigates to SOFTWARETETINGHELP.COM$") public void navigatePage() { System.out.println("Cucumber executed Given statement"); } @When("^user logs in using Username as "(.*)" and Password "(.*)"$") public void login(String usename, String password) { System.out.println("Username is: " + usename); System.out.println("Password is: " + password); } @When("^click the Submit button$") public void clickTheSubmitButton() { System.out.println("Executing When statement"); } @Then("^Home page should be displayed$") public void validatePage() { System.out.println("Executing Then statement"); } @Then("^login should be successful$") public void validateLoginSuccess() { System.out.println("Executing 2nd Then statement"); } }
When the Cucumber runner class is executed, Cucumber will read the steps stipulated in the feature file. For instance, on running the @smokeTest tag, Cucumber will examine the Feature step and the Given directive in the Scenario. Upon encountering the Given directive, Cucumber will search for the exact step in your Java files. If the step is discovered, Cucumber will execute the relevant function. If the step is not found, it will be overlooked.
Final Thoughts
In this lesson, we have explored the facets and practical application of the Cucumber tool. Cucumber stands out as a favorite selection for many projects due to its simplicity, readability, and business functionality support.
In our next lesson, we will assist you in setting up a Cucumber-Java project and amalgamating Selenium WebDriver with Cucumber.