Gherkin for test automation in agile

31
Gherkin Automate your testing using Gherkin for BDD

Transcript of Gherkin for test automation in agile

Page 1: Gherkin for test automation in agile

GherkinAutomate your testing using Gherkin for

BDD

Page 2: Gherkin for test automation in agile

About• This slide deck is about the Gherkin language which

is used to drive test automation.

[email protected] 2

Page 3: Gherkin for test automation in agile

Aim• If you are doing Agile then it’s a no brainer to use

Gherkin to drive your automation efforts.

[email protected] 3

Page 4: Gherkin for test automation in agile

Background• Created by a clever chap called Dan North in 2009.

[email protected] 4

Page 5: Gherkin for test automation in agile

In a nutshellFeature: Log in and out of the site.

In order to maintain an account

As a site visitor

I need to log in and out of the site.

@monsterlogin

Scenario: Logs in to the site

Given I am on "/"

When I follow "Sign In"

And I fill in "EmailAddress" with “[email protected]"

And I fill in "Password" with “MyPassword123"

And I press "Sign In"

Then I should see "Welcome Jonny"

And I should see "Jobs You Might Like"

[email protected] 5

Page 6: Gherkin for test automation in agile

What is it?• Business readable domain specific language.

• It uses a very simple syntax.

• Tackles the business needs.

• Aims to automate the main business scenarios.

• Outside in design philosophy.

• Software practices – evolved.

• Compliments the Agile/SCRUM process.

• Starting point for test automation.

[email protected] 6

Page 7: Gherkin for test automation in agile

What it isn’t?• It’s not a programming language.

• It’s not a unit testing tool.

• It’s got nothing to do with Kebabs

[email protected] 7

Page 8: Gherkin for test automation in agile

What is BDD?• It stands for Behavior Driven Development.

• Think TDD – test driven development for

development driven by testing.

• Describe the behaviors and then develop to satisfy

those behaviors.

[email protected] 8

Page 9: Gherkin for test automation in agile

BDD vs. Traditional• Test automation team usually trawl through

requirements documents and automate in similar

fashion to how developers develop. This introduces

ambiguity between how test and dev interpret

requirements.

• Usually automation is done after the developers.

• The Gherkin syntax ( acceptance criteria) is the

requirements which are deemed to be signed off

between dev, test and business.

[email protected] 9

Page 10: Gherkin for test automation in agile

Benefits• Plain business text.

• Understood by business and developers

• Targets the business requirements.

• Significant proportion of the functional

specifications is written as user stories.

• Small command set to learn.

• Just enough living documentation

[email protected] 10

Page 11: Gherkin for test automation in agile

Where to start?• BDD starts with a conversation between

stakeholders.

• Goal is to determine the features and derive the

acceptance criteria.

• Determine the Done.

[email protected] 11

Page 12: Gherkin for test automation in agile

Syntax: FeatureFeature – The text acts as the high level information for

the test. Nothing gets processed.

Feature: Log in and out of the site.

In order to maintain an account

As a site visitor

I need to log in and out of the site.

[email protected] 12

Page 13: Gherkin for test automation in agile

Syntax: BackgroundBackground – Executed before each scenario.

Background:

Given I am logged in

[email protected] 13

Page 14: Gherkin for test automation in agile

Syntax: ScenarioScenario – Main part of the BDD feature file with test

steps.

Scenario: Logs in to the site

Given I am on "/"

When I follow "Sign In"

And I fill in "EmailAddress" with

[email protected]"

And I fill in "Password" with “MyPassword123"

And I press "Sign In"

Then I should see "Welcome Jonny"

And I should see "Jobs You Might Like"

[email protected] 14

Page 15: Gherkin for test automation in agile

Syntax: Scenario OutlineScenario Outline – use with examples syntax and executes the scenario multiple times.

@wip

Scenario Outline: Click through all the account pages and assert

present

Given I click on account "editProfile"

Then I should see "Contact Information"

And I follow "<account_link>"

Then I should see "<expected_text>"

Examples:

| account_link | expected_text |

| Photo | Choose a photo for your professional profile. |

| Education | Degree Level |

[email protected] 15

Page 16: Gherkin for test automation in agile

Syntax: GivenGiven – a test step that defines the ‘context’

Given I am on "/"

[email protected] 16

Page 17: Gherkin for test automation in agile

Syntax: WhenWhen – a test step that defines the ‘action’ performed

When I follow "Sign In"

[email protected] 17

Page 18: Gherkin for test automation in agile

Syntax: AndAnd – additional test step that defines the ‘action’

