4 * This file is part of the Behat MinkExtension.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\MinkExtension\Context;
13 use Behat\Behat\Context\TranslatableContext;
14 use Behat\Gherkin\Node\TableNode;
17 * Mink context for Behat BDD tool.
18 * Provides Mink integration and base step definitions.
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
22 class MinkContext extends RawMinkContext implements TranslatableContext
26 * Example: Given I am on "/"
27 * Example: When I go to "/"
28 * Example: And I go to "/"
30 * @Given /^(?:|I )am on (?:|the )homepage$/
31 * @When /^(?:|I )go to (?:|the )homepage$/
33 public function iAmOnHomepage()
35 $this->visitPath('/');
39 * Opens specified page
40 * Example: Given I am on "http://batman.com"
41 * Example: And I am on "/articles/isBatmanBruceWayne"
42 * Example: When I go to "/articles/isBatmanBruceWayne"
44 * @Given /^(?:|I )am on "(?P<page>[^"]+)"$/
45 * @When /^(?:|I )go to "(?P<page>[^"]+)"$/
47 public function visit($page)
49 $this->visitPath($page);
53 * Reloads current page
54 * Example: When I reload the page
55 * Example: And I reload the page
57 * @When /^(?:|I )reload the page$/
59 public function reload()
61 $this->getSession()->reload();
65 * Moves backward one page in history
66 * Example: When I move backward one page
68 * @When /^(?:|I )move backward one page$/
70 public function back()
72 $this->getSession()->back();
76 * Moves forward one page in history
77 * Example: And I move forward one page
79 * @When /^(?:|I )move forward one page$/
81 public function forward()
83 $this->getSession()->forward();
87 * Presses button with specified id|name|title|alt|value
88 * Example: When I press "Log In"
89 * Example: And I press "Log In"
91 * @When /^(?:|I )press "(?P<button>(?:[^"]|\\")*)"$/
93 public function pressButton($button)
95 $button = $this->fixStepArgument($button);
96 $this->getSession()->getPage()->pressButton($button);
100 * Clicks link with specified id|title|alt|text
101 * Example: When I follow "Log In"
102 * Example: And I follow "Log In"
104 * @When /^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/
106 public function clickLink($link)
108 $link = $this->fixStepArgument($link);
109 $this->getSession()->getPage()->clickLink($link);
113 * Fills in form field with specified id|name|label|value
114 * Example: When I fill in "username" with: "bwayne"
115 * Example: And I fill in "bwayne" for "username"
117 * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
118 * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with:$/
119 * @When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
121 public function fillField($field, $value)
123 $field = $this->fixStepArgument($field);
124 $value = $this->fixStepArgument($value);
125 $this->getSession()->getPage()->fillField($field, $value);
129 * Fills in form fields with provided table
130 * Example: When I fill in the following"
131 * | username | bruceWayne |
132 * | password | iLoveBats123 |
133 * Example: And I fill in the following"
134 * | username | bruceWayne |
135 * | password | iLoveBats123 |
137 * @When /^(?:|I )fill in the following:$/
139 public function fillFields(TableNode $fields)
141 foreach ($fields->getRowsHash() as $field => $value) {
142 $this->fillField($field, $value);
147 * Selects option in select field with specified id|name|label|value
148 * Example: When I select "Bats" from "user_fears"
149 * Example: And I select "Bats" from "user_fears"
151 * @When /^(?:|I )select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/
153 public function selectOption($select, $option)
155 $select = $this->fixStepArgument($select);
156 $option = $this->fixStepArgument($option);
157 $this->getSession()->getPage()->selectFieldOption($select, $option);
161 * Selects additional option in select field with specified id|name|label|value
162 * Example: When I additionally select "Deceased" from "parents_alive_status"
163 * Example: And I additionally select "Deceased" from "parents_alive_status"
165 * @When /^(?:|I )additionally select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/
167 public function additionallySelectOption($select, $option)
169 $select = $this->fixStepArgument($select);
170 $option = $this->fixStepArgument($option);
171 $this->getSession()->getPage()->selectFieldOption($select, $option, true);
175 * Checks checkbox with specified id|name|label|value
176 * Example: When I check "Pearl Necklace" from "itemsClaimed"
177 * Example: And I check "Pearl Necklace" from "itemsClaimed"
179 * @When /^(?:|I )check "(?P<option>(?:[^"]|\\")*)"$/
181 public function checkOption($option)
183 $option = $this->fixStepArgument($option);
184 $this->getSession()->getPage()->checkField($option);
188 * Unchecks checkbox with specified id|name|label|value
189 * Example: When I uncheck "Broadway Plays" from "hobbies"
190 * Example: And I uncheck "Broadway Plays" from "hobbies"
192 * @When /^(?:|I )uncheck "(?P<option>(?:[^"]|\\")*)"$/
194 public function uncheckOption($option)
196 $option = $this->fixStepArgument($option);
197 $this->getSession()->getPage()->uncheckField($option);
201 * Attaches file to field with specified id|name|label|value
202 * Example: When I attach "bwayne_profile.png" to "profileImageUpload"
203 * Example: And I attach "bwayne_profile.png" to "profileImageUpload"
205 * @When /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)"$/
207 public function attachFileToField($field, $path)
209 $field = $this->fixStepArgument($field);
211 if ($this->getMinkParameter('files_path')) {
212 $fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
213 if (is_file($fullPath)) {
218 $this->getSession()->getPage()->attachFileToField($field, $path);
222 * Checks, that current page PATH is equal to specified
223 * Example: Then I should be on "/"
224 * Example: And I should be on "/bats"
225 * Example: And I should be on "http://google.com"
227 * @Then /^(?:|I )should be on "(?P<page>[^"]+)"$/
229 public function assertPageAddress($page)
231 $this->assertSession()->addressEquals($this->locatePath($page));
235 * Checks, that current page is the homepage
236 * Example: Then I should be on the homepage
237 * Example: And I should be on the homepage
239 * @Then /^(?:|I )should be on (?:|the )homepage$/
241 public function assertHomepage()
243 $this->assertSession()->addressEquals($this->locatePath('/'));
247 * Checks, that current page PATH matches regular expression
248 * Example: Then the url should match "superman is dead"
249 * Example: Then the uri should match "log in"
250 * Example: And the url should match "log in"
252 * @Then /^the (?i)url(?-i) should match (?P<pattern>"(?:[^"]|\\")*")$/
254 public function assertUrlRegExp($pattern)
256 $this->assertSession()->addressMatches($this->fixStepArgument($pattern));
260 * Checks, that current page response status is equal to specified
261 * Example: Then the response status code should be 200
262 * Example: And the response status code should be 400
264 * @Then /^the response status code should be (?P<code>\d+)$/
266 public function assertResponseStatus($code)
268 $this->assertSession()->statusCodeEquals($code);
272 * Checks, that current page response status is not equal to specified
273 * Example: Then the response status code should not be 501
274 * Example: And the response status code should not be 404
276 * @Then /^the response status code should not be (?P<code>\d+)$/
278 public function assertResponseStatusIsNot($code)
280 $this->assertSession()->statusCodeNotEquals($code);
284 * Checks, that page contains specified text
285 * Example: Then I should see "Who is the Batman?"
286 * Example: And I should see "Who is the Batman?"
288 * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)"$/
290 public function assertPageContainsText($text)
292 $this->assertSession()->pageTextContains($this->fixStepArgument($text));
296 * Checks, that page doesn't contain specified text
297 * Example: Then I should not see "Batman is Bruce Wayne"
298 * Example: And I should not see "Batman is Bruce Wayne"
300 * @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)"$/
302 public function assertPageNotContainsText($text)
304 $this->assertSession()->pageTextNotContains($this->fixStepArgument($text));
308 * Checks, that page contains text matching specified pattern
309 * Example: Then I should see text matching "Batman, the vigilante"
310 * Example: And I should not see "Batman, the vigilante"
312 * @Then /^(?:|I )should see text matching (?P<pattern>"(?:[^"]|\\")*")$/
314 public function assertPageMatchesText($pattern)
316 $this->assertSession()->pageTextMatches($this->fixStepArgument($pattern));
320 * Checks, that page doesn't contain text matching specified pattern
321 * Example: Then I should see text matching "Bruce Wayne, the vigilante"
322 * Example: And I should not see "Bruce Wayne, the vigilante"
324 * @Then /^(?:|I )should not see text matching (?P<pattern>"(?:[^"]|\\")*")$/
326 public function assertPageNotMatchesText($pattern)
328 $this->assertSession()->pageTextNotMatches($this->fixStepArgument($pattern));
332 * Checks, that HTML response contains specified string
333 * Example: Then the response should contain "Batman is the hero Gotham deserves."
334 * Example: And the response should contain "Batman is the hero Gotham deserves."
336 * @Then /^the response should contain "(?P<text>(?:[^"]|\\")*)"$/
338 public function assertResponseContains($text)
340 $this->assertSession()->responseContains($this->fixStepArgument($text));
344 * Checks, that HTML response doesn't contain specified string
345 * Example: Then the response should not contain "Bruce Wayne is a billionaire, play-boy, vigilante."
346 * Example: And the response should not contain "Bruce Wayne is a billionaire, play-boy, vigilante."
348 * @Then /^the response should not contain "(?P<text>(?:[^"]|\\")*)"$/
350 public function assertResponseNotContains($text)
352 $this->assertSession()->responseNotContains($this->fixStepArgument($text));
356 * Checks, that element with specified CSS contains specified text
357 * Example: Then I should see "Batman" in the "heroes_list" element
358 * Example: And I should see "Batman" in the "heroes_list" element
360 * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
362 public function assertElementContainsText($element, $text)
364 $this->assertSession()->elementTextContains('css', $element, $this->fixStepArgument($text));
368 * Checks, that element with specified CSS doesn't contain specified text
369 * Example: Then I should not see "Bruce Wayne" in the "heroes_alter_egos" element
370 * Example: And I should not see "Bruce Wayne" in the "heroes_alter_egos" element
372 * @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
374 public function assertElementNotContainsText($element, $text)
376 $this->assertSession()->elementTextNotContains('css', $element, $this->fixStepArgument($text));
380 * Checks, that element with specified CSS contains specified HTML
381 * Example: Then the "body" element should contain "style=\"color:black;\""
382 * Example: And the "body" element should contain "style=\"color:black;\""
384 * @Then /^the "(?P<element>[^"]*)" element should contain "(?P<value>(?:[^"]|\\")*)"$/
386 public function assertElementContains($element, $value)
388 $this->assertSession()->elementContains('css', $element, $this->fixStepArgument($value));
392 * Checks, that element with specified CSS doesn't contain specified HTML
393 * Example: Then the "body" element should not contain "style=\"color:black;\""
394 * Example: And the "body" element should not contain "style=\"color:black;\""
396 * @Then /^the "(?P<element>[^"]*)" element should not contain "(?P<value>(?:[^"]|\\")*)"$/
398 public function assertElementNotContains($element, $value)
400 $this->assertSession()->elementNotContains('css', $element, $this->fixStepArgument($value));
404 * Checks, that element with specified CSS exists on page
405 * Example: Then I should see a "body" element
406 * Example: And I should see a "body" element
408 * @Then /^(?:|I )should see an? "(?P<element>[^"]*)" element$/
410 public function assertElementOnPage($element)
412 $this->assertSession()->elementExists('css', $element);
416 * Checks, that element with specified CSS doesn't exist on page
417 * Example: Then I should not see a "canvas" element
418 * Example: And I should not see a "canvas" element
420 * @Then /^(?:|I )should not see an? "(?P<element>[^"]*)" element$/
422 public function assertElementNotOnPage($element)
424 $this->assertSession()->elementNotExists('css', $element);
428 * Checks, that form field with specified id|name|label|value has specified value
429 * Example: Then the "username" field should contain "bwayne"
430 * Example: And the "username" field should contain "bwayne"
432 * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should contain "(?P<value>(?:[^"]|\\")*)"$/
434 public function assertFieldContains($field, $value)
436 $field = $this->fixStepArgument($field);
437 $value = $this->fixStepArgument($value);
438 $this->assertSession()->fieldValueEquals($field, $value);
442 * Checks, that form field with specified id|name|label|value doesn't have specified value
443 * Example: Then the "username" field should not contain "batman"
444 * Example: And the "username" field should not contain "batman"
446 * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should not contain "(?P<value>(?:[^"]|\\")*)"$/
448 public function assertFieldNotContains($field, $value)
450 $field = $this->fixStepArgument($field);
451 $value = $this->fixStepArgument($value);
452 $this->assertSession()->fieldValueNotEquals($field, $value);
456 * Checks, that (?P<num>\d+) CSS elements exist on the page
457 * Example: Then I should see 5 "div" elements
458 * Example: And I should see 5 "div" elements
460 * @Then /^(?:|I )should see (?P<num>\d+) "(?P<element>[^"]*)" elements?$/
462 public function assertNumElements($num, $element)
464 $this->assertSession()->elementsCount('css', $element, intval($num));
468 * Checks, that checkbox with specified in|name|label|value is checked
469 * Example: Then the "remember_me" checkbox should be checked
470 * Example: And the "remember_me" checkbox is checked
472 * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should be checked$/
473 * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" (?:is|should be) checked$/
475 public function assertCheckboxChecked($checkbox)
477 $this->assertSession()->checkboxChecked($this->fixStepArgument($checkbox));
481 * Checks, that checkbox with specified in|name|label|value is unchecked
482 * Example: Then the "newsletter" checkbox should be unchecked
483 * Example: Then the "newsletter" checkbox should not be checked
484 * Example: And the "newsletter" checkbox is unchecked
486 * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should not be checked$/
487 * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" should (?:be unchecked|not be checked)$/
488 * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" is (?:unchecked|not checked)$/
490 public function assertCheckboxNotChecked($checkbox)
492 $this->assertSession()->checkboxNotChecked($this->fixStepArgument($checkbox));
496 * Prints current URL to console.
497 * Example: Then print current URL
498 * Example: And print current URL
500 * @Then /^print current URL$/
502 public function printCurrentUrl()
504 echo $this->getSession()->getCurrentUrl();
508 * Prints last response to console
509 * Example: Then print current response
510 * Example: And print current response
512 * @Then /^print last response$/
514 public function printLastResponse()
517 $this->getSession()->getCurrentUrl()."\n\n".
518 $this->getSession()->getPage()->getContent()
523 * Opens last response content in browser
524 * Example: Then show last response
525 * Example: And show last response
527 * @Then /^show last response$/
529 public function showLastResponse()
531 if (null === $this->getMinkParameter('show_cmd')) {
532 throw new \RuntimeException('Set "show_cmd" parameter in behat.yml to be able to open page in browser (ex.: "show_cmd: firefox %s")');
535 $filename = rtrim($this->getMinkParameter('show_tmp_dir'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.uniqid().'.html';
536 file_put_contents($filename, $this->getSession()->getPage()->getContent());
537 system(sprintf($this->getMinkParameter('show_cmd'), escapeshellarg($filename)));
541 * Returns list of definition translation resources paths
545 public static function getTranslationResources()
547 return self::getMinkTranslationResources();
551 * Returns list of definition translation resources paths for this dictionary
555 public static function getMinkTranslationResources()
557 return glob(__DIR__.'/../../../../i18n/*.xliff');
561 * Returns fixed step argument (with \\" replaced back to ")
563 * @param string $argument
567 protected function fixStepArgument($argument)
569 return str_replace('\\"', '"', $argument);