Cucumber proposes to write scenario in the Given/When/Then format. In the last chapter of Cucumber Selenium Java test we decided on the LogIn scenario on Store.DemoQA.com. In this chapter we will write a test in Cucumber Format (Feature File).
What is Cucumber Feature File?
A Feature File is an entry point to the Cucumber tests. This is a file where you will describe your tests in Descriptive language (Like English). It is an essential part of Cucumber, as it serves as an automation test script as well as live documents. A feature file can contain a scenario or can contain many scenarios in a single feature file but it usually contains a list of scenarios. Let’s create one such file.
1) On the Feature folder Right click and select New > File
2) In order for Cucumber to automatically detect the stories (or features, as they’re known in Cucumber), you need to make sure that they carry the ‘.feature‘ file extension. For example, in this case, I’ve named my user story ‘LogIn_Test.feature‘. Every ‘.feature‘ file conventionally consists of a single feature.
3) Write the first cucumber script. In BDD terms the scenario would look like the following.
Cucumber Test Script
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters UserName and Password
Then Message displayed Login Successfully
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
Note:This is a simple test in Cucumber. Don’t worry about the syntax if you don’t understand it. Ideally you should be able to understand the intent of the test just by reading a test in feature file. We will discuss this in more details in next chapter.
Now moving forward we have just defined a test. You will notice colored part of the tests (Feature, Scenario, Given, When, And and Then). These are keywords defined by Gherkin. Gherkin has more keywords and we will discuss those in following tutorials. But to start off we can quickly explain some of the keywords in one line. Note this is not complete listing of Keywords:
Feature: Defines what feature you will be testing in the tests below
Given: Tells the pre-condition of the test
And: Defines additional conditions of the test
Then: States the post condition. You can say that it is expected result of the test.
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. On TDD those tests were pure Java tests, in your case those might be a C++ or C# tests. But the basic idea is that those are core technical tests.
If we now come back to BDD/BRDSL we will see that we are able to describe tests in a more readable format. In the above test it’s quite clear and evident, just by reading, what test would do. At the same time of being a test it also documents the behavior of application. This is the true power of BDD/BRDSL and it will become the power of cucumber eventually because cucumber works on the same principles.