Cucumber Tutorial
Cucumber Options
What Are Cucumber Options ?
Cucumber options enable us to set properties for our tests.It provides the same options as the cucumber jvm command line to specify the path of feature file, step-definition file and to set other properties on it. @CucumberOptions is that annotation which provides such options. @CucumberOptions has a list of options as mentioned below :
features()
glue()
dryRun()
strict()
monochrome()
plugin()
name()
tags()
Syntax to write single cucumber options :
@io.cucumber.junit.CucumberOptions(
features = "path to the feature file"
}
Syntax to write multiple cucumber options :
@io.cucumber.junit.CucumberOptions( ,glue = "path to the package of step-definition file" features = "path to the feature file" }
We have already discussed about feature and glue in previous sections, here you can see...
So let's take an example to understand this in a much better way :
Feature file as 'featureoptions.feature' :
Feature: Cucumber Options Example
Scenario: Account Session
Given Homepage
When Login
Then Logout
Then Close Browser
Step-definition class as 'CucumberOptionsExample.java' :
package stepdefinitions;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class CucumberOptionsExample {
@Given("^Homepage$")
public void homePage() {
System.out.println("Home Page");
}
@When("^Login$")
public void logIn() {
System.out.println("Log In");
}
@Then("^Logout$")
public void logOut() {
System.out.println("Log Out");
}
}
dryRun This option can be set either 'true' or 'false'. If it is set as 'true', cucumber checks that every mentioned step in the feature file has its corresponding code in stepdefinition and if any of them is undefined then it will show error. If it is set 'true' it will disable the displaying of output on console. To enable the display set it 'false'. The default value is 'false'. Added one more step in feature file as :
Then Close Browser
Write JUnit test runner class as :
package cucumberTest; import org.junit.runner.RunWith; import io.cucumber.junit.Cucumber; @RunWith(Cucumber.class) @io.cucumber.junit.CucumberOptions( features = "Features/featureoptions.feature" //pass the path of feature file or the folder name that contains feature file ,glue={"stepdefinitions"} //package name of step definitions classes ,dryRun=true ) public class TestRunner { }
*As dryRun is set true so it will just show you error message but not output.
monochrome
The default value of this option is false and if it is set true the console output would be much more readable as it should be.
Create test runner class for above feature file and step-definition :
package cucumberTest;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@io.cucumber.junit.CucumberOptions(
features = "Features/featureoptions.feature" //pass the path of feature file or the folder name that contains feature file
,glue={"stepdefinitions"} //package name of step definitions classes
,monochrome=true
)
public class TestRunner {
}
Run this test runner class with and without 'monochrome' and see the differences .
plugin It is used to specify different formatting options for the output reports.
Various options that can be used with plugin are as follows :
Pretty - It prints Gherkin source on console.
Syntax : plugin= {"pretty"}
HTML - It will generate a HTML report at the mentioned location.
Syntax : plugin= {"html:location of HTML file"}
JSON - It will generate a JSON format file that contains all the information from the Gherkin source at the mentioned location.
Syntax : plugin= {"json:location of JSON file"}
JUnit - Report will be generated in XML format at the mentioned location.
Syntax : plugin= {"junit:location of XML file"}
For practice, let's create a test runner class with plugin options :
package cucumberTest;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
@RunWith(Cucumber.class)
@io.cucumber.junit.CucumberOptions(
features = "Features/featureoptions.feature" //pass the path of feature file or the folder name that contains feature file
,glue={"stepdefinitions"} //package name of step definitions classes
,plugin= {"pretty","html:report.html","json:jsonreport.json","junit:xmlreport.xml"}
)
public class TestRunner {
}