Completed
Pull Request — master (#178)
by Gordon
03:59
created

BrowserContext::iSetBasicAuthenticationWithAnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Sanpi\Behatch\Context;
4
5
use Behat\Gherkin\Node\TableNode;
6
use Behat\Mink\Exception\ExpectationException;
7
use Behat\Mink\Exception\ResponseTextException;
8
use Behat\Mink\Exception\ElementNotFoundException;
9
10
class BrowserContext extends BaseContext
11
{
12
    private $timeout;
13
    private $dateFormat = 'dmYHi';
14
15
    public function __construct($timeout = 1)
16
    {
17
        $this->timeout = $timeout;
18
    }
19
20
    /**
21
     * @AfterScenario
22
     */
23
    public function closeBrowser()
24
    {
25
        $this->getSession()->stop();
26
    }
27
28
    /**
29
     * Set login / password for next HTTP authentication
30
     *
31
     * @When I set basic authentication with :user and :password
32
     */
33
    public function iSetBasicAuthenticationWithAnd($user, $password)
34
    {
35
        $this->getSession()->setBasicAuth($user, $password);
36
    }
37
38
    /**
39
     * Open url with various parameters
40
     *
41
     * @Given (I )am on url composed by:
42
     */
43
    public function iAmOnUrlComposedBy(TableNode $tableNode)
44
    {
45
        $url = '';
46
        foreach ($tableNode->getHash() as $hash) {
47
            $url .= $hash['parameters'];
48
        }
49
50
        return $this->getMinkContext()
51
            ->visit($url);
52
    }
53
54
    /**
55
     * Clicks on the nth CSS element
56
     *
57
     * @When (I )click on the :index :element element
58
     */
59
    public function iClickOnTheNthElement($index, $element)
60
    {
61
        $node = $this->findElement('css', $element, $index);
62
        $node->click();
63
    }
64
65
    /**
66
     * Click on the nth specified link
67
     *
68
     * @When (I )follow the :index :link link
69
     */
70
    public function iFollowTheNthLink($index, $link)
71
    {
72
        $element = ['link', $this->getSession()->getSelectorsHandler()->xpathLiteral($link)];
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Mink\Selector\Sele...Handler::xpathLiteral() has been deprecated with message: since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath or pass the unescaped value when using the named selector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
73
        $node = $this->findElement('named', $element, $index);
74
        $node->click();
75
    }
76
77
    /**
78
     * Presses the nth specified button
79
     *
80
     * @When (I )press the :index :button button
81
     */
82
    public function pressTheNthButton($index, $button)
83
    {
84
        $element = ['button', $this->getSession()->getSelectorsHandler()->xpathLiteral($button)];
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Mink\Selector\Sele...Handler::xpathLiteral() has been deprecated with message: since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath or pass the unescaped value when using the named selector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
85
        $node = $this->findElement('named', $element, $index);
86
        $node->click();
87
    }
88
89
    /**
90
     * Fills in form field with current date
91
     *
92
     * @When (I )fill in :field with the current date
93
     */
94
    public function iFillInWithTheCurrentDate($field)
95
    {
96
        return $this->iFillInWithTheCurrentDateAndModifier($field, 'now');
97
    }
98
99
    /**
100
     * Fills in form field with current date and strtotime modifier
101
     *
102
     * @When (I )fill in :field with the current date and modifier :modifier
103
     */
104
    public function iFillInWithTheCurrentDateAndModifier($field, $modifier)
105
    {
106
        return $this->getMinkContext()
107
            ->fillField($field, date($this->dateFormat, strtotime($modifier)));
108
    }
109
110
    /**
111
     * Mouse over a CSS element
112
     *
113
     * @When (I )hover :element
114
     */
115 View Code Duplication
    public function iHoverIShouldSeeIn($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $node = $this->getSession()->getPage()->find('css', $element);
118
        if ($node === null) {
119
            throw new \Exception("The hovered element '$element' was not found anywhere in the page");
120
        }
121
        $node->mouseOver();
122
    }
123
124
    /**
125
     * Save value of the field in parameters array
126
     *
127
     * @When (I )save the value of :field in the :parameter parameter
128
     */
129
    public function iSaveTheValueOfInTheParameter($field, $parameter)
130
    {
131
        $field = str_replace('\\"', '"', $field);
132
        $node  = $this->getSession()->getPage()->findField($field);
133
        if ($node === null) {
134
            throw new \Exception("The field '$field' was not found anywhere in the page");
135
        }
136
137
        $this->setMinkParameter($parameter, $node->getValue());
138
    }
139
140
    /**
141
     * Checks, that the page should contains specified text after given timeout
142
     *
143
     * @Then (I )wait :count second(s) until I see :text
144
     */
145
    public function iWaitSecondsUntilISee($count, $text)
146
    {
147
        $this->iWaitSecondsUntilISeeInTheElement($count, $text, 'html');
148
    }
149
150
    /**
151
     * Checks, that the page should contains specified text after timeout
152
     *
153
     * @Then (I )wait until I see :text
154
     */
155
    public function iWaitUntilISee($text)
156
    {
157
        $this->iWaitSecondsUntilISee($this->timeout, $text);
158
    }
159
160
    /**
161
     * Checks, that the element contains specified text after timeout
162
     *
163
     * @Then (I )wait :count second(s) until I see :text in the :element element
164
     */
165
    public function iWaitSecondsUntilISeeInTheElement($count, $text, $element)
166
    {
167
        $this->iWaitSecondsForElement($count, $element);
168
169
        $expected = str_replace('\\"', '"', $text);
170
        $node = $this->getSession()->getPage()->find('css', $element);
171
        $message = "The text '$expected' was not found after a $count seconds timeout";
172
173
        $this->assertContains($expected, $node->getText(), $message);
174
    }
175
176
    /**
177
     * @Then (I )wait :count second(s)
178
     */
179
    public function iWaitSeconds($count)
180
    {
181
        sleep($count);
182
    }
183
184
    /**
185
     * Checks, that the element contains specified text after timeout
186
     *
187
     * @Then (I )wait until I see :text in the :element element
188
     */
189
    public function iWaitUntilISeeInTheElement($text, $element)
190
    {
191
        $this->iWaitSecondsUntilISeeInTheElement($this->timeout, $text, $element);
192
    }
193
194
    /**
195
     * Checks, that the page should contains specified element after timeout
196
     *
197
     * @Then (I )wait for :element element
198
     */
199
    public function iWaitForElement($element)
200
    {
201
        $this->iWaitSecondsForElement($this->timeout, $element);
202
    }
203
204
    /**
205
     * Wait for a element
206
     *
207
     * @Then (I )wait :count second(s) for :element element
208
     */
209
    public function iWaitSecondsForElement($count, $element)
210
    {
211
        $found = false;
212
        $startTime = time();
213
214
        do {
215
            try {
216
                $node = $this->getSession()->getPage()->findAll('css', $element);
217
                $this->assertCount(1, $node);
218
                $found = true;
219
            }
220
            catch (ExpectationException $e) {
221
                /* Intentionnaly leave blank */
222
            }
223
        }
224
        while (time() - $startTime < $count);
225
226
        if ($found === false) {
227
            $message = "The element '$element' was not found after a $count seconds timeout";
228
            throw new ResponseTextException($message, $this->getSession(), $e);
229
        }
230
    }
231
232
    /**
233
     * @Then /^(?:|I )should see (?P<count>\d+) "(?P<element>[^"]*)" in the (?P<index>\d+)(?:st|nd|rd|th) "(?P<parent>[^"]*)"$/
234
     */
235 View Code Duplication
    public function iShouldSeeNElementInTheNthParent($count, $element, $index, $parent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
    {
237
        $actual = $this->countElements($element, $index, $parent);
238
        if ($actual !== $count) {
239
            throw new \Exception("$actual occurrences of the '$element' element in '$parent' found");
240
        }
241
    }
242
243
    /**
244
     * @Then (I )should see less than :count :element in the :index :parent
245
     */
246 View Code Duplication
    public function iShouldSeeLessThanNElementInTheNthParent($count, $element, $index, $parent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
247
    {
248
        $actual = $this->countElements($element, $index, $parent);
249
        if ($actual > $count) {
250
            throw new \Exception("$actual occurrences of the '$element' element in '$parent' found");
251
        }
252
    }
253
254
    /**
255
     * @Then (I )should see more than :count :element in the :index :parent
256
     */
257 View Code Duplication
    public function iShouldSeeMoreThanNElementInTheNthParent($count, $element, $index, $parent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
    {
259
        $actual = $this->countElements($element, $index, $parent);
260
        if ($actual < $count) {
261
            throw new \Exception("$actual occurrences of the '$element' element in '$parent' found");
262
        }
263
    }
264
265
    /**
266
     * Checks, that element with given CSS is enabled
267
     *
268
     * @Then the element :element should be enabled
269
     */
270 View Code Duplication
    public function theElementShouldBeEnabled($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
    {
272
        $node = $this->getSession()->getPage()->find('css', $element);
273
        if ($node === null) {
274
            throw new \Exception("There is no '$element' element");
275
        }
276
277
        if ($node->hasAttribute('disabled')) {
278
            throw new \Exception("The element '$element' is not enabled");
279
        }
280
    }
281
282
    /**
283
     * Checks, that element with given CSS is disabled
284
     *
285
     * @Then the element :element should be disabled
286
     */
287
    public function theElementShouldBeDisabled($element)
288
    {
289
        $this->not(function () use($element) {
290
            $this->theElementShouldBeEnabled($element);
291
        }, "The element '$element' is not disabled");
292
    }
293
294
    /**
295
     * Checks, that given select box contains the specified option
296
     *
297
     * @Then the :select select box should contain :option
298
     */
299
    public function theSelectBoxShouldContain($select, $option)
300
    {
301
        $select = str_replace('\\"', '"', $select);
302
        $option = str_replace('\\"', '"', $option);
303
304
        $obj = $this->getSession()->getPage()->findField($select);
305
        if ($obj === null) {
306
            throw new ElementNotFoundException(
307
                $this->getSession(), 'select box', 'id|name|label|value', $select
308
            );
309
        }
310
        $optionText = $obj->getText();
311
312
        $message = "The '$select' select box does not contain the '$option' option";
313
        $this->assertContains($option, $optionText, $message);
314
    }
315
316
    /**
317
     * Checks, that given select box does not contain the specified option
318
     *
319
     * @Then the :select select box should not contain :option
320
     */
321
    public function theSelectBoxShouldNotContain($select, $option)
322
    {
323
        $this->not(function () use($select, $option) {
324
            $this->theSelectBoxShouldContain($select, $option);
325
        }, "The '$select' select box does contain the '$option' option");
326
    }
327
328
    /**
329
     * Checks, that the specified CSS element is visible
330
     *
331
     * @Then the :element element should be visible
332
     */
333
    public function theElementShouldBeVisible($element)
334
    {
335
        $displayedNode = $this->getSession()->getPage()->find('css', $element);
336
        if ($displayedNode === null) {
337
            throw new \Exception("The element '$element' was not found anywhere in the page");
338
        }
339
340
341
        $message = "The element '$element' is not visible";
342
        $this->assertTrue($displayedNode->isVisible(), $message);
343
    }
344
345
    /**
346
     * Checks, that the specified CSS element is not visible
347
     *
348
     * @Then the :element element should not be visible
349
     */
350
    public function theElementShouldNotBeVisible($element)
351
    {
352
        $exception = new \Exception("The element '$element' is visible");
353
354
        $this->not(function () use($element) {
355
            $this->theElementShouldBeVisible($element);
356
        }, $exception);
357
    }
358
359
    /**
360
     * Select a frame by its name or ID.
361
     *
362
     * @When (I )switch to iframe :name
363
     * @When (I )switch to frame :name
364
     */
365
    public function switchToIFrame($name)
366
    {
367
        $this->getSession()->switchToIFrame($name);
368
    }
369
370
    /**
371
     * Go back to main document frame.
372
     *
373
     * @When (I )switch to main frame
374
     */
375
    public function switchToMainFrame()
376
    {
377
        $this->getSession()->switchToIFrame();
378
    }
379
380
    /**
381
     * Press keyboard key.
382
     *
383
     * @When (I )press key :char
384
     * @When (I )press key :char with :modifier
385
     * @When (I )press key :char on :xpath
386
     * @When (I )press key :char with :modifier on :xpath
387
     */
388
    public function pressKey($char, $modifier = null, $xpath = '')
389
    {
390
        $this->getSession()->keyPress($xpath, $char, $modifier);
0 ignored issues
show
Bug introduced by
The method keyPress() does not seem to exist on object<Behat\Mink\Session>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
391
    }
392
}
393