Test Automation with Gherkin Scenarios

  November 08, 2022

A discussion with BDD advocate, former Test Automation Specialist, and SmartBear Senior Solutions Engineer Dermot Canniffe. Learn about Gherkin Syntax, best practices for writing Gherkin scenarios, and the value of automating Gherkin scenarios using TestComplete.

What is Gherkin Syntax?

Gherkin Syntax is a plain text language used to write test scenarios. It’s a way of expressing the desired behavior a user will enact when using your software. The idea is to have it expressed in plain language using a simple set of keywords. This makes it understandable across a broad spectrum of users.

With Gherkin, we write scenarios using the keywords Given-When-Then. With these keywords at the start of each of our steps, we categorize a flow of logic. We start with a Given, which is a prerequisite. The next step is When, which is an action that occurs when enacted by the user. Finally, Then is the outcome that we validate.

Here’s an example scenario:

What are the benefits of creating automated tests in this way?

In the past when we created tests, they were composed in script. Only those who were familiar with a scripting language could decipher the intended test outcome. Gherkin uses plain language to describe the test conditions, so it doesn't matter if you have a technical background or not. You’ll still be able to understand the expected outcome and the actual outcome of the software.

It’s going to have a broader understanding across a wider audience. So that’s the key benefit to using Gherkin; it’s relatable regardless of your technical background.

What are some best practices for writing Gherkin scenarios?

Learn how to write declarative scenarios and avoid imperative scenarios.

When I first started out, I used to write imperative scenarios. The scenario would start off; Given I can log into the application, When I do A, and B, and C, and D... And finally, I’d get to Then. What should have been a straightforward scenario of 3 to 5 lines ended up being 27 lines long. And if you were used to scripting, that made sense to you.

With Gherkin, we try to step back from all those details. We try to express the process in broader terms rather than getting down to the nitty gritty of what we need to perform. Little things may change from release to release, especially with an agile project. You don’t want to refactor your scenarios every time a small change is made.

That complexity is for the automation layer, which is the layer below your Gherkin. Everything else on the Gherkin level should be relatively abstract.

Here’s an example of an imperative scenario vs a declarative scenario:

Imperative Scenario:

Scenario: Library member reserves a book
  Given Andrew is a library member 
  And Lisa is library staff 
  And “Loaned book 1” is out on loan and is not overdue
  When she scans Andrew’s membership card 
  And she clicks on “reserve books”
  And she enters “Loaned book 1” in the text box 
  And she clicks “Search book” button
  Then “Loaned book 1” should be displayed in list box 
  When she selects “Loaned book 1” 
  And she clicks “Checkout” button
  Then she should charge Andrew $1	

Declarative Scenario:

Scenario: Library member reserves a book 
  Given Andrew is a Library Member  
  And “War and Peace” is out on loan  
  When he reserves “War and Peace”  
  Then he should be charged $1 

What is TestComplete?

TestComplete is a UI test automation tool for web, desktop, and mobile applications. It was designed to facilitate teams with varying technical backgrounds, allowing for code-based or codeless test creation.

You may have people on your team who are familiar with automation frameworks and scripting. You may have people who are unfamiliar with that. You want a tool that can facilitate people from both types of backgrounds simultaneously, while also reducing that learning curve.

TestComplete does this with name mapping, which allows you to create a model of your application while recording tests. The use of record and replay also allows for easy entry into test automation. Once you’ve recorded a test, you can modify it and augment it with new logic. Make your test units robust. Make them reusable. Make them modular.

How does TestComplete allow users to test with Gherkin?

If you want to start specifying tests using Gherkin syntax, you can import a Gherkin feature file or write it directly into TestComplete. From there, you can record or configure automated routines that correspond with each step in your scenario.

You also can build Gherkin on top of the recorded routines that you already have. Start by writing scenarios that express your requirements. Then bind those to existing automated routines that you have for keyword tests, scripts, or any other executable items in your project.

Want to automate Gherkin scenarios with TestComplete? Try it for free today.