Write a Cucumber BDD Test for the Login form using Scenario Outline

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

Write a feature for login functionality using Scenario Outline. Use examples to provide multiple sets of data for positive and negative test scenarios.

Solution

Feature: User Login
  Scenario Outline: Login with different credentials
    Given I am on the login page
    When I login with email "" and password ""
    Then I should see the message ""
    Examples:
      | email                      | password      | message               |
      | valid@gmail.com            | Pass1234      | Login successful      |
      | invalid@gmail.com          | wrongpass     | Invalid credentials   |
CucumberEx5.java
package cucumber;

import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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 CucumberEx5
 {
	WebDriver driver = new ChromeDriver();

	@Given("I am on the login page")
	public void i_am_on_the_login_page() {
		driver.get("https://www.example.com/");
	}

	@When("I login with email {string} and password {string}")
public void i_login_with_email_and_password(String email, String password)
 {

		WebElement EmailAddress = driver.findElement(By.id("email"));
		EmailAddress.sendKeys("example@gmail.com");

		WebElement Password = driver.findElement(By.id("password"));
		Password.sendKeys("pass123");

		WebElement login = driver.findElement(By.id("login_button"));
		login.click();

	}

	@Then("I should see the message {string}")
	public void i_should_see_the_message(String expectedMessage) {

	String actualMessage =  driver.findElement(By.id("message")).getText();
	assertEquals(expectedMessage, actualMessage);
	}
}
Cucumber Practice Exercises - QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.