1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Tests\Features\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Element\Element; |
6
|
|
|
use Behat\Mink\Exception\ExpectationException; |
7
|
|
|
use Knp\FriendlyContexts\Context\MinkContext as KFMinkContext; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Feature context. |
11
|
|
|
*/ |
12
|
|
|
class MinkContext extends KFMinkContext |
13
|
|
|
{ |
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* Checks, that page contains specified text |
16
|
|
|
* Example: Then I should see "Who is the Batman?" |
17
|
|
|
* Example: And I should see "Who is the Batman?". |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function assertPageContainsText($text, $timeout = 10000) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$element = $this->findOrRetry($this->getSession()->getPage(), $text, $timeout); |
22
|
|
|
if (!$element) { |
23
|
|
|
$message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text); |
24
|
|
|
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* Checks, that page doesn't contain specified text |
30
|
|
|
* Example: Then I should not see "Batman is Bruce Wayne" |
31
|
|
|
* Example: And I should not see "Batman is Bruce Wayne". |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function assertPageNotContainsText($text, $timeout = 10000) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$element = $this->findOrRetry($this->getSession()->getPage(), $text, $timeout); |
36
|
|
|
if ($element && $element->isVisible()) { |
37
|
|
|
$message = sprintf('The text "%s" was found in the text of the current page although it should not.', $text); |
38
|
|
|
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* Checks, that element with specified CSS contains specified text |
44
|
|
|
* Example: Then I should see "Batman" in the "heroes_list" element |
45
|
|
|
* Example: And I should see "Batman" in the "heroes_list" element. |
46
|
|
|
*/ |
|
|
|
|
47
|
|
|
public function assertElementContainsText($element, $text, $timeout = 10000) |
48
|
|
|
{ |
49
|
|
|
if ($timeout <= 0) { |
50
|
|
|
$message = sprintf('The element "%s" was not found in the page.', $element); |
51
|
|
|
throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession()); |
52
|
|
|
} |
53
|
|
|
$selectorType = 'css'; |
54
|
|
|
|
55
|
|
|
$node = $this->getSession()->getPage()->find($selectorType, $element); |
56
|
|
|
|
57
|
|
|
if (is_object($node)) { |
58
|
|
|
$item = $this->findOrRetry($node, $text); |
59
|
|
|
if (!$item) { |
60
|
|
|
$this->assertElementContainsText($element, $text, 0); |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
$this->getSession()->wait(100); |
64
|
|
|
|
65
|
|
|
return $this->assertElementContainsText($element, $text, $timeout - 100); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Element $page |
71
|
|
|
* @param float|int $timeout |
72
|
|
|
*/ |
|
|
|
|
73
|
|
|
public function assertPageAddress($page, $timeout = 10000) |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$this->assertSession()->addressEquals($this->locatePath($page)); |
|
|
|
|
77
|
|
|
} catch (ExpectationException $e) { |
78
|
|
|
if ($timeout >= 0) { |
79
|
|
|
$this->getSession()->wait(100); |
80
|
|
|
|
81
|
|
|
return $this->assertPageAddress($page, $timeout - 100); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Try to find value in element and retry for a given time. |
88
|
|
|
* |
89
|
|
|
* @param Element $element |
90
|
|
|
* @param string $value |
91
|
|
|
* @param int $timeout |
92
|
|
|
*/ |
93
|
|
|
protected function findOrRetry(Element $element, $value, $timeout = 10000) |
94
|
|
|
{ |
95
|
|
|
if ($timeout <= 0) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Sensitive case search |
100
|
|
|
/** @see http://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more/3655588#3655588 */ |
101
|
|
|
$item = $element->find('xpath', '//*[text()[contains(., "'.$value.'")]]'); |
102
|
|
|
|
103
|
|
View Code Duplication |
if ($item) { |
|
|
|
|
104
|
|
|
return $item; |
105
|
|
|
} else { |
106
|
|
|
$this->getSession()->wait(100); |
107
|
|
|
|
108
|
|
|
return $this->findOrRetry($element, $value, $timeout - 100); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|