Write a step definition code for simple web interaction

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Write the step definitions and implement the test methods for the previous feature file created in Exercise 1

Solution

Right-click on the feature file > Run As > Cucumber Feature.

In the console window, you will see an undefined scenario and a line “You can implement missing steps with the snippets below:”

Create a Java Class in your project and paste these code snippets and implement the test methods.

CucumberEx1.java
package cucumber;	

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

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

public class CucumberEx1 {

	WebDriver driver = null;


	@Given("Open Browser")
	public void open_browser() {
		driver = new ChromeDriver();
		System.out.println("Chrome Browser is open");
	}

	@When("navigate to google.com")
	public void navigate_to_google_com() {
		driver.get("https://www.google.com/");
		System.out.println("Navigate to Google");
	}

	@Then("Validate the Google page is open")
	public void validate_the_google_page_is_open() {
		String title = driver.getTitle();
		assertEquals(title, "Google");
		System.out.println("Validating the title of the webpage which is " +title);

	}

	@Then("Close the browser")
	public void close_the_browser() {
		driver.close();
		System.out.println("Closing Browser");
	}
}
Cucumber Practice Exercises - QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.