Cucumber Testing in Selenium
Cucumber is a popular behavior-driven development (BDD) framework that can be integrated with Selenium for automated testing. It allows you to write test cases in plain language that can be understood by non-technical stakeholders. Here’s how to set up and use Cucumber with Selenium for testing:
Create a Maven or Gradle Project: Start by creating a Java project using either Maven or Gradle. You can use your preferred build tool, but for this example, we’ll use Maven.
Add Dependencies: In your project’s
pom.xml
, add dependencies for Selenium WebDriver, Cucumber, and a Cucumber runner.xml<dependencies> <!-- Selenium WebDriver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> <!-- Use the latest version --> </dependency> <!-- Cucumber dependencies --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>6.9.1</version> <!-- Use the latest version --> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>6.9.1</version> <!-- Use the latest version --> <scope>test</scope> </dependency> </dependencies>
Make sure to update the versions to the latest available if necessary.
Create Cucumber Feature Files: Cucumber tests are defined in
.feature
files using Gherkin syntax. Create feature files in your project’ssrc/test/resources
directory. For example,sample.feature
:gherkinFeature: Login to a website Scenario: Successful login Given I am on the login page When I enter my username and password And I click the login button Then I should be logged in successfully
Create Step Definitions: Implement step definitions for your feature files. These are Java classes that map the Gherkin steps to actual Selenium WebDriver actions. Place these classes in a package, such as
com.example.steps
.javapackage com.example.steps; import io.cucumber.java.en.Given; import io.cucumber.java.en.When; import io.cucumber.java.en.Then; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyStepDefinitions { private WebDriver driver; @Given("I am on the login page") public void i_am_on_the_login_page() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); driver.get("https://example.com/login"); } @When("I enter my username and password") public void i_enter_my_username_and_password() { driver.findElement(By.id("username")).sendKeys("your_username"); driver.findElement(By.id("password")).sendKeys("your_password"); } @When("I click the login button") public void i_click_the_login_button() { driver.findElement(By.id("login-button")).click(); } @Then("I should be logged in successfully") public void i_should_be_logged_in_successfully() { // Add assertions to verify login success driver.quit(); } }
Run Cucumber Tests: You can run your Cucumber tests using a test runner class. Create a class in your project, for example,
RunCucumberTests.java
:javapackage com.example; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources", glue = "com.example.steps" ) public class RunCucumberTests { }
Execute Tests: Run the
RunCucumberTests
class as a JUnit test. Cucumber will read your feature files, execute the step definitions, and generate reports.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 IT Training Institute for Selenium Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Selenium here – Selenium Blogs
You can check out our Best In Class Selenium Training Details here – Selenium Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook:https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks