Selenium and Cucumber 1: Writing BDD tests in gherkin syntax

By: Slav Kurochkin

Here is the question: why use Cucumber to run your Selenium WebDriver tests.

So what is Cucumber and how it is related to Test Automation?

Here is how wiki describes Cucumber - "Cucumber is a software tool that computer programmers use for testing other software. It runs automated acceptance tests written in a behavior-driven development (BDD) style."

Kind of confusing, isn't it? So what it have to do with QA? And what a hell is it behavior-driven development?
Well, lets try to understand it, past few years collaboration in Software companies where pushed a lot, that is why there is so many different methodologies: Agile, Kanban, Cleanroom etc. As you can see people in software teams finally started talk to each other, the only thing it is steal confusing when Business Analyst see the problem from one angel, and developers finding that BA was not clear enough and developing it the way they understand software should work, qa finding mismatch between acceptance criteria, software and test cases they created based on their vision. And that is actually where collaboration starting and teams starting realized that something went wrong, spending time on fixing issues and making stuff works as intended.

Hold on a second, why don't you guys (BA Developers and QA) meet at the beginning of development life cycle and talk, review acceptance criteria, ask questions and write down the answers. And yes please think as an end user and figure out, but please first find out who is the end user. Three of you would need to figure out:

1. Find out the feature and who is the user for that feature
2. Write his behavior and possible scenarios, using "given-when-than" gherkin syntax
2. Drive your application based on that behavior using Cucumber acceptance tests, unit tests and functional UI tests

We will to know what feature we want to develop, in gherking syntax it would look something like:
Feature: short enough but descriptive feature title

Then lets try to figure out how can we find out who is the user, is it actual end user or maybe some stakeholder. John Ferguson Smart explains in his book BDD in Action different ways to do it, I personally like the one bellow:
In order to [goal]
[stakeholder]
wants [behaviour]

Once we done with feature titles and stakeholders, lets try to find out Scenarios, for example we have blog search feature, there is few possible scenarios:

1. Scenario: user search for existing article
2. Scenario: user search for article which can not be found

Based on those scenario titles we can write user behaviors using Given When Then format, lets try to do it now with our example:

Scenario: user search for existing article
Given user on a home page
When user search for existing article
Then he can see it on search result page

Lets do the same thing for the negative test result

Scenario: user search for existing article
Given user on a home page
When user search for non existing article
Then he can not see it on search result page

As we can see steps is almost the same, which allow us to see what can be abstracted later on and used in multiple scenarios, pretty cool ah?!

Lets put everything together and see how our acceptance test would look in gherkin syntax in our Search.feature file:

In next series of articles we will discover:
- Cucumber and Gherkin relationship,
- How to drive Cucumber tests using Selenium WebDriver,
- BDD Test Automation design practices.

View Comments

Leave a Comment

Use markdown or basic HTML and be nice.