Introduction
Feature files can be executed from the command line using the main() method of cucumber.api.cli.Main class. This article describes the overall process of downloading the necessary cucumber jars, setting up folders, creating the source files, compiling and execution.
Folder Structure
Create a parent folder named ‘cukecli’ and another folder ‘code’ within it. The names of the folder can be anything but appropriate changes will be needed to be made.
Cucumber Jars
To compile and run the command from CLI, below cucumber jars are required. The jar version numbers are compatible with 4.4.0. Download these jars into the ‘cukecli’ folder from Maven or if available from a local repository.
cucumber-core.jar
4.4.0
cucumber-java.jar
4.4.0
cucumber-java8.jar
4.4.0
gherkin.jar
5.1.0
cucumber-expressions.jar
7.0.2
datatable.jar
1.1.14
datatable-dependencies.jar
1.1.14
tag-expressions.jar
1.1.1
typetools.jar
0.5.0
StepDefinition Class
Create a stepdefiniton java class named Stepdefs.java in the ‘code’ folder and and copy the below source.
package code; import cucumber.api.java.en.Given; public class Stepdefs { @Given("Step from {string} in {string} feature file") public void step(String scenario, String file) { System.out.format("%s from %s feature file.\n", scenario, file); } }
Feature Files
Create a feature file named scenarios.feature in the ‘code’ folder and copy the below source.
Feature: Scenarios feature file Scenario: Scenario Number 1 Given Step from 'Given Scenario 1' in 'scenarios' feature file Scenario: Scenario Number 2 Given Step from 'Given Scenario 2' in 'scenarios' feature file
Create a feature file named scenariooutlines.feature in the ‘code’ folder and copy the below source.
Feature: Scenario Outlines feature file Scenario Outline: <scen_out_row_num> Given Step from 'Given <scen_out_row_num>' in 'scenariooutlines' feature file Examples: | scen_out_row_num | | Scenario Outline Row 1 | | Scenario Outline Row 2 |
Compiling And Execution
Navigate to the ‘cukecli’ folder at the command line to compile the code using the following command
javac -cp “.;<path to cukecli folder>\*” code\*.java
Run the following command from the ‘cukecli’ folder to execute the feature files using the following command
java -cp “.;<path to cukecli folder>\*” cucumber.api.cli.Main -g code code
Console Output
An output similar to below should be visible on the console window.
Given Scenario Outline Row 1 from scenariooutlines feature file. Given Scenario Outline Row 2 from scenariooutlines feature file. Given Scenario 1 from scenarios feature file. Given Scenario 2 from scenarios feature file.
Usage Options
A list of all the options available for the cucumber.ap.cli.Main class is located here. Below is a brief explanation of some of them which are frequently used.
- feature file path – There is no key required for specifying the feature file path. A folder can be specified(as above) which will load all the files with .feature extension in the folder and any other sub-folder in it. A specific feature file can also be mentioned eg. glue\scenarios.feature. A specific line or number of lines can also be added for execution for a specific feature file eg. glue\scenarios.feature:3:7. Refer to this article’s “Filtering by Line Number” section for more explanation. Other feature file path options are mentioned in the usage details linked above. The feature file path is usually mentioned at the end of the command.
- glue packages – The key for this option is either -g or –glue. The package path for the stepdefinition and hook classes is mentioned here. Multiple glue packages can be added by repeating the option.
- plugin addition – The key for this option is either -p or –plugin. This allows plugins to be added either inbuilt ones like html, junit, testng, json etc or custom plugins with a full qualified class name. Refer to this article for creating a custom plugin. Multiple plugins can be added by repeating the option.
- tags – The key for this option is either -t or –tags. This allows tags to be mentioned which filters the scenarios to be run. Refer to this article’s “Filtering by Tags Option” section for more explanation.
- name on scenario(outline) – The key for this option is either -n or –name. This allows a regular expression to be used for filtering on the basis of scenario or scenario outline names. Refer to this article’s “Filtering by Name Option” section for more explanation.
- thread for parallel runs – The key for this option is –threads. This enables parallel execution of scenarios.
- order of execution – The key for this option is –order. The scenarios can be run in reverse, random or random with a long seed. The random with seed order is mentioned as random:seed.
- count of scenarios – The key for this option is –count. The number of scenarios can be limited by using the count option. This is applied after the filtering for tags and names have been completed.
- Remaining options – Details of other options are mentioned in the usage document.
i am using the exact folder struture, commands, files and jars mentioned in the steps above
I just tried following the exact steps and it worked. I think the jar versions u r using are not the same. Try from scratch.
Hi Grasshopper,
You have explaied it very well. Steps are easy to understand and follow
But unfortunately even after following exactly same steps I am unable to run it using command promt. Can you please help, I am stuck on it from 2 weeks. Below are the errors
G:\cuk cmd\cukecli>java -cp “.;G:\cuk cmd\cukecli\*” cucumber.api.cli.Main -g code code
[31mF[0m[31mF[0m[31mF[0m[31mF[0m
[31mFailed scenarios:[0m
[31mcode/scenariooutlines.feature:8 [0m# Scenario Outline Row 1
[31mcode/scenariooutlines.feature:9 [0m# Scenario Outline Row 2
[31mcode/scenarios.feature:3 [0m# Scenario Number 1
[31mcode/scenarios.feature:6 [0m# Scenario Number 2
4 Scenarios ([31m4 failed[0m)
4 Steps ([31m4 failed[0m)
0m0.165s
java.lang.NoSuchMethodError: io.cucumber.cucumberexpressions.Expression.match(Ljava/lang/String;)Ljava/util/List;
at io.cucumber.stepexpression.StepExpression.match(StepExpression.java:21)
at io.cucumber.stepexpression.ExpressionArgumentMatcher.argumentsFrom(ExpressionArgumentMatcher.java:20)
at cucumber.runtime.java.JavaStepDefinition.matchedArguments(JavaStepDefinition.java:53)
at cucumber.runner.Glue.stepDefinitionMatches(Glue.java:104)
at cucumber.runner.Glue.stepDefinitionMatch(Glue.java:82)
at cucumber.runner.Runner.addTestStepsForPickleSteps(Runner.java:64)
at cucumber.runner.Runner.createTestCaseForPickle(Runner.java:54)
at cucumber.runner.Runner.runPickle(Runner.java:39)
at cucumber.runtime.Runtime$1.run(Runtime.java:84)
at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:220)
at cucumber.runtime.Runtime.run(Runtime.java:81)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
at ?.Step from ‘Given Scenario Outline Row 1’ in ‘scenariooutlines’ feature file(code/scenariooutlines.feature:4)
java.lang.NoSuchMethodError: io.cucumber.cucumberexpressions.Expression.match(Ljava/lang/String;)Ljava/util/List;
at io.cucumber.stepexpression.StepExpression.match(StepExpression.java:21)
at io.cucumber.stepexpression.ExpressionArgumentMatcher.argumentsFrom(ExpressionArgumentMatcher.java:20)
at cucumber.runtime.java.JavaStepDefinition.matchedArguments(JavaStepDefinition.java:53)
at cucumber.runner.Glue.stepDefinitionMatches(Glue.java:104)
at cucumber.runner.Glue.stepDefinitionMatch(Glue.java:82)
at cucumber.runner.Runner.addTestStepsForPickleSteps(Runner.java:64)
at cucumber.runner.Runner.createTestCaseForPickle(Runner.java:54)
at cucumber.runner.Runner.runPickle(Runner.java:39)
at cucumber.runtime.Runtime$1.run(Runtime.java:84)
at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:220)
at cucumber.runtime.Runtime.run(Runtime.java:81)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
at ?.Step from ‘Given Scenario Outline Row 2’ in ‘scenariooutlines’ feature file(code/scenariooutlines.feature:4)
java.lang.NoSuchMethodError: io.cucumber.cucumberexpressions.Expression.match(Ljava/lang/String;)Ljava/util/List;
at io.cucumber.stepexpression.StepExpression.match(StepExpression.java:21)
at io.cucumber.stepexpression.ExpressionArgumentMatcher.argumentsFrom(ExpressionArgumentMatcher.java:20)
at cucumber.runtime.java.JavaStepDefinition.matchedArguments(JavaStepDefinition.java:53)
at cucumber.runner.Glue.stepDefinitionMatches(Glue.java:104)
at cucumber.runner.Glue.stepDefinitionMatch(Glue.java:82)
at cucumber.runner.Runner.addTestStepsForPickleSteps(Runner.java:64)
at cucumber.runner.Runner.createTestCaseForPickle(Runner.java:54)
at cucumber.runner.Runner.runPickle(Runner.java:39)
at cucumber.runtime.Runtime$1.run(Runtime.java:84)
at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:220)
at cucumber.runtime.Runtime.run(Runtime.java:81)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
at ?.Step from ‘Given Scenario 1’ in ‘scenarios’ feature file(code/scenarios.feature:4)
java.lang.NoSuchMethodError: io.cucumber.cucumberexpressions.Expression.match(Ljava/lang/String;)Ljava/util/List;
at io.cucumber.stepexpression.StepExpression.match(StepExpression.java:21)
at io.cucumber.stepexpression.ExpressionArgumentMatcher.argumentsFrom(ExpressionArgumentMatcher.java:20)
at cucumber.runtime.java.JavaStepDefinition.matchedArguments(JavaStepDefinition.java:53)
at cucumber.runner.Glue.stepDefinitionMatches(Glue.java:104)
at cucumber.runner.Glue.stepDefinitionMatch(Glue.java:82)
at cucumber.runner.Runner.addTestStepsForPickleSteps(Runner.java:64)
at cucumber.runner.Runner.createTestCaseForPickle(Runner.java:54)
at cucumber.runner.Runner.runPickle(Runner.java:39)
at cucumber.runtime.Runtime$1.run(Runtime.java:84)
at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:220)
at cucumber.runtime.Runtime.run(Runtime.java:81)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
at ?.Step from ‘Given Scenario 2’ in ‘scenarios’ feature file(code/scenarios.feature:7)
Can u share your repo? If not please create a minimum example repo. Will definitely look into it
I am doing the same thing and trying to run feature file using –glue & feature file options through command line(cucumber.api.cli.Main class)and maven dependencies jar but I am getting below error :
command : java -jar target/xyz-jar-with-dependencies.jar –glue package name src/test/java/…featurefile.feature
UUUUUUUUUUUUU
3 Scenarios (3 undefined)
13 Steps (13 undefined)
0m0.000s
You can implement missing steps with the snippets below: