Enjoy ‘MinkExtension’ for Behat

7 May

Note : This post may not work with recent Behat. Please use and my  Github repo for to get it working without any issue https://github.com/Shashikant86/BehatDemo

MinkExtension‘ has been just released which has additional services for Behat. This extension has ‘Mink’ instance for your ‘FeatureContext’ and ‘SubContext’. UI testers would be happy now, as they don’t need to create Mink instance every time in order to use Mink API’s.

Well, in order to use ‘MinkExtension’ you need latest Behat and Mink versions (Behat 2.4+ and Mink 1.4+). Don’t worry so much about the versions,  creator of Behat has taken care of all this with composer.

Ok. Let’s clone the MinkExtenion-Example repository from GitHub. See, everything is mentioned in README file.

git clone https://github.com/Behat/MinkExtension-example.git
Cloning into MinkExtension-example...
remote: Counting objects: 31, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 31 (delta 13), reused 31 (delta 13)
Receiving objects: 100% (31/31), 4.43 KiB, done.
Resolving deltas: 100% (13/13), done.

Navigate to directory & Get the composer and Install dependencies

cd MinkExtension-example
curl http://getcomposer.org/installer | php
moonstar:MinkExtension-example sjagtap$ curl http://getcomposer.org/installer | php
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100  7438  100  7438    0     0  52386      0 --:--:-- --:--:-- --:--:--  105k
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /Users/sjagtap/MinkExtension-example/composer.phar
Use it: php composer.phar

Install Dependencies

php composer.phar install
oonstar:MinkExtensionTest sjagtap$ php composer.phar install
Installing dependencies
- Package symfony/yaml (dev-master)
Cloning 48fcffece8de2ade6f6dbc909f22444356b7659f

- Package symfony/translation (dev-master)
Cloning 34b64346b24024d54f42877b7c34ccc05ff1eaa8

- Package symfony/event-dispatcher (dev-master)
Cloning 0c1ae4898196f5e96b79028d8d2f35de4b584659

- Package symfony/dependency-injection (dev-master)
Cloning b01877ddbde08c1eded2502a6fdce8dbde89bd35

- Package symfony/config (dev-master)
Cloning 2f2fb45da8100bc03d2d9cbcd75e3ff36e325b89

- Package symfony/finder (2.0.x-dev)
Cloning v2.0.13

- Package symfony/css-selector (2.0.x-dev)
Cloning v2.0.12

- Package symfony/dom-crawler (2.0.x-dev)
Cloning v2.0.13

- Package symfony/browser-kit (2.0.x-dev)
Cloning v2.0.12

- Package symfony/process (dev-master)
Cloning 1bc1398d51180fd2aeded9c44f4d7b73bea817cd

- Package symfony/console (dev-master)
Cloning 8d58af16c425977018293eb0a1570c9629b37994

- Package behat/gherkin (2.2.x-dev)
Cloning ada4bda5823ffe1df8228e9dddefa38d6420f983

- Package zendframework/zend-registry (2.0.0beta3)
Downloading: 100%

- Package zendframework/zend-loader (2.0.0beta3)
Downloading: 100%

- Package zendframework/zend-stdlib (2.0.0beta3)
Downloading: 100%

- Package zendframework/zend-validator (2.0.0beta3)
Downloading: 100%

- Package zendframework/zend-http (2.0.0beta3)
Downloading: 100%

- Package zendframework/zend-uri (2.0.0beta3)
Downloading: 100%

- Package facebook/php-webdriver (master-dev)
Cloning master

- Package fabpot/goutte (master-dev)
Cloning master

- Package behat/mink-extension (dev-master)
Cloning cd8eb2c85859f7d5e88e5787d4e171eaa52002eb

- Package behat/mink (v1.4.0beta2)
Downloading: 100%

- Package behat/behat (v2.4.0beta3)
Downloading: 100%

symfony/event-dispatcher suggests installing symfony/http-kernel (dev-master)
behat/mink suggests installing behat/sahi-client (Sahi-Client is needed to use the SahiDriver)
behat/mink suggests installing alexandresalome/php-selenium (Php-Selenium is needed to use the SeleniumDriver)
behat/behat suggests installing behat/yii-extension (for integration of with Yii framework)
Writing lock file
Generating autoload files

Now Run Behat command,

./bin/behat

Look, if you still using PHP5.3.x Then you will get

