Completed
Pull Request — master (#260)
by
unknown
04:13
created

MinkContext::assertResponseContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Behat MinkExtension.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\MinkExtension\Context;
12
13
use Behat\Behat\Context\TranslatableContext;
14
use Behat\Gherkin\Node\TableNode;
15
16
/**
17
 * Mink context for Behat BDD tool.
18
 * Provides Mink integration and base step definitions.
19
 *
20
 * @author Konstantin Kudryashov <[email protected]>
21
 */
22
class MinkContext extends RawMinkContext implements TranslatableContext
23
{
24
25
    /**
26
     * Opens homepage or specified page
27
     * Example: Given I am on "http://batman.com"
28
     * Example: And I am on "/articles/isBatmanBruceWayne"
29
     * Example: When I go to "/articles/isBatmanBruceWayne"
30
     * Example: Given I am on homepage
31
     * Example: And I am on homepage
32
     * Example: When I go to homepage
33
     * Example: When I go to the homepage
34
     *
35
     * @Given /^(?:|I )am on "(?P<page>[^"]+)"$/
36
     * @When /^(?:|I )go to "(?P<page>[^"]+)"$/
37
     * @Given /^(?:|I )am on (?:|the )homepage$/
38
     * @When /^(?:|I )go to (?:|the )homepage$/
39
     */
40
    public function visit($page = "/")
41
    {
42
        $this->visitPath($page);
43
    }
44
45
    /**
46
     * Reloads current page
47
     * Example: When I reload the page
48
     * Example: And I reload the page
49
     *
50
     * @When /^(?:|I )reload the page$/
51
     */
52
    public function reload()
53
    {
54
        $this->getSession()->reload();
55
    }
56
57
    /**
58
     * Moves backward one page in history
59
     * Example: When I move backward one page
60
     *
61
     * @When /^(?:|I )move backward one page$/
62
     */
63
    public function back()
64
    {
65
        $this->getSession()->back();
66
    }
67
68
    /**
69
     * Moves forward one page in history
70
     * Example: And I move forward one page
71
     *
72
     * @When /^(?:|I )move forward one page$/
73
     */
74
    public function forward()
75
    {
76
        $this->getSession()->forward();
77
    }
78
79
    /**
80
     * Presses button with specified id|name|title|alt|value
81
     * Example: When I press "Log In"
82
     * Example: And I press "Log In"
83
     *
84
     * @When /^(?:|I )press "(?P<button>(?:[^"]|\\")*)"$/
85
     */
86
    public function pressButton($button)
87
    {
88
        $button = $this->fixStepArgument($button);
89
        $this->getSession()->getPage()->pressButton($button);
90
    }
91
92
    /**
93
     * Clicks link with specified id|title|alt|text
94
     * Example: When I follow "Log In"
95
     * Example: And I follow "Log In"
96
     *
97
     * @When /^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/
98
     */
99
    public function clickLink($link)
100
    {
101
        $link = $this->fixStepArgument($link);
102
        $this->getSession()->getPage()->clickLink($link);
103
    }
104
105
    /**
106
     * Fills in form field with specified id|name|label|value
107
     * Example: When I fill in "username" with: "bwayne"
108
     * Example: And I fill in "bwayne" for "username"
109
     *
110
     * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
111
     * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with:$/
112
     * @When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
113
     */
114
    public function fillField($field, $value)
115
    {
116
        $field = $this->fixStepArgument($field);
117
        $value = $this->fixStepArgument($value);
118
        $this->getSession()->getPage()->fillField($field, $value);
119
    }
120
121
    /**
122
     * Fills in form fields with provided table
123
     * Example: When I fill in the following"
124
     *              | username | bruceWayne |
125
     *              | password | iLoveBats123 |
126
     * Example: And I fill in the following"
127
     *              | username | bruceWayne |
128
     *              | password | iLoveBats123 |
129
     *
130
     * @When /^(?:|I )fill in the following:$/
131
     */
132
    public function fillFields(TableNode $fields)
133
    {
134
        foreach ($fields->getRowsHash() as $field => $value) {
135
            $this->fillField($field, $value);
136
        }
137
    }
138
139
    /**
140
     * Selects option in select field with specified id|name|label|value
141
     * Example: When I select "Bats" from "user_fears"
142
     * Example: And I select "Bats" from "user_fears"
143
     *
144
     * @When /^(?:|I )select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/
145
     */
146
    public function selectOption($select, $option)
147
    {
148
        $select = $this->fixStepArgument($select);
149
        $option = $this->fixStepArgument($option);
150
        $this->getSession()->getPage()->selectFieldOption($select, $option);
151
    }
152
153
    /**
154
     * Selects additional option in select field with specified id|name|label|value
155
     * Example: When I additionally select "Deceased" from "parents_alive_status"
156
     * Example: And I additionally select "Deceased" from "parents_alive_status"
157
     *
158
     * @When /^(?:|I )additionally select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/
159
     */
160
    public function additionallySelectOption($select, $option)
161
    {
162
        $select = $this->fixStepArgument($select);
163
        $option = $this->fixStepArgument($option);
164
        $this->getSession()->getPage()->selectFieldOption($select, $option, true);
165
    }
166
167
    /**
168
     * Checks checkbox with specified id|name|label|value
169
     * Example: When I check "Pearl Necklace"
170
     * Example: And I check "Pearl Necklace"
171
     *
172
     * @When /^(?:|I )check "(?P<option>(?:[^"]|\\")*)"$/
173
     */
174
    public function checkOption($option)
175
    {
176
        $option = $this->fixStepArgument($option);
177
        $this->getSession()->getPage()->checkField($option);
178
    }
179
180
    /**
181
     * Unchecks checkbox with specified id|name|label|value
182
     * Example: When I uncheck "Broadway Plays"
183
     * Example: And I uncheck "Broadway Plays"
184
     *
185
     * @When /^(?:|I )uncheck "(?P<option>(?:[^"]|\\")*)"$/
186
     */
187
    public function uncheckOption($option)
188
    {
189
        $option = $this->fixStepArgument($option);
190
        $this->getSession()->getPage()->uncheckField($option);
191
    }
192
193
    /**
194
     * Attaches file to field with specified id|name|label|value
195
     * Example: When I attach "bwayne_profile.png" to "profileImageUpload"
196
     * Example: And I attach "bwayne_profile.png" to "profileImageUpload"
197
     *
198
     * @When /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)"$/
199
     */
200
    public function attachFileToField($field, $path)
201
    {
202
        $field = $this->fixStepArgument($field);
203
204
        if ($this->getMinkParameter('files_path')) {
205
            $fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
206
            if (is_file($fullPath)) {
207
                $path = $fullPath;
208
            }
209
        }
210
211
        $this->getSession()->getPage()->attachFileToField($field, $path);
212
    }
213
214
    /**
215
     * Checks, that current page PATH is equal to specified
216
     * Example: Then I should be on "/"
217
     * Example: And I should be on "/bats"
218
     * Example: And I should be on "http://google.com"
219
     *
220
     * @Then /^(?:|I )should be on "(?P<page>[^"]+)"$/
221
     */
222
    public function assertPageAddress($page)
223
    {
224
        $this->assertSession()->addressEquals($this->locatePath($page));
225
    }
226
227
    /**
228
     * Checks, that current page is the homepage
229
     * Example: Then I should be on the homepage
230
     * Example: And I should be on the homepage
231
     *
232
     * @Then /^(?:|I )should be on (?:|the )homepage$/
233
     */
234
    public function assertHomepage()
235
    {
236
        $this->assertSession()->addressEquals($this->locatePath('/'));
237
    }
238
239
    /**
240
     * Checks, that current page PATH matches regular expression
241
     * Example: Then the url should match "superman is dead"
242
     * Example: Then the uri should match "log in"
243
     * Example: And the url should match "log in"
244
     *
245
     * @Then /^the (?i)url(?-i) should match (?P<pattern>"(?:[^"]|\\")*")$/
246
     */
247
    public function assertUrlRegExp($pattern)
248
    {
249
        $this->assertSession()->addressMatches($this->fixStepArgument($pattern));
250
    }
251
252
    /**
253
     * Checks, that current page response status is equal to specified
254
     * Example: Then the response status code should be 200
255
     * Example: And the response status code should be 400
256
     *
257
     * @Then /^the response status code should be (?P<code>\d+)$/
258
     */
259
    public function assertResponseStatus($code)
260
    {
261
        $this->assertSession()->statusCodeEquals($code);
262
    }
263
264
    /**
265
     * Checks, that current page response status is not equal to specified
266
     * Example: Then the response status code should not be 501
267
     * Example: And the response status code should not be 404
268
     *
269
     * @Then /^the response status code should not be (?P<code>\d+)$/
270
     */
271
    public function assertResponseStatusIsNot($code)
272
    {
273
        $this->assertSession()->statusCodeNotEquals($code);
274
    }
275
276
    /**
277
     * Checks, that page contains specified text
278
     * Example: Then I should see "Who is the Batman?"
279
     * Example: And I should see "Who is the Batman?"
280
     *
281
     * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)"$/
282
     */
283
    public function assertPageContainsText($text)
284
    {
285
        $this->assertSession()->pageTextContains($this->fixStepArgument($text));
286
    }
287
288
    /**
289
     * Checks, that page doesn't contain specified text
290
     * Example: Then I should not see "Batman is Bruce Wayne"
291
     * Example: And I should not see "Batman is Bruce Wayne"
292
     *
293
     * @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)"$/
294
     */
295
    public function assertPageNotContainsText($text)
296
    {
297
        $this->assertSession()->pageTextNotContains($this->fixStepArgument($text));
298
    }
299
300
    /**
301
     * Checks, that page contains text matching specified pattern
302
     * Example: Then I should see text matching "Batman, the vigilante"
303
     * Example: And I should not see "Batman, the vigilante"
304
     *
305
     * @Then /^(?:|I )should see text matching (?P<pattern>"(?:[^"]|\\")*")$/
306
     */
307
    public function assertPageMatchesText($pattern)
308
    {
309
        $this->assertSession()->pageTextMatches($this->fixStepArgument($pattern));
310
    }
311
312
    /**
313
     * Checks, that page doesn't contain text matching specified pattern
314
     * Example: Then I should see text matching "Bruce Wayne, the vigilante"
315
     * Example: And I should not see "Bruce Wayne, the vigilante"
316
     *
317
     * @Then /^(?:|I )should not see text matching (?P<pattern>"(?:[^"]|\\")*")$/
318
     */
319
    public function assertPageNotMatchesText($pattern)
320
    {
321
        $this->assertSession()->pageTextNotMatches($this->fixStepArgument($pattern));
322
    }
323
324
    /**
325
     * Checks, that HTML response contains specified string
326
     * Example: Then the response should contain "Batman is the hero Gotham deserves."
327
     * Example: And the response should contain "Batman is the hero Gotham deserves."
328
     *
329
     * @Then /^the response should contain "(?P<text>(?:[^"]|\\")*)"$/
330
     */
331
    public function assertResponseContains($text)
332
    {
333
        $this->assertSession()->responseContains($this->fixStepArgument($text));
334
    }
335
336
    /**
337
     * Checks, that HTML response doesn't contain specified string
338
     * Example: Then the response should not contain "Bruce Wayne is a billionaire, play-boy, vigilante."
339
     * Example: And the response should not contain "Bruce Wayne is a billionaire, play-boy, vigilante."
340
     *
341
     * @Then /^the response should not contain "(?P<text>(?:[^"]|\\")*)"$/
342
     */
343
    public function assertResponseNotContains($text)
344
    {
345
        $this->assertSession()->responseNotContains($this->fixStepArgument($text));
346
    }
347
348
    /**
349
     * Checks, that element with specified CSS contains specified text
350
     * Example: Then I should see "Batman" in the "heroes_list" element
351
     * Example: And I should see "Batman" in the "heroes_list" element
352
     *
353
     * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
354
     */
355
    public function assertElementContainsText($element, $text)
356
    {
357
        $this->assertSession()->elementTextContains('css', $element, $this->fixStepArgument($text));
358
    }
359
360
    /**
361
     * Checks, that element with specified CSS doesn't contain specified text
362
     * Example: Then I should not see "Bruce Wayne" in the "heroes_alter_egos" element
363
     * Example: And I should not see "Bruce Wayne" in the "heroes_alter_egos" element
364
     *
365
     * @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
366
     */
367
    public function assertElementNotContainsText($element, $text)
368
    {
369
        $this->assertSession()->elementTextNotContains('css', $element, $this->fixStepArgument($text));
370
    }
371
372
    /**
373
     * Checks, that element with specified CSS contains specified HTML
374
     * Example: Then the "body" element should contain "style=\"color:black;\""
375
     * Example: And the "body" element should contain "style=\"color:black;\""
376
     *
377
     * @Then /^the "(?P<element>[^"]*)" element should contain "(?P<value>(?:[^"]|\\")*)"$/
378
     */
379
    public function assertElementContains($element, $value)
380
    {
381
        $this->assertSession()->elementContains('css', $element, $this->fixStepArgument($value));
382
    }
383
384
    /**
385
     * Checks, that element with specified CSS doesn't contain specified HTML
386
     * Example: Then the "body" element should not contain "style=\"color:black;\""
387
     * Example: And the "body" element should not contain "style=\"color:black;\""
388
     *
389
     * @Then /^the "(?P<element>[^"]*)" element should not contain "(?P<value>(?:[^"]|\\")*)"$/
390
     */
391
    public function assertElementNotContains($element, $value)
392
    {
393
        $this->assertSession()->elementNotContains('css', $element, $this->fixStepArgument($value));
394
    }
395
396
    /**
397
     * Checks, that element with specified CSS exists on page
398
     * Example: Then I should see a "body" element
399
     * Example: And I should see a "body" element
400
     *
401
     * @Then /^(?:|I )should see an? "(?P<element>[^"]*)" element$/
402
     */
403
    public function assertElementOnPage($element)
404
    {
405
        $this->assertSession()->elementExists('css', $element);
406
    }
407
408
    /**
409
     * Checks, that element with specified CSS doesn't exist on page
410
     * Example: Then I should not see a "canvas" element
411
     * Example: And I should not see a "canvas" element
412
     *
413
     * @Then /^(?:|I )should not see an? "(?P<element>[^"]*)" element$/
414
     */
415
    public function assertElementNotOnPage($element)
416
    {
417
        $this->assertSession()->elementNotExists('css', $element);
418
    }
419
420
    /**
421
     * Checks, that form field with specified id|name|label|value has specified value
422
     * Example: Then the "username" field should contain "bwayne"
423
     * Example: And the "username" field should contain "bwayne"
424
     *
425
     * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should contain "(?P<value>(?:[^"]|\\")*)"$/
426
     */
427
    public function assertFieldContains($field, $value)
428
    {
429
        $field = $this->fixStepArgument($field);
430
        $value = $this->fixStepArgument($value);
431
        $this->assertSession()->fieldValueEquals($field, $value);
432
    }
433
434
    /**
435
     * Checks, that form field with specified id|name|label|value doesn't have specified value
436
     * Example: Then the "username" field should not contain "batman"
437
     * Example: And the "username" field should not contain "batman"
438
     *
439
     * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should not contain "(?P<value>(?:[^"]|\\")*)"$/
440
     */
441
    public function assertFieldNotContains($field, $value)
442
    {
443
        $field = $this->fixStepArgument($field);
444
        $value = $this->fixStepArgument($value);
445
        $this->assertSession()->fieldValueNotEquals($field, $value);
446
    }
447
448
    /**
449
     * Checks, that (?P<num>\d+) CSS elements exist on the page
450
     * Example: Then I should see 5 "div" elements
451
     * Example: And I should see 5 "div" elements
452
     *
453
     * @Then /^(?:|I )should see (?P<num>\d+) "(?P<element>[^"]*)" elements?$/
454
     */
455
    public function assertNumElements($num, $element)
456
    {
457
        $this->assertSession()->elementsCount('css', $element, intval($num));
458
    }
459
460
    /**
461
     * Checks, that checkbox with specified id|name|label|value is checked
462
     * Example: Then the "remember_me" checkbox should be checked
463
     * Example: And the "remember_me" checkbox is checked
464
     *
465
     * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should be checked$/
466
     * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox is checked$/
467
     * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" (?:is|should be) checked$/
468
     */
469
    public function assertCheckboxChecked($checkbox)
470
    {
471
        $this->assertSession()->checkboxChecked($this->fixStepArgument($checkbox));
472
    }
473
474
    /**
475
     * Checks, that checkbox with specified id|name|label|value is unchecked
476
     * Example: Then the "newsletter" checkbox should be unchecked
477
     * Example: Then the "newsletter" checkbox should not be checked
478
     * Example: And the "newsletter" checkbox is unchecked
479
     *
480
     * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should (?:be unchecked|not be checked)$/
481
     * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox is (?:unchecked|not checked)$/
482
     * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" should (?:be unchecked|not be checked)$/
483
     * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" is (?:unchecked|not checked)$/
484
     */
485
    public function assertCheckboxNotChecked($checkbox)
486
    {
487
        $this->assertSession()->checkboxNotChecked($this->fixStepArgument($checkbox));
488
    }
489
490
    /**
491
     * Prints current URL to console.
492
     * Example: Then print current URL
493
     * Example: And print current URL
494
     *
495
     * @Then /^print current URL$/
496
     */
497
    public function printCurrentUrl()
498
    {
499
        echo $this->getSession()->getCurrentUrl();
500
    }
501
502
    /**
503
     * Prints last response to console
504
     * Example: Then print current response
505
     * Example: And print current response
506
     *
507
     * @Then /^print last response$/
508
     */
509
    public function printLastResponse()
510
    {
511
        echo (
512
            $this->getSession()->getCurrentUrl()."\n\n".
513
            $this->getSession()->getPage()->getContent()
514
        );
515
    }
516
517
    /**
518
     * Opens last response content in browser
519
     * Example: Then show last response
520
     * Example: And show last response
521
     *
522
     * @Then /^show last response$/
523
     */
524
    public function showLastResponse()
525
    {
526
        if (null === $this->getMinkParameter('show_cmd')) {
527
            throw new \RuntimeException('Set "show_cmd" parameter in behat.yml to be able to open page in browser (ex.: "show_cmd: firefox %s")');
528
        }
529
530
        $filename = rtrim($this->getMinkParameter('show_tmp_dir'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.uniqid().'.html';
531
        file_put_contents($filename, $this->getSession()->getPage()->getContent());
532
        system(sprintf($this->getMinkParameter('show_cmd'), escapeshellarg($filename)));
533
    }
534
535
    /**
536
     * Returns list of definition translation resources paths
537
     *
538
     * @return array
539
     */
540
    public static function getTranslationResources()
541
    {
542
        return self::getMinkTranslationResources();
543
    }
544
545
    /**
546
     * Returns list of definition translation resources paths for this dictionary
547
     *
548
     * @return array
549
     */
550
    public static function getMinkTranslationResources()
551
    {
552
        return glob(__DIR__.'/../../../../i18n/*.xliff');
553
    }
554
555
    /**
556
     * Returns fixed step argument (with \\" replaced back to ")
557
     *
558
     * @param string $argument
559
     *
560
     * @return string
561
     */
562
    protected function fixStepArgument($argument)
563
    {
564
        return str_replace('\\"', '"', $argument);
565
    }
566
}
567