Application Login Feature in Cucumber Framework
Introduction
In this document, we will explore a Cucumber framework implementation for an application login feature. The code provided demonstrates how to define scenarios for both an admin and a user logging into a net banking application. We will discuss the key concepts, code structure, and provide detailed code examples to illustrate the functionality.
Key Concepts
Cucumber is a testing framework that supports Behavior Driven Development (BDD). It allows developers and testers to write test cases in a natural language format, which can be easily understood by non-technical stakeholders. The key components of a Cucumber framework include:
- Feature Files: These files contain scenarios written in Gherkin syntax, which describe the behavior of the application.
- Step Definitions: These are Java methods that implement the steps defined in the feature files.
- Test Runner: This is a class that executes the feature files and reports the results.
Code Structure
The code is organized into three main components:
- Feature File: Defines the scenarios for the application login.
- Step Definitions: Contains the Java methods that execute the steps in the feature file.
- Test Runner: Executes the feature files and integrates with TestNG.
=============================================================
Feature File Example
language-gherkinFeature: Application Login Scenario: Admin Page default Login Given user is on NetBanking Landing Page When user login into application with "admin" and password "1234" Then homepage is displayed And cards are displayed Scenario: User Page default Login Given user is on NetBanking Landing Page When user login into application with "user" and password "0953" Then homepage is displayed And cards are displayed
Scenarios:
Admin Page Default Login
- Given: The user is on the NetBanking Landing Page. This sets the initial context for the scenario.
- When: The user attempts to log in with the username "admin" and the password "1234". This action performs the main functionality being tested.
- Then: Two conditions are verified:
- The homepage is displayed, indicating a successful login.
- Cards are displayed, suggesting that after logging in, the user can access certain features.
User Page Default Login
- Given: Similar to the first scenario, the user is on the NetBanking Landing Page.
- When: The user logs in with the username "user" and the password "0953". This checks the functionality for a regular user account.
- Then: Again, two conditions are checked:
- The homepage is displayed, confirming a successful login.
- Cards are displayed, indicating that the user has access to the application's features after logging in.
Step Definitions Example
language-javapackage stepDefinitions;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class MainSteps {
@Given("user is on NetBanking Landing Page")
public void user_is_on_net_banking_landing_page() {
System.out.println("User landed on netbanking page");
}
@When("user login into application with {string} and password {string}")
public void user_login_into_application_with_and_password(String username, String password) {
System.out.println(username + " and password in " + password);
}
@Then("homepage is displayed")
public void homepage_is_displayed() {
System.out.println("Home page is displayed");
}
@Then("cards are displayed")
public void cards_are_displayed() {
System.out.println("Cards are displayed");
}
}
Code Breakdown
Package Declaration
javaThis line specifies that the class
MainSteps
belongs to thestepDefinitions
package, which helps organize related classes together.Imports
javaThese import statements bring in specific annotations from the Cucumber library, which are used to define the behavior of the user in a readable format.
Class Declaration
javaThis line declares a public class named
MainSteps
where the step definitions are implemented.Step Definitions
Each method annotated with@Given
,@When
, or@Then
corresponds to a step in a Cucumber feature file.Given Step
java- Purpose: This method simulates the scenario where a user is expected to start on the NetBanking landing page.
- Output: Prints a message indicating that the user has landed on the page.
When Step
java- Purpose: This method represents the action where the user logs into the application using provided credentials (username and password).
- Parameters: It takes two parameters (
username
andpassword
), which are passed dynamically when the step is executed. - Output: Prints the username and password used for the login attempt.
Then Steps
Homepage Displayed
java- Purpose: This method checks if the homepage is displayed after a successful login.
- Output: Prints a confirmation message about the homepage display.
Cards Displayed
java- Purpose: This method verifies that certain cards (possibly financial products or options) are displayed on the homepage.
- Output: Prints a message indicating that the cards are shown.
Test Runner Example
language-javapackage cucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(features="src/test/java/features", glue ="stepDefinitions", monochrome=true)
public class TestNGRunner extends AbstractTestNGCucumberTests {
}
Explanation:
Package Declaration:
java- This line declares the package
cucumberOptions
, which is a way to organize Java classes.
- This line declares the package
Imports:
java- These lines import classes from the Cucumber library:
AbstractTestNGCucumberTests
: A base class for running Cucumber tests with TestNG.CucumberOptions
: An annotation used to configure Cucumber options.
- These lines import classes from the Cucumber library:
CucumberOptions Annotation:
java- This annotation configures how Cucumber should run:
features="src/test/java/features"
: Specifies the path to the feature files, which describe the scenarios to be tested.glue ="stepDefinitions"
: Specifies the package containing the step definitions, which provide the implementation for the steps defined in the feature files.monochrome=true
: Enables a more readable console output (removes color codes).
- This annotation configures how Cucumber should run:
Class Declaration:
java- This declares a public class
TestNGRunner
that extendsAbstractTestNGCucumberTests
, making it a test runner for executing Cucumber tests with TestNG. It will use the configurations specified in theCucumberOptions
annotation
Output
When the tests are executed, the output will be as follows:
User landed on netbanking page
admin and password in 1234
Home page is displayed
Cards are displayed
User landed on netbanking page
user and password in 0953
Home page is displayed
Cards are displayed