Let us now implementing a simple cucumber test. We will validate "Gmail invalid login" scenario to understand the test case implementation. I would recommend you to go through my previous post What are Features and Step Definitions in Cucumber? and understand Features file and Step Definitions. Let us go step by step...
- Create a project and adding Jars. Please go through my previous post Downloading Cucumber JVM and Selenium Jars to understand.
- Create Features folder and features file.
- Add a Feature and sample Scenario.
- Create a Test Runner.
- Create Step Definitions package and define steps.
- Run the Test Runner as Junit test.
Step 1: Create a
Project New > Project > select Java Project and click Next. Give the project name as “seleniumCucumberProject” (you can
give any name).
Step 2: Add the "cucumber-jvm" and Selenium
Jars. Right Click on the project > Build Path > Configure Build Path.
Step 3: Libraries > Add External Jars > Select all the Jars > Click Open > Click OK.
Step 4: Create
a new folder by right clicking on the project “seleniumCucumberProject” and Select New > Folder name as Features.
Step 5: Create a file by right clicking on
the Features folder and Select New > File.
Step 6: Enter
the file name as "gamilFeatures.feature" and click Finish.
Step 7: Write the below cucumber test script
in the features file and save it.
Step 8: To trigger the cucumber tests
Cucumber uses Junit framework. We
need to write a Java Class called Test Runner. In this Test Runner program we
will provide a reference to features folder using Junit annotations (@RunWith,
@CucumberOptions) to execute the cucumber tests under features folder.
Create test
runner package under package and
name it as smokeTests or regressionTests. Now create a TestRunner class file (without main ()
function) under the above package.
“Junit
runner” class is used to run the cucumber tests. @RunWith annotation tells the
Junit to run test using Cucumber. @CucumberOptions tells “cucumber-jvm” which
configurations to run. In @CucumberOptions we specify “.feature” file location,
reports location and etc. Define the TestRunner class as below.
Step 9: Now we are done with declaring the
steps in Cucumber test in a features file and TestRunner to run the feature file.
Let’s run the TestRunner as Junit test. Right click on the TestRunner class and select Run as > Junit Test.
Now we would
expect the launch of the Firefox, navigation to url and the other actions to be
performed. But we see nothing happening and check the output in Console.
We can see
that console telling some steps are missing and we have to implement the same.
Yes, in the above cucumber test “gmailLogin.feature” we only declared the steps
using gherkin keywords but never defined them.
We need to
define each step under in a Java class “cucumberTestSteps”
under “StepDefinitions” package.
Each step in cucumber test will have a definite step definition and a reference
pattern attached to it. This pattern links the step in the features file and the step definition in the step definitions class. Now
create the package StepDefinitions and
a class cucumberTestSteps.
Step 10: Copy the steps missed from console
and paste it in the “cucumberTestSteps”
class. Remove unwanted comments and lines from the step definitions.
Step 11: Import the required libraries and
define the actions to be performed by each step definition.
The steps
are defined. Now to execute these we steps definition we should tell the
cucumber where our steps are defined in @CucumberOptions.
Step 12: Go to TestRunner class and specify
step definitions package in the cucumber options as @CucumberOptions(features="Features",
glue={"stepDefinitions"}).
Step 13: Run the TestRunner as Junit test and see.
And we can
see in the below screen shot all four the steps executed and in green means i.e
all the steps are passed.
That’s it.
We are done with implementing our first cucumber test successfully.
Nice blog...
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteReally cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!
ReplyDeletesoftware testing course in chennai