CucumberClass 20230303 Lyst4675
CucumberClass 20230303 Lyst4675
CucumberClass 20230303 Lyst4675
What is Cucumber?
Cucumber is a testing framework which supports Behavior Driven Development (BDD). It
lets us define application behavior in plain meaningful English text using a simple grammar
defined by a language called Gherkin. Cucumber itself is written in Ruby, but it can be used to
“test” code written in Ruby or other languages including but not limited to Java, C# and Python.
It also allows to write specification in human readable Gherkin format.
Gherkin
A language above is called Gherkin and it implements the principles of Business
readable domain specific language(BRDSL). Domain specific language gives you the
ability to describe your application behavior without getting into details of
implementation. What does that mean? If we go back to our tutorial in TDD we saw
that we wrote test code before writing any application code. In a way we described
what is the expected behavior of our application in terms of tests.
Cucumber Introduction:
Cucumber is a framework which supports BDD-Behavior Driven Development
In BDD Automation programs are created based on the behavior of the application
Cucumber was initially implemented with Ruby, later it was extended to Java, C#
In Cucumber the automation programs are created based on a file called Feature File.
In this feature file the task to be automated is written in plain English statements which are connected
to the selenium code which performs those activities. The advantage of this process is the flow of the
automation programs can be understood by Non-Technical people like Stake-Holders or Business
Analyst.
Note: Cucumber uses its own language called as Gherkin
Cucumber Keywords:
Feature: This represents the module or functionality that is under tests
Scenario: This represents the test case that is been automated in a particular feature
Note: one Feature have one or more Scenarios.
A Step Definition is a small piece of code with a patternattached to it or in other words a Step
Definition is a java method in a class with an annotation above it. An annotation followed by the
pattern is used to link the Step Definition to all the matching Steps, and the code is
what Cucumber will execute when it sees a Gherkin Step. Cucumber finds the Step Definition file
with the help of Glue code in Cucumber Options.
Cucumber test runner class is one of the many mechanisms using which you can run Cucumber feature
file. The test runner class also acts as an inter-link between feature files and step definition classes. It is
in test runner class, that you provide the path for both feature file and step defs class.
With a test runner class, you have the option to run either a single feature file, or multiple feature files
as well. For now, we will focus on running a single feature file
Example TestRunner:
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@RunWith(Cucumber.class)
@CucumberOptions(features="FeatureFiles",
glue= "stepDefinitions",
tags= {"@CreationTest"},
plugin =
{"com.cucumber.listener.ExtentCucumberFormatter:Reports/report.html","pretty",
"html:target/cucumber-reports"},
//monochrome = true
//,dryRun=true
)
public class Runner extends AbstractTestNGCucumberTests {
A Step Definition is a small piece of code with a pattern attached to it or in other words a Step
Definition is a java method in a class with an annotation above it. An annotation followed by the
pattern is used to link the Step Definition to all the matching Steps, and the code is
what Cucumber will execute when it sees a Gherkin Step. Cucumber finds the Step Definition file
with the help of Glue code in Cucumber Options.
Create FeatureFiles folder at the project level and create one feature file in it
Create testRunner package and create a Runner.java under \src\test\java
Now run the test runner and observe the missing steps generated by cucumber
Create stepDefinitions package and create a StepDefinitions.java class under \src\test\java
Add the missing steps generated by cucumber to StepDefinitions.java class
Create a new source folder and name it as \src\test\resources and put driver files in it
Create Reports folder at the project level to store extent reports
int rqColNumber=-1;
for(int i=0;i<tdtf.colCount(sheet, 0);i++) {
if(tdtf.getData(sheet,0,i).equalsIgnoreCase(colHdr)) {
rqColNumber=i;
}
}