moonstar:MinkExtension-example sjagtap$ ./bin/behat
PHP Parse error:  syntax error, unexpected T_USE, expecting T_FUNCTION in /Users/sjagtap/MinkExtension-example/features/bootstrap/TraitedFeatureContext.php on line 5
PHP Stack trace:
PHP   1. {main}() /Users/sjagtap/MinkExtension-example/vendor/behat/behat/bin/behat:0
PHP   2. Symfony\Component\Console\Application->run() /Users/sjagtap/MinkExtension-example/vendor/behat/behat/bin/behat:27
PHP   3. Behat\Behat\Console\BehatApplication->doRun() /Users/sjagtap/MinkExtension-example/vendor/symfony/console/Symfony/Component/Console/Application.php:106
PHP   4. Symfony\Component\Console\Application->doRun() /Users/sjagtap/MinkExtension-example/vendor/behat/behat/src/Behat/Behat/Console/BehatApplication.php:93
PHP   5. Symfony\Component\Console\Command\Command->run() /Users/sjagtap/MinkExtension-example/vendor/symfony/console/Symfony/Component/Console/Application.php:193
PHP   6. Behat\Behat\Console\Command\BaseCommand->initialize() /Users/sjagtap/MinkExtension-example/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:225
PHP   7. Behat\Behat\Console\Processor\AggregateProcessor->process() /Users/sjagtap/MinkExtension-example/vendor/behat/behat/src/Behat/Behat/Console/Command/BaseCommand.php:55
PHP   8. Behat\Behat\Console\Processor\LocatorProcessor->process() /Users/sjagtap/MinkExtension-example/vendor/behat/behat/src/Behat/Behat/Console/Processor/AggregateProcessor.php:57

This is time to update your PHP version. If you still want run features with same PHP version, then comment out/ remove everything from ‘TraitedFeatureContext.php’ which is for PHP5.4

Now,  Start Selenium server & Run behat with ‘inheritance’  or  ‘subcontext’ profile.

./bin/behat -p=inheritance
./bin/behat -p=subcontexts

You will see test running with headless as well as with browser. You will see output like this

moonstar:MinkExtension-example sjagtap$ ./bin/behat -p=subcontexts
Feature: Search
  In order to see a word definition
  As a website user
  I need to be able to search for a word

  Scenario: Searching for a page that does exist               # features/search.feature:6
    Given I am on "/wiki/Main_Page"                            # Behat\MinkExtension\Context\MinkContext::visit()
    When I fill in "search" with "Behavior Driven Development" # Behat\MinkExtension\Context\MinkContext::fillField()
    And I press "searchButton"                                 # Behat\MinkExtension\Context\MinkContext::pressButton()
    Then I should see "agile software development"             # Behat\MinkExtension\Context\MinkContext::assertPageContainsText()

  Scenario: Searching for a page that does NOT exist           # features/search.feature:12
    Given I am on "/wiki/Main_Page"                            # Behat\MinkExtension\Context\MinkContext::visit()
    When I fill in "search" with "Glory Driven Development"    # Behat\MinkExtension\Context\MinkContext::fillField()
    And I press "searchButton"                                 # Behat\MinkExtension\Context\MinkContext::pressButton()
    Then I should see "Search results"                         # Behat\MinkExtension\Context\MinkContext::assertPageContainsText()

  @javascript
  Scenario: Searching for a page with autocompletion           # features/search.feature:19
    Given I am on "/wiki/Main_Page"                            # Behat\MinkExtension\Context\MinkContext::visit()
    When I fill in "search" with "Behavior Driv"               # Behat\MinkExtension\Context\MinkContext::fillField()
    And I wait for the suggestion box to appear                # SubcontextedFeatureContext::iWaitForTheSuggestionBoxToAppear()
    Then I should see "Behavior Driven Development"            # Behat\MinkExtension\Context\MinkContext::assertPageContainsText()

3 scenarios (3 passed)
12 steps (12 passed)
0m14.59s

What’s new for Testers in MinkExtension?

  • You will get Mink instance in your ‘FeatureContext’ as well as ‘SubContexts’ classes.
  • You can extend Base Contextfile to MinkContext as shown in below. For other context you need to extend from RawMinkContext
class MyContext extends Behat\MinkExtension\Context\MinkContext
  • You can use MinkExtension as a SubContext in your files like this:
class SubcontextedFeatureContext extends Behat\Behat\Context\BehatContext
{
public function __construct()
{
$this->useContext('mink', new Behat\MinkExtension\Context\MinkContext);
}

This extension is a base for future extensions. We can extend Behat/Mink with any other services testing services like SauceLabs and many more.  This feature will definitely useful for Web Testers using Behat and Mink for browser automation. SauceLabs extension is on it’s way..  stay tuned :)

Happy BDD !!!

About these ads

2 Responses to “Enjoy ‘MinkExtension’ for Behat”

  1. stof70 May 7, 2012 at 8:08 pm #

    A few comments about this (great) post:

    - you don’t need to sudo the call to composer. The installation does not require accessing special folders (only your home directory and the current one)
    - You can only extend from MinkContext once as it contains the step definitions (which cannot be duplicated in the contexts used by Behat). For other contexts, you need to extend from RawMinkContext (the parent class giving access to Mink but without the step definitions)

    • shashikant86 May 7, 2012 at 9:22 pm #

      Thanks Stof70, I have updated post accordingly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: