Enroll in Selenium Training

The next target is to test or run the feature file and in order to test the feature file, we need to write the implementation or step definition for each step in the feature file in java. When Cucumber executes a Step in a Scenario it will look for a matching Step Definition to execute.

What is Step Definition?

A Step Definition is a small piece of code with a pattern attached to it or in other words a Step Definition is a java method in a class with an annotation above it. An annotation followed by the pattern is used to link the Step Definition to all the matching Steps, and the code is what Cucumber will execute when it sees a Gherkin Step. Cucumber finds the Step Definition file with the help of the Glue code in Cucumber Options. We will cover different Cucumber Options in the next chapter.

Add a Step Definition file

1) Create a new Class file in the ‘stepDefinition‘ package and name it as ‘Test_Steps‘, by right click on the Package and select New > Class. Do not check the option for ‘public static void main‘ and click on Finish button.

  1. Take a look at the message in the console window. This message was displayed, when we ran the Test_Runner class.

Step Definition

NotePlease go through the chapter of First Cucumber Selenium Test to understand the above message.

  1. Notice, the eclipse console window says 'You can implement missing steps with the snippets below:'. It is very easy to implement all the steps, all you need to do is to copy the complete text marked in a blue box and paste it into the above created Test_Steps class.

  2. As of now, the test will show many errors on '@' annotations. Mouse hover at the annotations and import the 'cucumber.api.java.en' for all the annotations.

Step_Definition_2

Add Selenium Java code in the Step Definition methods

  1. Now take out the Selenium Java code of the following steps from the 'SeleniumTest' and paste it into the first method '@Given("^User is on Home Page$")'.
  • Launch the Browser
  • Navigate to Home Page

Method will look like this now:

@Given("^User is on Home Page$")
	public void user_is_on_Home_Page() throws Throwable {
		driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.store.demoqa.com");
		}
  1. Take out the code take out the Selenium Java code of the following steps from the 'SeleniumTest' and paste it into the second method '@When("^User Navigate to LogIn Page$")'.
  • Click on the LogIn link

Method will look like this now:

@When("^User Navigate to LogIn Page$")
	public void user_Navigate_to_LogIn_Page() throws Throwable {
		driver.findElement(By.xpath(".//*[@id='account']/a")).click();
		}
  1. Take out the code take out the Selenium Java code of the following steps from the 'SeleniumTest' and paste it into the second method '@When("^User enters UserName and Password$")'.
  • Enter UserName and Password
  • Click on Submit button

Method will look like this now:

@When("^User enters UserName and Password$")
	public void user_enters_UserName_and_Password() throws Throwable {
		driver.findElement(By.id("log")).sendKeys("testuser_1"); 	 
	    driver.findElement(By.id("pwd")).sendKeys("Test@123");
	    driver.findElement(By.id("login")).click();
		}
  1. Do the same steps for the rest of the methods as well and complete Test_Steps class will look like this:

Step Definition: Test_Steps Class

package stepDefinition;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

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

public class Test_Steps {
	public static WebDriver driver;
	@Given("^User is on Home Page$")
	public void user_is_on_Home_Page() throws Throwable {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.store.demoqa.com");
		}

	@When("^User Navigate to LogIn Page$")
	public void user_Navigate_to_LogIn_Page() throws Throwable {
		driver.findElement(By.xpath(".//*[@id='account']/a")).click();
		}

	@When("^User enters UserName and Password$")
	public void user_enters_UserName_and_Password() throws Throwable {
		driver.findElement(By.id("log")).sendKeys("testuser_1"); 	 
	    driver.findElement(By.id("pwd")).sendKeys("Test@123");
	    driver.findElement(By.id("login")).click();
		}

	@Then("^Message displayed Login Successfully$")
	public void message_displayed_Login_Successfully() throws Throwable {
		System.out.println("Login Successfully");
	}

	@When("^User LogOut from the Application$")
	public void user_LogOut_from_the_Application() throws Throwable {
		driver.findElement (By.xpath(".//*[@id='account_logout']/a")).click();
	}

	@Then("^Message displayed Logout Successfully$")
	public void message_displayed_Logout_Successfully() throws Throwable {
        System.out.println("LogOut Successfully");
	}

}

Note: Make sure to create your own Username and Password for the test and do not attempt to login with wrong credentials, as you will be blocked for few hours then on demo website.

Run the Cucumber Test

Now we are all set to run the first Cucumber test. Right Click on TestRunner class and Click Run As  > JUnit TestCucumber will run the script the same way it runs in Selenium WebDriver and the result will be shown in the left hand side project explorer window in JUnit tab.

Step_Definition_3

Cucumber starts it's execution by reading the feature file steps. As soon as Cucumber reaches the first step for e.g. Given the statement of Scenario, it looks for the same statement in the Step Definition file, the moment it finds the statement, it executes the piece of code written inside the function.

JUnit Test Runner Class
JUnit Test Runner Class
Previous Article
Gherkin Keywords
Gherkin Keywords
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback