Tutorial 1 What Is Cucumber-BDD
Tutorial 1 What Is Cucumber-BDD
Look at above test case. We have described our test case in the form of a ‘behaviour’.
So our automation test case will check whether ‘Alice will be full if she eats 3
cucumbers’.
However, look at the below test case that was built using pure java syntax. To a
client/end-user, the syntax looks to be slighlty technical and is not so easy to understand
So by looking at above, it is difficult for the client to understand what exactly you have
automated, what test case feature has been automated. They can only guess what this
test case might be doing. So the business scenarios are missing in above script. So we
cannot say for sure what the above test case exactly does.
Instead, our test case can be described in this behaviour driven style (see below).
The above syntax is called as ‘Gherkin’ syntax and our client can easily understand it. The
syntax closely matches with the English language (having ‘Given’, ‘When’, Then’ etc
keywords). Most lines in a Gherkin syntax start with one of the keywords (Given, When
etc).
So you can understand the test case syntax as below:
Given explains what you have
When you perform some action
And you perform additional action
Then what is the result or outcome of that action
Below is another example of a test case in Gherkin syntax.
Scenario: All shopping done
So you can describe your test scenario using Gherkin syntax and you can link these lines
with actual java code.
E2E shopping cart Gherkin syntax
Consider below end to end test case:
Let us try to convert above steps into Gherkin syntax. So, below is the business
representation in BDD/cucumber world which is self-explanatory.
Recall that ‘And’ keyword is used to concatenate additional actions.
Scenario: E2E automation
Given I launch an e-commerce website
When I enter ‘Lenovo IdeaCentre 600 All-in-One PC’ in search field
And I search the item
And I add item to shopping cart
And I go to shopping cart
And I enter 2 in quantity field
And I update my shopping cart
Then 'Total' amount should be $1000
Scenario keyword
As you must have noticed in above test cases, in addition to other keywords, we also
used ‘Scenario’ keyword. A ‘Scenario’ describes intended behaviour of the test case. In
other words, a ‘Scenario’ describes what, not how.
Consider below test case.
The ‘Scenario’ is describing the what viz what the free subscribers will see. It does not
describe how the free subscribers will see only free articles.
Feature keyword
‘Feature’ represents a Test Suite. A test suite can have multiple test cases. Each scenario
represents one test case. So if you consider below example, we have a ‘Feature’ that
contains 2 test cases or 2 scenarios.
Linking Gherkin with java
Let us look at below test case again. We will ultimately be linking all our Gherkin syntax
with java code, as shown below.
Scenario: E2E automation
Given I launch an e-commerce website 🡪linked to
<some java codeto launch website> .get('https://demo.nopcommerce.com/')
When I enter ‘Lenovo IdeaCentre 600 All-in-One PC’ in search field 🡪linked to
<some java code to type text in the search box> .sendkeys('Lenovo IdeaCentre 600
All-in-One PC')
And I search the item 🡪linked to
<some java code to click the search button> .click()
And I add item to shopping cart 🡪linked to
<some java code to add item to cart> ('.product-box-add-to-cart-button').click()
And I go to shopping cart 🡪linked to
<some java code to click the shopping cart> ('.cart-label').click()
You would see an example of Gherkin and the corresponding java syntax, see below
References
You can refer the link https://cucumber.io/docs/gherkin/reference/ to read more about
Gherkin syntax