Explanation of feature, StepIdentifier, TestNGRunner file

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-gherkin
Explain
Feature: 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:

  1. 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.
  2. 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-java
Explain
package 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

  1. Package Declaration

    java
    package stepDefinitions;

    This line specifies that the class MainSteps belongs to the stepDefinitions package, which helps organize related classes together.

  2. Imports

    java
    import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When;

    These import statements bring in specific annotations from the Cucumber library, which are used to define the behavior of the user in a readable format.

  3. Class Declaration

    java
    public class MainSteps {

    This line declares a public class named MainSteps where the step definitions are implemented.

  4. Step Definitions
    Each method annotated with @Given@When, or @Then corresponds to a step in a Cucumber feature file.

    • Given Step

      java
      @Given("user is on NetBanking Landing Page") public void user_is_on_net_banking_landing_page() { System.out.println("User landed on netbanking page"); }
      • 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
      @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); }
      • 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 and password), 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
        @Then("homepage is displayed") public void homepage_is_displayed() { System.out.println("Home page is displayed"); }
        • 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
        @Then("cards are displayed") public void cards_are_displayed() { System.out.println("Cards are displayed"); }
        • 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-java
Explain
package 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:

  1. Package Declaration:

    java
    package cucumberOptions;
    • This line declares the package cucumberOptions, which is a way to organize Java classes.
  2. Imports:

    java
    import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions;
    • 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.
  3. CucumberOptions Annotation:

    java
    @CucumberOptions(features="src/test/java/features", glue ="stepDefinitions", monochrome=true)
    • 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).
  4. Class Declaration:

    java
    public class TestNGRunner extends AbstractTestNGCucumberTests { }
    • This declares a public class TestNGRunner that extends AbstractTestNGCucumberTests, making it a test runner for executing Cucumber tests with TestNG. It will use the configurations specified in the CucumberOptions annotation

Output

When the tests are executed, the output will be as follows:

Explain
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