Step Definition

Step Definitions is where the automation code is written. The steps in the feature files(introduced by Given/When/Then) are mapped to step definitions. For a step definition to be executed, it must match the given component in a feature.

Each step in the gherkin maps to a step definition that in turn contains a block of code that gets executed when the scenario is running. If any line of code inside the step definition returns an error or exception to the Cucumber Runner, then that step is marked as failed, otherwise, pass.

 

  • The steps in the feature file map to each step definition in the Step Definitions file.
  • Automation code (<code>) has to be written within each step definition block.
  • Automation code can further refer to the support system for including modules, classes, libraries, etc. [For example it can include selenium library to handle browser UI automation].
  • The support system in turn depends on the packages/libraries provided by the programming language stack that is used. In Java, it is called jars.

 

Scenario: TEQuality login Test Scenario
Given user is already on login page
When user enters username and password
Then user clicks on login button
And user navigates to home page of TEQuality

 

Add Step Definition file

Add a new Class file in the ‘stepDefinition‘ package and name it as ‘LoginStepDefination’. Do not check the option for ‘public static void main’.

Now on running the Test Runner class, the below message gets displayed (User can run through Junit Screenshot).

Now all the missing steps can be easily implemented. All that needs to be done is to just copy the complete text marked in a blue box and paste it into the above created ‘Login_StepDefination class.

After copying is done, the error will be displayed besides annotation as mentioned below:

 

This error can be easily resolved by importing the necessary dependencies.

 

Add Selenium Java code in the Step Definition methods

Step1: Use before method to create the driver object

@Before()

public void Setup() {

    driver = new ChromeDriver();

}

 

Step2: Implement the Given statement. As the given statement suggested user is in the login page, the same can be implemented as below:

       @Given("^user is already on login page$")

       public void user_is_already_on_login_page() throws Throwable {

              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("https://TEQuality.com");

       }

 

Step 3: Now implement the When statement. Here we provide username and password

      @When("^user enters username and password$")

      public void user_enters_username_and_password() throws Throwable {

          // Write code here that turns the phrase above into concrete actions

         driver.findElement(By.id("username")).sendKeys("user1");

         driver.findElement(By.id("password")).sendKeys("password");
}

 

Step 4: Now implement the Then statement, where the user will click on the login page

      @Then("^user clicks on login button$")

      public void user_clicks_on_login_button() throws Throwable {

              driver.findElement(By.id("login")).click();
      }

 

Step 5: At last verify the user is successfully logged in to TEQuality


@Then("^user navigates to home page of TEQuality$")

      public void user_navigates_to_home_page_of_TEQuality() throws Throwable {

             String Title= driver.getTitle();

            Assert.assertEquals("TEQuality", Title);

     }

Complete Code will look like below


package com.stepDefination;

import org.junit.Before;

import org.openqa.selenium.By;

import cucumber.api.java.en.Given;

import cucumber.api.java.en.Then;

import cucumber.api.java.en.When;

import junit.framework.Assert;

public class loginStepDefination extends TestBase {

@Before()

public void Setup() {

    driver = new ChromeDriver();

}

@Given("^user is already on login page$")

       public void user_is_already_on_login_page() throws Throwable {

              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("https://TEQuality.com");

       }

@When("^user enters username and password$")

      public void user_enters_username_and_password() throws Throwable {

          // Write code here that turns the phrase above into concrete actions

         driver.findElement(By.id("username")).sendKeys("user1");

         driver.findElement(By.id("password")).sendKeys("password");
}

@Then("^user clicks on login button$")

      public void user_clicks_on_login_button() throws Throwable {

              driver.findElement(By.id("login")).click();
      }

@Then("^user navigates to home page of TEQuality$")

      public void user_navigates_to_home_page_of_TEQuality() throws Throwable {

             String Title= driver.getTitle();

            Assert.assertEquals("TEQuality", Title);

      }

}