Gherkin for test automation in agile
-
Upload
viresh-doshi -
Category
Software
-
view
1.344 -
download
3
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.
Aim• If you are doing Agile then it’s a no brainer to use
Gherkin to drive your automation efforts.
Background• Created by a clever chap called Dan North in 2009.
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"
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.
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
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.
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.
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
Where to start?• BDD starts with a conversation between
stakeholders.
• Goal is to determine the features and derive the
acceptance criteria.
• Determine the Done.
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.
Syntax: BackgroundBackground – Executed before each scenario.
Background:
Given I am logged in
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
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"
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 |
Syntax: WhenWhen – a test step that defines the ‘action’ performed
When I follow "Sign In"
Syntax: AndAnd – additional test step that defines the ‘action’
performed
And I fill in "EmailAddress" with
Syntax: ThenThen – test step that defines the ‘outcome’
Then I should see "Welcome Jonny"
Syntax: ButBut – additional test step that defines the ‘action’ or
‘outcome’
But I should see "Welcome Jonny"
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!
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.
TechnicalLets look at how Gherkin actually works in real life
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
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.
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"
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()• {• }• }
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
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";
}
Languages• All these languages are supported :
• PHP, Java, Ruby, Python, C Sharp
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.