Cucumber Basics:
There are few
terminologies in cucumber. Let's understand them
Feature Files
Feature files are the files which
are used to write the test automation steps in Gherkin language. Normally this
will be live document. The extension of these files will be .feature.
A Feature file will contain much
number of Scenarios. Each scenario represents a test case. In Each Scenario
there are Steps which are differentiated by keywords called (GIVEN, WHEN, THEN
etc.,)
Feature:
This gives information about the
high level business functionality and the purpose of Application under test.
Everybody should be able to understand the intent of feature file by reading
the first Feature step.
Scenario:
Basically a scenario represents a
particular functionality which is under test. By seeing the scenario user
should be able to understand what the test is all about. Each scenario should
follow given, when and then format. This language is called as “gherkin”
language.
·
Given: As we know the meaning of given,
it specifies the precondition.
·
When: This is used when some action is
to be performed.
·
Then: The expected outcome or result
should be placed here.
·
Background: Whenever any step is required to
perform in each scenario then those steps needs to be placed in Background.
·
And: And is used to combine two or more
same type of action. i.e., if you have multiple prerequisites, then we have
the combine all the GIVEN statement by using "AND" keyword.
Basic template for a
FEATURE file:
Feature:
<Description of the Feature>
Scenario:
<Scenario 1>
Given <Prerequisite>
When <Actions be be
performed>
Then <Expected Result>
Scenario:
<Scenario 2>
Given <Prerequisite>
When <Actions be be
performed>
Then <Expected Result>
|
EX:
Feature: Login
Functionality Feature
In order to ensure Login
Functionality works,
I want to run the cucumber
test to verify it is working
Scenario: Login
Functionality
Given user navigates to
www.facebook.com
When user logs in using
Username as “USER” and Password “PASSWORD”
Then login should be
successful
|
Scenario Outline:
Scenario outlines are used when same
test has to be performed with different data set. Ex: if we want to test login
functionality with multiple different set of username and password.
Let's take the same
example to show the usage of Scenario Outline
Feature: Login
Functionality Feature
In order to ensure Login
Functionality works,
I want to run the cucumber
test to verify it is working
Scenario Outline:
Login Functionality
Given user navigates to
www.facebook.com
When user logs in using
Username as <username> and Password <password>
Then login should be
successful
Examples:
|username|password|
|Suresh|password1|
|Naresh|password12|
|
Observations:
- Column names (username and password) are passed as parameters to WHEN statement.
- Scenario Outline is used instead of Scenario
- EXAMPLES are used to pass different arguments in tabular format. Vertical pipes are used to separate the columns
Tags:
Cucumber by default runs all
scenarios in all the feature files. In real time projects, there could be
hundreds of feature file which are not required to run at all times. We can
also use multiple tag names for a single feature.
For instance: Feature files related
to smoke test need not run all the time. So if you mention a tag as smokeTest
in each feature file which is related to smoke test and run cucumber test with
@SmokeTest tag. Cucumber will run only those feature files specific to given
tags. Please follow the below example. You can specify multiple tags in one
feature file.
Let's take the same
example to show the usage of Scenario Outline
@SmokeTest
@RegressionTest
Feature: Login
Functionality Feature
In order to ensure Login
Functionality works,
I want to run the cucumber
test to verify it is working
Scenario Outline:
Login Functionality
Given user navigates to
www.facebook.com
When user logs in using
Username as <username> and Password <password>
Then login should be
successful
Examples:
|username|password|
|Suresh|password1|
|Naresh|password12|
|
Tags can be applied to
Scenarios, Please see the example below
@SmokeTest
@RegressionTest
Feature: Login
Functionality Feature
In order to ensure Login
Functionality works,
I want to run the cucumber
test to verify it is working
Scenario Outline:
Login Success Functionality
Given user navigates to
www.facebook.com
When user logs in using
Username as <username> and Password <password>
Then login should be
successful
Examples:
|username|password|
|Suresh|password1|
|Naresh|password12|
@SmokeTest
Scenario Outline:
Login Failure Functionality
Given user navigates to
www.facebook.com
When user logs in using
Username as <username> and Password <password>
Then login should be
successful
Examples:
|username|password|
|Rajesh|password1|
|Mahesh|password12|
|
Observation:
- Suppose you want to execute the scenarios/features which are in Regression Suite, then the Junit Runner will execute only "Login Success Functionality" Scenario
- Suppose you want to execute the scenarios/features which are in Smoke Suite, then the Junit Runner will execute only "Login Success Functionality" and "Login Failure Functionality" Scenarios
Junit Runner:
Cucumber Runner / JUNIT Runner Class
is the class, which is used to get all the feature files in the specified
location.
The Basic Template of
Junit Runner is as follows:
@RunWith(Cucumber.class)
@CucumberOptions (
format(deprecated)/plugin={"pretty","json:target"}
,
features =
{"src/java/features/"}
)
public class Runner
{
}
|
Observations:
- Cucumber.class : Whenever we run anything as part of cucumber it iwll run using the cucumber.class file
- CucumberOptions: it is used to provide some additional inputs like, format, feature files location and tags
Example:
@RunWith(Cucumber.class)
@CucumberOptions (
format(deprecated)/plugin={"pretty","json:target"}
,
features =
{"src/java/features/"},
tags={"@SmokeTest", "@RegressionTest"}
)
public class Runner
{
}
|
Note: To run the
specific feature file cucumber uses standard Junit Runner and specify tags in
@Cucumber. Options(Deprecated)/@CucumberOptions. Multiple tags can be given by
using comma separate. Here you can specify the path of the report and type of
report you want to generate.
If you run this Runner
file, all the Scenarios/features under SmokeTest/RegressionTest will be
executed and the result be generated in JSON format.
Trouble Shootings:
If you get the
Invocation Target Exception, then you have to remove Cucumber-spring.jar from
the build path
|
If you ran the
feature file but it threw an error like this:
Exception in thread
"main" cucumber.runtime.CucumberException: Unknown option: --plugin
at
cucumber.runtime.RuntimeOptions.parse(RuntimeOptions.java:119)
at
cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:50)
then you have to
update the jar files to latest version
|
If you are not able
to import the "@Cucumber.Options" and it is throwing error, the use
"@CucumberOptions" instead of "@Cucumber.Options"
(Deprecated)
|
If you are getting
the below exception
Exception in thread
"main" Usage: java cucumber.api.cli.Main [options] [
[FILE|DIR][:LINE[:LINE]*] ]+
Then use the version
1.2.4 for cucumber-Java, Cucumber-Junit and Cucumber-core jars. user version
2.12.2 for gherkin
change the code as below:
@CucumberOptions(
plugin =
{"pretty","jason:Target/"},
features = {"src/Cucumber/"}
)
|
0 comments:
Post a Comment