behat in Selenium in php

This topic covers how to write the Selenium tests in behat (BDD framework just like cucumber). Just follow below steps.
  • Create composer.json file and mention all dependencies. Then install all dependencies.
  • Create a features directory and store features in it.
  • Create step definitions.

Installing all dependencies

Create composer.json file with below contents.

    “require-dev”: {
    “phpunit/phpunit”: “*”,
    “facebook/webdriver”: “dev-master”,
    “behat/behat”: “2.4.*@stable”
    “minimum-stability”: “dev”,
    “config”: {
    “bin-dir”: “bin/”
Then install the dependencies using composer.
php composer.phar install

Creating feature file

Then Create features directory and create a feature file in it.
Feature: Title check of pages

Scenario: Check that the title of the home page is “Tutorial”

Given I am on the “” webpage
Then I verify that title contains “Tutorial”

Creating step definitions

Then create a bootstrap directory inside features directory and store FeatureContext.php file in it. This file contains step definitions.

use BehatBehatContextBehatContext,

use BehatGherkinNodePyStringNode,

class FeatureContext extends BehatContext {

protected $driver;

/** @BeforeScenario */
public function before($event)
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->driver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);


* @Given /^I am on the “([^”]*)” webpage$/
public function iAmOnTheWebpage($page)

* @Then /^I verify that title contains “([^”]*)”$/
public function iVerifyThatTitleContains($title)

// checking that page title contains word ‘Tutorial’
PHPUnit_Framework_TestCase::assertContains($title, $this->driver->getTitle());

/** @AfterScenario */
public function after($event)


To execute the features, you can use below syntax.

Web development and Automation testing

solutions delivered!!