forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomCrawlerAssertionsTrait.php
More file actions
181 lines (166 loc) · 5.73 KB
/
Copy pathDomCrawlerAssertionsTrait.php
File metadata and controls
181 lines (166 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextContains;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextSame;
trait DomCrawlerAssertionsTrait
{
/**
* Asserts that the checkbox with the given name is checked.
*
* ```php
* <?php
* $I->assertCheckboxChecked('agree_terms');
* ```
*/
public function assertCheckboxChecked(string $fieldName, string $message = ''): void
{
$this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"), $message);
}
/**
* Asserts that the checkbox with the given name is not checked.
*
* ```php
* <?php
* $I->assertCheckboxNotChecked('subscribe');
* ```
*/
public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
{
$this->assertThatCrawler(
new LogicalNot(
new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")
),
$message
);
}
/**
* Asserts that the value of the form input with the given name does not equal the expected value.
*
* ```php
* <?php
* $I->assertInputValueNotSame('username', 'admin');
* ```
*/
public function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void
{
$this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message);
$this->assertThatCrawler(
new LogicalNot(
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
),
$message
);
}
/**
* Asserts that the value of the form input with the given name equals the expected value.
*
* ```php
* <?php
* $I->assertInputValueSame('username', 'johndoe');
* ```
*/
public function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
{
$this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message);
$this->assertThatCrawler(
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue),
$message
);
}
/**
* Asserts that the `<title>` element contains the given title.
*
* ```php
* <?php
* $I->assertPageTitleContains('Welcome');
* ```
*/
public function assertPageTitleContains(string $expectedTitle, string $message = ''): void
{
$this->assertSelectorTextContains('title', $expectedTitle, $message);
}
/**
* Asserts that the `<title>` element equals the given title.
*
* ```php
* <?php
* $I->assertPageTitleSame('Home Page');
* ```
*/
public function assertPageTitleSame(string $expectedTitle, string $message = ''): void
{
$this->assertSelectorTextSame('title', $expectedTitle, $message);
}
/**
* Asserts that the given selector matches at least one element in the response.
*
* ```php
* <?php
* $I->assertSelectorExists('.main-content');
* ```
*/
public function assertSelectorExists(string $selector, string $message = ''): void
{
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
}
/**
* Asserts that the given selector does not match at least one element in the response.
*
* ```php
* <?php
* $I->assertSelectorNotExists('.error');
* ```
*/
public function assertSelectorNotExists(string $selector, string $message = ''): void
{
$this->assertThatCrawler(new LogicalNot(new CrawlerSelectorExists($selector)), $message);
}
/**
* Asserts that the first element matching the given selector contains the expected text.
*
* ```php
* <?php
* $I->assertSelectorTextContains('h1', 'Dashboard');
* ```
*/
public function assertSelectorTextContains(string $selector, string $text, string $message = ''): void
{
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
$this->assertThatCrawler(new CrawlerSelectorTextContains($selector, $text), $message);
}
/**
* Asserts that the first element matching the given selector does not contain the expected text.
*
* ```php
* <?php
* $I->assertSelectorTextNotContains('p', 'error');
* ```
*/
public function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
{
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
$this->assertThatCrawler(new LogicalNot(new CrawlerSelectorTextContains($selector, $text)), $message);
}
/**
* Asserts that the text of the first element matching the given selector equals the expected text.
*
* ```php
* <?php
* $I->assertSelectorTextSame('h1', 'Dashboard');
* ```
*/
public function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
{
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
$this->assertThatCrawler(new CrawlerSelectorTextSame($selector, $text), $message);
}
protected function assertThatCrawler(Constraint $constraint, string $message): void
{
$this->assertThat($this->getClient()->getCrawler(), $constraint, $message);
}
}