4 You can use Behat to describe anything, that you can describe in business
5 logic. It’s tools, gui applications, web applications. Most interesting part
6 is web applications. First, behavioral testing already exists in web world -
7 it’s called functional or acceptance testing. Almost all popular frameworks
8 and languages provide functional testing tools. Today we’ll talk about how to
9 use Behat for functional testing of web applications. `Mink <http://mink.behat.org>`_
10 is a tool exactly for that and this extension provides integration for it.
12 Basically, MinkExtension is an integration layer between Behat 3.0+ and Mink 1.4+
15 * Additional services for Behat (``Mink``, ``Sessions``, ``Drivers``).
16 * ``Behat\MinkExtension\Context\MinkAwareContext`` which provides ``Mink``
17 instance for your contexts.
18 * Base ``Behat\MinkExtension\Context\MinkContext`` context which provides base
19 step definitions and hooks for your contexts or subcontexts. Or it could be
20 even used as context on its own.
25 This extension requires:
33 The easiest way to keep your suite updated is to use `Composer <http://getcomposer.org>`_:
35 1. Install with composer:
39 $ composer require --dev behat/mink-extension
41 2. Activate extension by specifying its class in your ``behat.yml``:
50 base_url: 'http://example.com'
58 After installing extension, there would be 6 usage options available for you:
60 1. Extending ``Behat\MinkExtension\Context\RawMinkContext`` in your feature suite.
61 This will give you ability to use a preconfigured `Mink` instance altogether with some
64 * ``getSession($name = null)``
65 * ``assertSession($name = null)``
67 ``RawMinkContext`` doesn't provide any hooks or definitions, so you can inherit from it
68 in as many contexts as you want - you'll never get ``RedundantStepException``.
70 2. Extending ``Behat\MinkExtension\Context\MinkContext`` with one of your contexts.
71 Exactly like the previous option, but also provides lots of predefined step definitions out
72 of the box. As this context provides step definitions and hooks, you can use it **only once**
73 inside your feature context tree.
79 use Behat\MinkExtension\Context\MinkContext;
81 class FeatureContext extends MinkContext
84 * @Then /^I wait for the suggestion box to appear$/
86 public function iWaitForTheSuggestionBoxToAppear()
88 $this->getSession()->wait(5000, "$('.suggestions-results').children().length > 0");
94 Keep in mind, that you can not have multiple step definitions with the same regex.
95 It will cause a ``RedundantException``. So, you can inherit from ``MinkContext``
96 only with one of your context/subcontext classes.
98 3. Adding ``Behat\MinkExtension\Context\MinkContext`` as context in your suite.
99 Exactly like previous option, but gives you the ability to keep your main context
109 - Behat\MinkExtension\Context\MinkContext
113 Keep in mind, that you can not have multiple step definitions with the same regex.
114 It will cause a ``RedundantException``. So, you can inherit from ``MinkContext``
115 only with one of your context/subcontext classes.
117 4. Implementing ``Behat\MinkExtension\Context\MinkAwareContext`` with your context.
119 There are common things between these methods. In each of those, the target context will implement
120 ``setMink(Mink $mink)`` and ``setMinkParameters(array $parameters)`` methods. Those methods would
121 be automatically called **immediately after** each context creation before each scenario. And
122 this ``$mink`` instance will be preconfigured based on the settings you've provided in your
128 MinkExtension comes with a flexible configuration system, that gives you
129 the ability to configure Mink inside Behat to fulfil all your needs.
134 You can register as many Mink sessions as you want. For each session, you
135 will need to choose the driver you want to use.
150 MinkExtension will set the default Mink session for each scenario based on
151 the configuration settings ``default_session`` and ``javascript_session``
152 and on scenario tags:
154 * A scenario tagged with ``@mink:foo`` will use ``foo`` as default session;
155 * A scenario tagged with ``@javascript`` will use the javascript session as default session;
156 * Other scenarios will use the default session.
158 The default session and the default javascript session can also be configured for
167 mink_javascript_session: sahi
169 If it is not configured explicitly, the javascript session is set to the first
170 session using a javascript driver in the order of the configuration (it would
171 be ``first_session`` in the example above as ``selenium2`` supports Javascript).
172 If it is not configured explicitly, the default session is set to the first
173 session using a non-javascript driver if any, or to the first javascript session
174 otherwise (it would be ``second_session`` above as ``goutte`` does not support
180 First of all, there are drivers enabling configuration. MinkExtension comes
181 with support for 6 drivers out of the box:
183 * ``GoutteDriver`` - headless driver without JavaScript support. In order to use
184 it, modify your ``behat.yml`` profile:
195 .. Tips : HTTPS and self-signed certificate
196 In case you use Behat/Mink/Goutte to test your application, and want to test an
197 application secured with HTTPS, but with a self-signed certificate, you can use
198 the following parameters to avoid the validation error triggered by Guzzle:
200 * For ``Guzzle 4`` or later:
213 * For ``Guzzle 3`` or earlier:
224 ssl.certificate_authority: false
226 * ``Selenium2Driver`` - javascript driver. In order to use it, modify your
227 ``behat.yml`` profile:
238 * ``SauceLabsDriver`` - special flavor of the Selenium2Driver configured to use the
239 selenium2 hosted installation of saucelabs.com. In order to use it, modify your
240 ``behat.yml`` profile:
251 * ``BrowserStackDriver`` - special flavor of the Selenium2Driver configured to use the
252 selenium2 hosted installation of browserstack.com. In order to use it, modify your
253 ``behat.yml`` profile:
264 * ``SeleniumDriver`` - javascript driver. In order to use it, modify your ``behat.yml``
276 * ``SahiDriver`` - javascript driver. In order to use it, modify your ``behat.yml``
288 * ``ZombieDriver`` - zombie.js javascript headless driver. In order to use it, modify
289 your ``behat.yml`` profile:
299 # Specify the path to the node_modules directory.
300 node_modules_path: /usr/local/lib/node_modules/
304 The phar version of Mink comes bundled with all 5 drivers and you don't need to do
305 anything except enabling them in order to use them.
307 But if you're using Composer, you need to install drivers that you need first:
309 - GoutteDriver - ``behat/mink-goutte-driver``
310 - SeleniumDriver - ``behat/mink-selenium-driver``
311 - Selenium2Driver (also used for Saucelabs) - ``behat/mink-selenium2-driver``
312 - SahiDriver - ``behat/mink-sahi-driver``
313 - ZombieDriver - ``behat/mink-zombie-driver``
317 All drivers share the same API, which means that you could use multiple drivers
318 for the same suite - which one fits your needs for concrete scenarios. Don't
319 try to stick to a single driver as there's simply no universal solution - every
320 driver has its pros and cons.
322 Additional Parameters
323 ~~~~~~~~~~~~~~~~~~~~~
325 There's other useful parameters, that you can use to configure your suite:
327 * ``base_url`` - if you're using relative paths in your ``*.feature`` files
328 (and you should), then this option will define which url to use as a basename
330 * ``files_path`` - there's a special step definition for file upload inputs
331 usage. You can use relative paths in those steps. ``files_path`` defines
332 base path in which Mink should search those relative files.
333 * ``show_cmd`` - there's a special definition in MinkExtension, that saves
334 currently opened page into temporary file and opens it with some browser
335 utility (for debugging). This option defines command to be used for opening.
336 For example: ``show_cmd: 'firefox %s'``.
337 * ``show_tmp_dir`` - the temporary folder used to show the opened page (defaults
338 to the system temp dir)
339 * ``show_auto`` - Whether the opened page should be shown automatically when
341 * ``browser_name`` - meta-option, that defines which browser to use for Sahi,
342 Selenium and Selenium2 drivers.
343 * ``default_session`` - defines default session (driver) to be used for all
344 untagged scenarios. Could be any enabled session name.
345 * ``javascript_session`` - defines javascript session (driver) (the one, which
346 will be used for ``@javascript`` tagged scenarios). Could be any enabled session
348 * ``mink_loader`` - path to a file loaded to make Mink available (useful when
349 using the PHAR archive for Mink, useless when using Composer)