Sei sulla pagina 1di 9

Gherkin

Gherkin is a plain English text language, which helps the tool - Behat/Cucumber to
interpret and execute the test scripts.

Gherkin provides the common set of keywords in English text, which can be used by
people amongst the different community and yet get the same output in the form of test
scripts.

Let’s start with the example:

Feature: Google Searching


As a web surfer, I want to search Google, so that I can learn new things.

Scenario: Simple Google search


Given a web browser is on the Google page
When the search phrase "panda" is entered
Then results for "panda" are shown

This is a complete feature file. It starts with a required Feature section and a
description. The description is optional, and it may have as many or as few lines as
desired. The description will not affect automation at all – think of it as a comment. As
an Agile best practice, it should include the user story for the features under test. This
feature file then has one Scenario section with a title and one each of Given–When–
Then steps in order. It could have more scenarios, but for simplicity, this example has
only one. The indents and blank lines also make the feature file easy to read
Tags Tags are a great way to classify scenarios. They can be used to selectively run
tests based on tag name, and they can be used to apply before-and-after wrappers
around scenarios. Most BDD frameworks support tags. Any scenario can be given tags
like this:

Feature: Google Searching


As a web surfer, I want to search Google, so that I can learn new things.

@automated @google @panda


Scenario: Simple Google search
Given a web browser is on the Google page
When the search phrase "panda" is entered
Then results for "panda" are shown

Tags start with the “@” symbol. Tag names are case-sensitive and whitespace-
separated. As a best practice, they should be lowercase and use hyphens (“-“) between
separate words. Tags must be put on the line before a Scenario or Scenario Outline
section begins. Any number of tags may be used
Steps Definitions
We have got our feature file ready with the test scenarios defined. However, this is not
the complete job done. Behat doesn’t really know which piece of code is to be
executed for any specific scenario outlined in a feature file.

This calls the need of an intermediate – Step Definition file. Steps definition file stores
the mapping between each step of the scenario defined in the feature file with a code
of function to be executed.

So, now when Behat executes a step of the scenario mentioned in the feature file, it
scans the step definition file and figures out which function is to be called.

Example of Step Definition File:


public void goToFacebook() {
driver = new FirefoxDriver();
driver.navigate().to("https://www.facebook.com/");
}
@When "^user logs in using Username as \"([^\"]*)\" and Password as
\"([^\"]*)\"$"
public void I_enter_Username_as_and_Password_as(String arg1, String
arg2) {
driver.findElement(By.id("email")).sendKeys(arg1);
driver.findElement(By.id("pass")).sendKeys(arg2);
driver.findElement(By.id("u_0_v")).click();
}
@Then"^login should be unsuccessful$"
public void validateRelogin() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test Pass");
} else {
System.out.println("Test Failed");
}
driver.close();
}
Behat Automation Framework setup.
Behat is a testing tool that makes behavior driven development (BDD) possible. We can write human-
readable stories/test script that describe the behavior of your application. Here is the installation steps follows

Step 1:

Create a folder as you want in any drive. I have created folder “alp_behat” in D:/alp_behat.

Step 2:

Open the command prompt direct to D:/alp_behat > and type the following command to download
the ‘composer.phar’

“curl -sS https://getcomposer.org/installer | php”

Step 3:

Create a JSON file “composer.json” defining your dependencies as given below inside it. This will install all
specified dependencies in ‘composer.json’

{
"require": {
"behat/behat": "@stable",
"behat/mink": "@stable",
"behat/mink-extension": "@stable",
"behat/mink-goutte-driver": "*",
"behat/mink-selenium2-driver": "*"
Step 4: },
"config": {
"bin-dir":“php
Run Composer: composer.phar install”
"bin/"
}
}
Step 5:

This will take some time to install all dependencies mentioned in the composer.json and creates the vendor
folder and bin folder.
Step 6:

Add the Behat path "D:\alp_behat\bin" to your system path variable, as follows:
● Right click "Computer" and select "Properties"
● Under "Control Panel Home" select "Advanced system settings"
● In the "Advanced" tab navigate to the bottom and select "Environment Variables"
● In the "System variables" section scroll down and select "Path" and then select the "Edit" button
● Ensure you are at the end of the "Variable value" field and add this to the end of the "Path" value ";
D:\alp_behat\bin;".

Step 7:

Test that Behat has installed correctly by doing the below steps.
Open a new command prompt window and type "Behat --version" (without the quotes). You should see
'Behat version DEV'.

Step 8:

In the same command window, navigate to "D:\alp_behat".

Type "behat --init" (without the quotes).

This will create:

- A features folder,
- A bootstrap folder,
- And the FeatureContext.php file inside the bootstrap folder.

Now your behat installation is complete. Add the feature files to the feature folder (ex: demo.feature).

Step 9:

You need Selenium Server and the corresponding browser drivers (Chromedriver) to test your application on
different browsers.

Server: ‘selenium-server-standalone-2.53.1.jar’
Driver: ‘Chromedriver.exe’

These can be downloaded from ‘http://www.seleniumhq.org/download/’ and ensure these are kept in the
project folder.

Step 10:

The below browser versions are compatible with ‘selenium-server-standalone-2.53.1.jar’.


Firefox: V46.0
Chrome: V54.0
Behat Execution:
To start Selenium Server to launch Firefox.
In command prompt goto the loacation of the Seleniumserverstandalone jar file and type the following.
“java -jar D:\BFW\Drivers\selenium-server-standalone-2.53.1.jar”

To start selenium Server to Launch Chrome.


In command prompt goto the loacation of the Seleniumserverstandalone jar file and type the following.
java -jar D:\BFW\Drivers\selenium-server-standalone-2.53.1.jar -
Dwebdriver.chrome.driver="D:\BFW\Drivers\chromedriver.exe"

To execute the behat feature in cmd prompt:

In the command prompt go to your project location “D:\alp_behat” and enter below command
“Behat features\demo.feature”

The browser is launch and the scripts runs.

To get HTML reports use the command:


“Behat features\demo.feature –format=HTML –out=behatreport.html

Reference Source:
1. http://docs.behat.org/en/latest/user_guide/initialize.html
2. https://github.com/composer/composer
3. https://github.com/Behat/MinkExtension
4. http://getcomposer.org/download/

Potrebbero piacerti anche