forked from minkphp/driver-testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectTest.php
167 lines (133 loc) · 5.37 KB
/
SelectTest.php
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
<?php
namespace Behat\Mink\Tests\Driver\Form;
use Behat\Mink\Exception\DriverException;
use Behat\Mink\Tests\Driver\TestCase;
final class SelectTest extends TestCase
{
public function testMultiselect(): void
{
$this->getSession()->visit($this->pathTo('/multiselect_form.html'));
$webAssert = $this->getAssertSession();
$page = $this->getSession()->getPage();
$this->assertEquals('Multiselect Test', $webAssert->elementExists('css', 'h1')->getText());
$selectWithoutOption = $webAssert->fieldExists('select_without_option');
$this->assertSame('', $selectWithoutOption->getValue());
$selectWithNoOptionSelected = $webAssert->fieldExists('select_first_option_is_selected_by_default');
$this->assertEquals('1', $selectWithNoOptionSelected->getValue());
$select = $webAssert->fieldExists('select_number');
$multiSelect = $webAssert->fieldExists('select_multiple_numbers[]');
$secondMultiSelect = $webAssert->fieldExists('select_multiple_values[]');
$this->assertEquals('20', $select->getValue());
$this->assertSame([], $multiSelect->getValue());
$this->assertSame(['2', '3'], $secondMultiSelect->getValue());
$select->selectOption('thirty');
$this->assertEquals('30', $select->getValue());
$multiSelect->selectOption('one', true);
$this->assertSame(['1'], $multiSelect->getValue());
$multiSelect->selectOption('three', true);
$this->assertEquals(['1', '3'], $multiSelect->getValue());
$secondMultiSelect->selectOption('two');
$this->assertSame(['2'], $secondMultiSelect->getValue());
$button = $page->findButton('Register');
$this->assertNotNull($button);
$button->press();
$out = <<<'OUT'
agreement = `off`,
select_first_option_is_selected_by_default = `1`,
select_multiple_numbers = array(
0 = `1`,
1 = `3`,
),
select_multiple_values = array(
0 = `2`,
),
select_number = `30`,
OUT;
$this->assertStringContainsString($out, $page->getContent());
}
/**
* @dataProvider elementSelectedStateCheckDataProvider
*/
public function testElementSelectedStateCheck(string $selectName, string $optionValue, string $optionText): void
{
$session = $this->getSession();
$webAssert = $this->getAssertSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $webAssert->fieldExists($selectName);
$option = $webAssert->elementExists('named', ['option', $optionValue], $select);
$this->assertFalse($option->isSelected());
$select->selectOption($optionText);
$this->assertTrue($option->isSelected());
}
/**
* @return iterable<array{string, string, string}>
*/
public static function elementSelectedStateCheckDataProvider(): iterable
{
return [
['select_number', '30', 'thirty'],
['select_multiple_numbers[]', '2', 'two'],
];
}
public function testSetValueSingleSelect(): void
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $this->getAssertSession()->fieldExists('select_number');
$select->setValue('10');
$this->assertEquals('10', $select->getValue());
}
public function testSetValueMultiSelect(): void
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $this->getAssertSession()->fieldExists('select_multiple_values[]');
$select->setValue(['1', '2']);
$this->assertEquals(['1', '2'], $select->getValue());
}
/**
* @dataProvider provideBooleanValues
*/
public function testSetBooleanValue(bool $value): void
{
$session = $this->getSession();
$session->visit($this->pathTo('/multiselect_form.html'));
$select = $this->getAssertSession()->fieldExists('select_number');
$this->expectException(DriverException::class);
$select->setValue($value);
}
/**
* @return iterable<array{bool}>
*/
public static function provideBooleanValues(): iterable
{
yield [true];
yield [false];
}
/**
* @see https://github.com/Behat/Mink/issues/193
*/
public function testOptionWithoutValue(): void
{
$session = $this->getSession();
$session->visit($this->pathTo('/issue193.html'));
$session->getPage()->selectFieldOption('options-without-values', 'Two');
$this->assertEquals('Two', $this->findById('options-without-values')->getValue());
$this->assertTrue($this->findById('two')->isSelected());
$this->assertFalse($this->findById('one')->isSelected());
$session->getPage()->selectFieldOption('options-with-values', 'two');
$this->assertEquals('two', $this->findById('options-with-values')->getValue());
}
/**
* @see https://github.com/Behat/Mink/issues/131
*/
public function testAccentuatedOption(): void
{
$this->getSession()->visit($this->pathTo('/issue131.html'));
$page = $this->getSession()->getPage();
$page->selectFieldOption('foobar', 'Gimme some accentués characters');
$field = $page->findField('foobar');
$this->assertNotNull($field);
$this->assertEquals('1', $field->getValue());
}
}