Installing and using Mink with Browserkit driver

1.7k views Asked by At

I seem to be unable to install Mink with Browserkit driver on Centos. I am using these instructions: https://github.com/minkphp/MinkBrowserKitDriver

The steps I am taking is by:

  1. adding a file in my project directory with the name composer.json and the contents:

    { "require": { "behat/mink": "~1.5", "behat/mink-browserkit-driver": "~1.1" } }

  2. Use the commands as below.

$> curl -sS https://getcomposer.org/installer | php

$> php composer.phar install

  1. Now there are 3 files (composer.json, composer.lock, composer.phar) and one folder (vendor) in the project directory. Where do I run the "Usage example" code from (as on the documentation)?

I have tried adding require_once "vendor/autoload.php"; to my test.php file:

<?php
require_once "vendor/autoload.php";
use Behat\Mink\Mink,
    Behat\Mink\Session,
    Behat\Mink\Driver\BrowserKitDriver;

use Symfony\Component\HttpKernel\Client;

$app  = require_once(__DIR__.'/app.php'); // Silex app

$mink = new Mink(array(
    'silex' => new Session(new BrowserKitDriver(new Client($app))),
));

$mink->getSession('silex')->getPage()->findLink('Chat')->click();

but getting a fatal error that app.php cannot be opened. I have also tried adding the following to test.php:

require_once 'vendor/behat/mink-browserkit-driver/tests/app.php';

Any help would be appreciated :)

2

There are 2 answers

0
mTorres On

Its seems you're missing some guideline in order to organize your code. Before integrating Behat and Mink, first of all you should organize your Silex project. My advice is for you to take a look at the official Silex Skeleton project.

After that you can start by installing behat, mink and your driver:

cd path/to/your/silex/project/root
composer require behat/behat:~2.5 behat/mink behat/mink-browserkit-driver

Then you can initialize behat.

bin/behat --init

Then configure your mink driver in behat.yml (on your project root directory)

default:
  extensions:
    Behat\MinkExtension\Extension:
      browserkit: ~
      base_url: http://my.dev.host

Notice that browser kit cannot execute JS, remember that (if you want to execute JS on your tests, you should install another driver)

After that you can start writing your features on the features directory (behat should've created that for you), for example if you have this controller in src/controllers.php:

<?php
//...
$app->get('/hello', function () use ($app) {
  return new Response("Hello world!");
});

You can write the feature (on features/greeting.feature):

Feature: Greetings from /hello page
  In order to say hello world
  As a visitor
  I need to go to the /hello page and see Hello world!

  Scenario: See Hello world!
    Given I am on "/hello"
    Then I should see "Hello world!"
0
Thomas On

Another option is to use full behat extension for silex: https://github.com/tabbi89/Behat-Silex-Extension

You can check how integrations of Mink and browserKit work there.