performed

And I fill in "EmailAddress" with

[email protected]"

[email protected] 18

Page 19: Gherkin for test automation in agile

Syntax: ThenThen – test step that defines the ‘outcome’

Then I should see "Welcome Jonny"

[email protected] 19

Page 20: Gherkin for test automation in agile

Syntax: ButBut – additional test step that defines the ‘action’ or

‘outcome’

But I should see "Welcome Jonny"

[email protected] 20

Page 21: Gherkin for test automation in agile

Test steps• Given , When, Then, And , But are test steps

• They can be used interchangeably. The interpreter

doesn’t care what is used however they may not

make ‘sense’ when read!

[email protected] 21

Page 22: Gherkin for test automation in agile

TermsUser story – agile term used to describe an end to end

journey of a feature

Cucumber – term used to describe the interpreter

used to process Gherkin syntax

Tear Down – term used in automation to describe the

series of actions executed at the end of scenario

execution.

Setup – term used in automation to describe the series

of actions executed at the beginning of scenario

execution.

[email protected] 22

Page 23: Gherkin for test automation in agile

TechnicalLets look at how Gherkin actually works in real life

[email protected] 23

Page 24: Gherkin for test automation in agile

Demo• Demo will show the Gherkin files come to life using

Behat and Mink on a PHP using Selenium Webdriver

• The files for this tutorial are stored on GITHUB

• https://github.com/VireshDoshi/behat3-mink-

website

[email protected] 24

Page 25: Gherkin for test automation in agile

Behat and Mink• Behat is a PHP tool that interprets.

• Mink is a library that interfaces with Web Browsers.

• Mink Extensions is a library that contains useful

Gherkin steps especially designed for the web.

[email protected] 25

Page 26: Gherkin for test automation in agile

Feature file• Out of the box, The following feature file requires minimum coding

Feature: Log in and out of the site.

In order to maintain an account

As a site visitor

I need to log in and out of the site.

@monsterlogin

Scenario: Logs in to the site

Given I am on "/"

When I follow "Sign In"

And I fill in "EmailAddress" with "[email protected]"

And I fill in "Password" with "12Xxxxxxxxx"

And I press "Sign In"

Then I should see "Welcome Simon"

And I should see "Jobs You Might Like"

[email protected] 26

Page 27: Gherkin for test automation in agile

FeatureContext.php• This special Behat file contains the code that connects the Behat feature file to PHP code and Mink • <?php

• use Behat\Behat\Tester\Exception\PendingException;• use Behat\Behat\Context\Context;• use Behat\Behat\Context\SnippetAcceptingContext;• use Behat\Gherkin\Node\PyStringNode;• use Behat\Gherkin\Node\TableNode;• use Behat\MinkExtension\Context\MinkContext;

• /**• * Defines application features from the specific context.• */• class FeatureContext extends MinkContext implements SnippetAcceptingContext• {• /**• * Initializes context.• *• * Every scenario gets its own context instance.• * You can also pass arbitrary arguments to the• * context constructor through behat.yml.• */• public function __construct()• {• }• }

[email protected] 27

Page 28: Gherkin for test automation in agile

Behat.yml• This special file contains setup details for Behatdefault:

extensions:

Behat\MinkExtension:

base_url: 'http://www.monster.co.uk'

browser_name: firefox

sessions:

default:

selenium2: ~

suites:

backend:

contexts:

- FeatureContext:

screen_shot_path: ./screenshot

[email protected] 28

Page 29: Gherkin for test automation in agile

Custom stepAnd I fill in "EmailAddress" with a unique email [email protected]

/**

* @Then I fill in :arg1 with a unique email :arg2

*/

public function iFillInWithAUniqueEmail($in_field, $in_email)

{

$session = $this->getSession();

$page=$session->getPage();

$random_num=mt_rand();

$parts= explode('@',$in_email);

$user=$parts[0];

$domain=$parts[1];

$email_element=$page->find('css','#' . $in_field);

$random_email = $user . $random_num . '@' . $domain;

$email_element->setValue($random_email);

echo "generated random email = [" . $random_email . "]\n";

}

[email protected] 29

Page 30: Gherkin for test automation in agile

Languages• All these languages are supported :

• PHP, Java, Ruby, Python, C Sharp

[email protected] 30

Page 31: Gherkin for test automation in agile

Conclusion• Technically, in 5 simple steps, BDD can be

introduced to your project.

• If you are doing Agile then it’s a no brainer to use

Gherkin to drive your automation efforts.

[email protected] 31