Step 1.
Create Simple Maven Project
Add Dependencies in pom.xml
(Dependency libraries are stored in our local repository once we add dependecies we can see it here : C:\Users\Administrator\.m2\repository)
Step 2.
We need to create feature file first (means we start writing requirements)
Create package with name features inside src/test/resources Folder.
Then create feature file with any name. Add .feature extension after name.
Step 3.
Install cucumber Eclipse plugin from market place else when we create feature file before installing cucumber-eclipse then could't see by default generated Content which are in feature file.
(we can create feature file without any default content also)
We are creating framework with cucumber as a top layer
what we write in statement with " " is considered as a parameter in feature file
It comes in yellow means there is no associated definition cucumber will find for it.
Cucumber says whatever written in feature file as a statement there has to have a one to one mapping with the method.
So for each line there has to be a method.
Step 4.
Now we want to run it
there are multiple ways to run it
a. maven
b. cucumber command line interface
c. J-unit class file for run cucumber feature file
Most of the case we use Junit because there is a tag run with. We can run it with cucumber file
Step: 5
Create a Package with any name, suppose name is runner in src/test/java
Create a class inside runner package with name RunTest.
Now name which we give to the class also has some significance, In TestNG we give any name to any file, Here it has some Significance. When we run a maven project, their is a Plugin called as sure-fire. It is Configure to take all the files which has Test written in his name.
@RunWith(value = null)
@RunWith is a Junit Runner File. We need to mention value and name here.
Now we Instructing Junit when you run, Run this as a cucumber file. So We add value and null as :
@RunWith(Cucumber.class)
Now whenever it runs it will run with cucumber with Junit. Thats how Cucumber and Junit Integrated.
Cucumber it not dependent on Junit but it gives you a facility if you want run cucumber Test via Junit then this is how we can do it.
Step: 6
I want Instruct Cucumber Whats need to be executed, where is the mapping, how the reports are being generated and what exactly the Scenarios I want cucumber to run this are called as Configuration Setting or options or Cucumber options.
So before I could run Cucumber I need to provide cucumber Options.
@CucumberOptions
(features="", glue="",tags=''",plugin="",dryRun=true
)
feature="" - We define where are my feature files
features="classpath:features",
glue="" - We define where are the stepdef files because we can keep it any where.
tag="" - In the tag we define what needs to be executed. this tag is actually which is mentioned in feature file inside @tag
plugin="" - We can inject external functionality in to the Cucumber. It we want to use extend report to fetch cucumber steps you can put that line here. Also we can say what kind of report cucumber wants to generate and where that files need to be saved also we can mention it here.
as like plugin="html:target/cucumber-reports"
dryRun=true - means just Run it but don't execute anything. If any problem in the feature file or glue mapping it will explain or print it.
Step 7:
Now Run it by Right click on RunTest.java & Run as Junit
Now we can see it as:
Step 8:
Cucumber has executed it & he is saying
Undefined scenarios:
features/HealthCheck.feature:6 [0m# Title of your scenario
That is he has picked it he is saying:
You can implement missing steps with the snippets below (See above image)
Now it has for us to create these methods and it has annotated as well.
Now we will create this file, So Copy all snippets methods -> goto Package Structure-> Create another package in src/test/java give them name as stepdefs ->create a Class inside stepdefs Package with name StepDefs ->Copy all steps from snippet and paste in StepDefs file
Now I have a Scalaton of the StepDefs
Now here is method have annotation on the top of it.
Each statement of the feature file corresponds to StepDef class file
Whatever we put in double quotes will be considered as a parameter and there is argument in the method.
Import all red line marked things like Given,When, Then
Now Feature file looks like this:
And StepDefs Class looks like this:
Cucumber assist as to generate it or we can write our own as well.
We can implement feature file & update methods as well in StepDefs class
Suppose we add step in feature file
@Then I want to do something
we can implement it in StepDefs as:
@Then("I want to do something")
public void akash()
If we want to send any value we can write it in double quotes.
Whatever we need to parameterise will put it in {String}, {int}, {float} {Boolean}
So name of method doesn't mater when what written on top of it under the significance only that is how it is mapping your test Scenario with the method needs to call once it is getting triggered.
Once we write StepDefs for each feature statement it will remove yellow color of it, we can close it and reopen to see it.
No comments:
Post a Comment