|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Browser\Tests\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @author Kevin Bond <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
trait HtmlTests |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @test |
|
14
|
|
|
*/ |
|
15
|
|
|
public function html_assertions(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$this->browser() |
|
|
|
|
|
|
18
|
|
|
->visit('/page1') |
|
19
|
|
|
->assertSee('h1 title') |
|
20
|
|
|
->assertNotSee('invalid text') |
|
21
|
|
|
->assertSeeIn('h1', 'title') |
|
22
|
|
|
->assertNotSeeIn('h1', 'invalid text') |
|
23
|
|
|
->assertSeeElement('h1') |
|
24
|
|
|
->assertNotSeeElement('h2') |
|
25
|
|
|
->assertElementCount('ul li', 2) |
|
26
|
|
|
; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
*/ |
|
32
|
|
|
public function html_head_assertions(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->browser() |
|
35
|
|
|
->visit('/page1') |
|
36
|
|
|
->assertSeeIn('title', 'meta title') |
|
37
|
|
|
->assertElementAttributeContains('meta[name="description"]', 'content', 'meta') |
|
38
|
|
|
->assertElementAttributeNotContains('meta[name="description"]', 'content', 'invalid') |
|
39
|
|
|
; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @test |
|
44
|
|
|
*/ |
|
45
|
|
|
public function form_assertions(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->browser() |
|
48
|
|
|
->visit('/page1') |
|
49
|
|
|
->assertFieldEquals('Input 1', 'input 1') |
|
50
|
|
|
->assertFieldEquals('input1', 'input 1') |
|
51
|
|
|
->assertFieldEquals('input_1', 'input 1') |
|
52
|
|
|
->assertFieldNotEquals('Input 1', 'invalid') |
|
53
|
|
|
->assertFieldNotEquals('input1', 'invalid') |
|
54
|
|
|
->assertFieldNotEquals('input_1', 'invalid') |
|
55
|
|
|
->assertChecked('Input 3') |
|
56
|
|
|
->assertChecked('input3') |
|
57
|
|
|
->assertChecked('input_3') |
|
58
|
|
|
->assertNotChecked('Input 2') |
|
59
|
|
|
->assertNotChecked('input2') |
|
60
|
|
|
->assertNotChecked('input_2') |
|
61
|
|
|
->assertSelected('Input 4', 'option 1') |
|
62
|
|
|
->assertSelected('input4', 'option 1') |
|
63
|
|
|
->assertSelected('input_4', 'option 1') |
|
64
|
|
|
->assertSelected('Input 7', 'option 1') |
|
65
|
|
|
->assertSelected('input7', 'option 1') |
|
66
|
|
|
->assertSelected('input_7[]', 'option 1') |
|
67
|
|
|
->assertSelected('Input 7', 'option 3') |
|
68
|
|
|
->assertSelected('input7', 'option 3') |
|
69
|
|
|
->assertSelected('input_7[]', 'option 3') |
|
70
|
|
|
->assertNotSelected('Input 4', 'option 2') |
|
71
|
|
|
->assertNotSelected('input4', 'option 2') |
|
72
|
|
|
->assertNotSelected('input_4', 'option 2') |
|
73
|
|
|
->assertNotSelected('Input 7', 'option 2') |
|
74
|
|
|
->assertNotSelected('input7', 'option 2') |
|
75
|
|
|
->assertNotSelected('input_7[]', 'option 2') |
|
76
|
|
|
; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
*/ |
|
82
|
|
|
public function link_action(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->browser() |
|
85
|
|
|
->visit('/page1') |
|
86
|
|
|
->follow('a link') |
|
87
|
|
|
->assertOn('/page2') |
|
88
|
|
|
->visit('/page1') |
|
89
|
|
|
->click('a link') |
|
90
|
|
|
->assertOn('/page2') |
|
91
|
|
|
; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
*/ |
|
97
|
|
|
public function form_actions_by_field_label(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->browser() |
|
100
|
|
|
->visit('/page1') |
|
101
|
|
|
->fillField('Input 1', 'Kevin') |
|
102
|
|
|
->checkField('Input 2') |
|
103
|
|
|
->uncheckField('Input 3') |
|
104
|
|
|
->selectFieldOption('Input 4', 'option 2') |
|
105
|
|
|
->attachFile('Input 5', __FILE__) |
|
106
|
|
|
->selectFieldOptions('Input 6', ['option 1', 'option 3']) |
|
107
|
|
|
->click('Submit') |
|
108
|
|
|
->assertOn('/submit-form') |
|
109
|
|
|
->assertContains('"input_1":"Kevin"') |
|
110
|
|
|
->assertContains('"input_2":"on"') |
|
111
|
|
|
->assertNotContains('"input_3') |
|
112
|
|
|
->assertContains('"input_4":"option 2"') |
|
113
|
|
|
->assertContains(\sprintf('"input_5":"%s"', \pathinfo(__FILE__, PATHINFO_BASENAME))) |
|
114
|
|
|
->assertContains('"input_6":["option 1","option 3"]') |
|
115
|
|
|
; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @test |
|
120
|
|
|
*/ |
|
121
|
|
|
public function form_actions_by_field_id(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$this->browser() |
|
124
|
|
|
->visit('/page1') |
|
125
|
|
|
->fillField('input1', 'Kevin') |
|
126
|
|
|
->checkField('input2') |
|
127
|
|
|
->uncheckField('input3') |
|
128
|
|
|
->selectFieldOption('input4', 'option 2') |
|
129
|
|
|
->attachFile('input5', __FILE__) |
|
130
|
|
|
->selectFieldOptions('input6', ['option 1', 'option 3']) |
|
131
|
|
|
->click('Submit') |
|
132
|
|
|
->assertOn('/submit-form') |
|
133
|
|
|
->assertContains('"input_1":"Kevin"') |
|
134
|
|
|
->assertContains('"input_2":"on"') |
|
135
|
|
|
->assertNotContains('"input_3') |
|
136
|
|
|
->assertContains('"input_4":"option 2"') |
|
137
|
|
|
->assertContains(\sprintf('"input_5":"%s"', \pathinfo(__FILE__, PATHINFO_BASENAME))) |
|
138
|
|
|
->assertContains('"input_6":["option 1","option 3"]') |
|
139
|
|
|
; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @test |
|
144
|
|
|
*/ |
|
145
|
|
|
public function form_actions_by_field_name(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->browser() |
|
148
|
|
|
->visit('/page1') |
|
149
|
|
|
->fillField('input_1', 'Kevin') |
|
150
|
|
|
->checkField('input_2') |
|
151
|
|
|
->uncheckField('input_3') |
|
152
|
|
|
->selectFieldOption('input_4', 'option 2') |
|
153
|
|
|
->attachFile('input_5', __FILE__) |
|
154
|
|
|
->selectFieldOptions('input_6[]', ['option 1', 'option 3']) |
|
155
|
|
|
->click('Submit') |
|
156
|
|
|
->assertOn('/submit-form') |
|
157
|
|
|
->assertContains('"input_1":"Kevin"') |
|
158
|
|
|
->assertContains('"input_2":"on"') |
|
159
|
|
|
->assertNotContains('"input_3') |
|
160
|
|
|
->assertContains('"input_4":"option 2"') |
|
161
|
|
|
->assertContains(\sprintf('"input_5":"%s"', \pathinfo(__FILE__, PATHINFO_BASENAME))) |
|
162
|
|
|
->assertContains('"input_6":["option 1","option 3"]') |
|
163
|
|
|
; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @test |
|
168
|
|
|
*/ |
|
169
|
|
|
public function can_dump_html_element(): void |
|
170
|
|
|
{ |
|
171
|
|
|
$dumpedValues[] = null; |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
VarDumper::setHandler(function($var) use (&$dumpedValues) { |
|
174
|
|
|
$dumpedValues[] = $var; |
|
175
|
|
|
}); |
|
176
|
|
|
|
|
177
|
|
|
$this->browser() |
|
178
|
|
|
->visit('/page1') |
|
179
|
|
|
->dump('p#link') |
|
180
|
|
|
; |
|
181
|
|
|
|
|
182
|
|
|
VarDumper::setHandler(); |
|
183
|
|
|
|
|
184
|
|
|
// a null value is added to the beginning |
|
185
|
|
|
$dumped = \array_values(\array_filter($dumpedValues))[0]; |
|
186
|
|
|
|
|
187
|
|
|
$this->assertSame('<a href="/page2">a link</a> not a link', $dumped); |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|