Gherkin for test automation in agile

Post on 21-Jan-2018

1.344 views 3 download

Transcript of Gherkin for test automation in agile

GherkinAutomate your testing using Gherkin for

BDD

About• This slide deck is about the Gherkin language which

is used to drive test automation.

vireshdoshi@time2test.co.uk 2

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

Gherkin to drive your automation efforts.

vireshdoshi@time2test.co.uk 3

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

vireshdoshi@time2test.co.uk 4

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 “jonny.jones@gbmail.com"

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"

vireshdoshi@time2test.co.uk 5

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.

vireshdoshi@time2test.co.uk 6

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

vireshdoshi@time2test.co.uk 7

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.

vireshdoshi@time2test.co.uk 8

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.

vireshdoshi@time2test.co.uk 9

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

vireshdoshi@time2test.co.uk 10

Where to start?• BDD starts with a conversation between

stakeholders.

• Goal is to determine the features and derive the

acceptance criteria.

• Determine the Done.

vireshdoshi@time2test.co.uk 11

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.

vireshdoshi@time2test.co.uk 12

Syntax: BackgroundBackground – Executed before each scenario.

Background:

Given I am logged in

vireshdoshi@time2test.co.uk 13

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

“jonny.jones@gbmail.com"

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"

vireshdoshi@time2test.co.uk 14

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 |

vireshdoshi@time2test.co.uk 15

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

Given I am on "/"

vireshdoshi@time2test.co.uk 16

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

When I follow "Sign In"

vireshdoshi@time2test.co.uk 17

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

performed

And I fill in "EmailAddress" with

“jonny.jones@gbmail.com"

vireshdoshi@time2test.co.uk 18

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

Then I should see "Welcome Jonny"

vireshdoshi@time2test.co.uk 19

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

‘outcome’

But I should see "Welcome Jonny"

vireshdoshi@time2test.co.uk 20

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!

vireshdoshi@time2test.co.uk 21

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.

vireshdoshi@time2test.co.uk 22

TechnicalLets look at how Gherkin actually works in real life

vireshdoshi@time2test.co.uk 23

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

vireshdoshi@time2test.co.uk 24

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.

vireshdoshi@time2test.co.uk 25

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 "simon.jones@mailinator.com"

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"

vireshdoshi@time2test.co.uk 26

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()• {• }• }

vireshdoshi@time2test.co.uk 27

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

vireshdoshi@time2test.co.uk 28

Custom stepAnd I fill in "EmailAddress" with a unique email simon.jones@mailinator.com

/**

* @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";

}

vireshdoshi@time2test.co.uk 29

Languages• All these languages are supported :

• PHP, Java, Ruby, Python, C Sharp

vireshdoshi@time2test.co.uk 30

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.

vireshdoshi@time2test.co.uk 31