Cucumber is one of the most popular BDD testing frameworks. In this article, you will learn how to write the feature files and step definitions for running Selenium tests.
What tools you will need to run cucumber tests?
- JAVA IDE - Intellij IDEA
- JDK
- Maven or Gradle
- Cucumber API
- Cucumber plug-in for Intellij IDEA
- Intellij IDEA plug-ins for Gherkin and Cucumber for Java
You will need to add below dependencies in your project to use Cucumber API
testCompile 'info.cukes:cucumber-java:1.2.4'testCompile 'info.cukes:cucumber-junit:1.2.4'
How to write tests using Cucumber?
Below example shows how you can write a simple cucumber test class. When we run that Test class, all features in the src/test/resources directory are run. If there are no step definitions written for the steps in feature file, it creates sample step methods for you.
package cucumber_tests;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
/** * Created by Sagar on 27-03-2016. */
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/")
public class CucumberTests {
}
I had a feature file as shown below. But I did not write any steps.
Feature:Simple Selenium test
Scenario: To check page loading
Given I am on "http://www.softpost.org" home page
When I click on "Selenium Test page" link
Then I land on Selenium Test Page
so after running above class, in the console I noticed that sample step methods are created.
1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.000s
You can implement missing steps with the snippets below:
@Given("^I am on \"([^\"]*)\" home page$")
public void i_am_on_home_page(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^I click on \"([^\"]*)\" link$")
public void i_click_on_link(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^I land on Selenium Test Page$")
public void i_land_on_Selenium_Test_Page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Process finished with exit code 0.
After that I just copied all these methods and pasted into the steps class . Then I updated all test methods.
package steps;
import cucumber.api.PendingException;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.runtime.junit.FeatureRunner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
/** * Created by Sagar on 27-03-2016. */public class step_definitions {
WebDriver driver;
@Before public void init(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@Given("^I am on \"([^\"]*)\" home page$")
public void i_am_on_home_page(String arg1) throws Throwable {
driver.get("http://www.softpost.org");
}
@When("^I click on \"([^\"]*)\" link$")
public void i_click_on_link(String arg1) throws Throwable {
driver.findElement(By.linkText("Selenium Test Page")).click();
}
@Then("^I land on Selenium Test Page$")
public void i_land_on_Selenium_Test_Page() throws Throwable {
assert driver.findElements(By.id("fn")).size() > 0;
}
@After public void clean() {
driver.close();
driver.quit();
}
}
After that I ran the test class but still Cucumber was unable to find the step definitions. Then I updated cucumber options as described below.
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources",
glue = {"steps"}
)
Notice that how I have added glue in the cucumber options. This is important to note that glue should be configured to be the package name in which your step definitions exist. While features option can have the value as directory name.
Another thing to note is that we can use lambda expressions to write step definitions. But for that you will have to use Java 8.
You may encounter below error when working with lambda expressions.
invalid method declaration return type required cucumber
To fix this error, you need to use below syntax.
public class lambda_steps implements En {
public lambda_steps() {
Given("^I am on softpost\\.org home page$", () -> {
});
}
}
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com