Site icon TestingDocs.com

Cucumber Java BDD Selenium Framework

Overview

Let’s learn the steps involved in creating Cucumber Java BDD Selenium Project using Eclipse IDE in this tutorial.

Create a Maven Project

Configure the Project

Specify the Group ID, Artifact ID, Version and Packaging format for the project.

 

 

Cucumber Java BDD Dependencies

Add the below dependencies to the Maven project file pom.xml.

<dependencies>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>6.8.1</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>6.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>
  </dependencies>

 

 

Eclipse plugin for Cucumber

Instructions to install Cucumber Eclipse Plugin can be found here:

https://www.testingdocs.com/questions/how-to-install-cucumber-eclipse-plugin/

Add Gherkin Steps

#Sample Demo Feature file
#www.TestingDocs.com

Feature:
  Browse Google search page, and search for a query,
  The search results should show the results about search query.

Scenario:
  Given that I go to Google search page
  When I add "testingdocs" to the search textbox
  And Click the Search Button
  Then "testingdocs" related results should show up

 

Add Step Definitions

Step definitions are methods or code that connect Gherkin steps.

package Steps;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

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

public class GoogleSearchSteps {
  private final WebDriver driver = new ChromeDriver();
  WebElement element;
  @Given("that I go to Google search page")
  public void that_i_go_to_google_search_page() {
    System.out.println("Google Search page");
    driver.get("https://www.google.com");
  }

  @When("I add {string} to the search textbox")
  public void i_add_to_the_search_textbox(String string) {
    System.out.println("Type Search Keyword");
    element = driver.findElement(By.name("q"));
    element.sendKeys(string);

  }

  @When("Click the Search Button")
  public void click_the_search_button() {
    System.out.println("Click Search Button");
    element.submit();
  }

  @Then("{string} related results should show up")
  public void related_results_should_show_up(final String title) {
    System.out.println("Google Search Results Page");
    new WebDriverWait(driver,30L).until(new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver d) {
        return d.getTitle().toLowerCase().startsWith(title);
      }
    });
  }

  @After()
  public void closeBrowser() {
    driver.quit();
  }

}

 

Cucumber Test Runner

Create a Test Runner class. Add the following code. Specify the feature file location to Cucumber.

package Steps;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources/Features",glue= {"Steps"})
public class CucumberTestRunner {

}

Download Browser Driver

We need to download Browser driver depending on where and how the test is run. For example Download Chrome Driver executable .exe file to Test on Google Chrome browser on Windows operating system.

Download URL:

https://sites.google.com/a/chromium.org/chromedriver/

Copy the driver executable to the project root folder.

Run the Tests

Run the tests as JUnit tests.

 

JBehave Tutorials on this website can be found at:
https://www.testingdocs.com/jbehave-framework-tutorial/

For more details on Cucumber, visit the official website at:

https://cucumber.io/

Exit mobile version