Cucumber – Integration with Selenium

In this tutorial, we will integrate Cucumber with Selenium WebDriver.

Let us consider a scenario to automate using Cucumber Selenium:

  • Navigate to Facebook website
  • Enter correct username and password
  • Click on sign in button
  • Verify that the user is navigated to the user’s homepage.

Feature File:

This file will have the test scenario which will cover the above steps in Gherkin format:

Feature: Login to Facebook
  In this feature, we will log in to facebook website.
 
  Scenario: Login with correct credentials.
    Given user navigates to facebook website
    Then user enters the emailId as saurabh.d2106@gmail.com
    Then user enters the password as abc@123
    Then user clicks on login button
    And user logged in successfully
    And user closes the browser

Step-Definition file:

This file consists of the test steps which are glued to the steps mentioned in the above gherkin language.

public class FacebookLogin {

	WebDriver driver;

	@Given("user navigates to facebook website")
	public void invokeBrowser() {

		System.setProperty("webdriver.chrome.driver",
				"C:/Users/Saurabh Dhingra/workspace/libs/chromedriver2.45/chromedriver.exe");

		driver = new ChromeDriver();

		driver.manage().window().maximize();

		driver.manage().deleteAllCookies();

		driver.get("https://facebook.com");
	}

	@Then("user enters the emailId as (.*)")
	public void enterEmailId(String emailId) {
		driver.findElement(By.id("email")).sendKeys(emailId);
	}

	@Then("user enters the password as (.*)")
	public void enterPassword(String password) {
		driver.findElement(By.id("pass")).sendKeys("abc@123");
	}

	@Then("user clicks on login button")
	public void click_login_button() {
		driver.findElement(By.id("u_0_2")).click();

	}

	@Then("user logged in successfully")
	public void verifyLoginIsSuccessful() {
		System.out.println("Logged in successful");
	}

	@And("user closes the browser")
	public void closeBrowser() {
		driver.quit();
	}
}

PS: For any questions, queries or feedback. Feel free to write us at saurabh@qatechhub.com or support@qatechhub.com

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: