forked from minkphp/driver-testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangeEventTest.php
164 lines (136 loc) · 5.49 KB
/
ChangeEventTest.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
<?php
namespace Behat\Mink\Tests\Driver\Js;
use Behat\Mink\Tests\Driver\TestCase;
use Behat\Mink\Tests\Driver\Util\FixturesKernel;
/**
* @group slow
*/
final class ChangeEventTest extends TestCase
{
/**
* 'change' event should be fired after selecting an <option> in a <select>.
*
* TODO check whether this test is redundant with other change event tests.
*/
public function testIssue255(): void
{
$session = $this->getSession();
$session->visit($this->pathTo('/issue255.html'));
$session->getPage()->selectFieldOption('foo_select', 'Option 3');
$session->wait(2000, '$("#output_foo_select").text() !== ""');
$this->assertEquals('onChangeSelect', $this->getAssertSession()->elementExists('css', '#output_foo_select')->getText());
}
public function testIssue178(): void
{
$session = $this->getSession();
$session->visit($this->pathTo('/issue178.html'));
$this->findById('source')->setValue('foo');
$this->assertEquals('foo', $this->findById('target')->getText());
}
/**
* @dataProvider setValueChangeEventDataProvider
* @group change-event-detector
*/
public function testSetValueChangeEvent(string $elementId, string $valueForEmpty, string $valueForFilled = ''): void
{
if ($elementId === 'the-file') {
$valueForEmpty = $this->mapRemoteFilePath($valueForEmpty);
$valueForFilled = $this->mapRemoteFilePath($valueForFilled);
}
$this->getSession()->visit($this->pathTo('/element_change_detector.html'));
$page = $this->getSession()->getPage();
$input = $this->findById($elementId);
$this->assertNull($page->findById($elementId . '-result'));
// Verify setting value, when control is initially empty.
$input->setValue($valueForEmpty);
$this->assertElementChangeCount($elementId, 'initial value setting triggers change event');
if ($valueForFilled) {
// Verify setting value, when control already has a value.
$this->findById('results')->click();
$input->setValue($valueForFilled);
$this->assertElementChangeCount($elementId, 'value change triggers change event');
}
}
/**
* @return iterable<string, array{string, string, string}>
*/
public static function setValueChangeEventDataProvider(): iterable
{
yield 'input default' => ['the-input-default', 'from empty', 'from existing'];
yield 'input text' => ['the-input-text', 'from empty', 'from existing'];
yield 'input email' => ['the-email', 'from empty', 'from existing'];
yield 'textarea' => ['the-textarea', 'from empty', 'from existing'];
yield 'file' => ['the-file', self::WEB_FIXTURES_DIR . '/file1.txt', self::WEB_FIXTURES_DIR . '/file2.txt'];
yield 'select' => ['the-select', '30', ''];
yield 'radio' => ['the-radio-m', 'm', ''];
}
/**
* @dataProvider selectOptionChangeEventDataProvider
* @group change-event-detector
*/
public function testSelectOptionChangeEvent(string $elementId, string $elementValue): void
{
$this->getSession()->visit($this->pathTo('/element_change_detector.html'));
$page = $this->getSession()->getPage();
$input = $this->findById($elementId);
$this->assertNull($page->findById($elementId . '-result'));
$input->selectOption($elementValue);
$this->assertElementChangeCount($elementId);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function selectOptionChangeEventDataProvider(): iterable
{
yield 'select' => ['the-select', '30'];
yield 'radio' => ['the-radio-m', 'm'];
}
/**
* @dataProvider checkboxTestWayDataProvider
* @group change-event-detector
*/
public function testCheckChangeEvent(bool $useSetValue): void
{
$this->getSession()->visit($this->pathTo('/element_change_detector.html'));
$page = $this->getSession()->getPage();
$checkbox = $this->findById('the-unchecked-checkbox');
$this->assertNull($page->findById('the-unchecked-checkbox-result'));
if ($useSetValue) {
$checkbox->setValue(true);
} else {
$checkbox->check();
}
$this->assertElementChangeCount('the-unchecked-checkbox');
}
/**
* @dataProvider checkboxTestWayDataProvider
* @group change-event-detector
*/
public function testUncheckChangeEvent(bool $useSetValue): void
{
$this->getSession()->visit($this->pathTo('/element_change_detector.html'));
$page = $this->getSession()->getPage();
$checkbox = $this->findById('the-checked-checkbox');
$this->assertNull($page->findById('the-checked-checkbox-result'));
if ($useSetValue) {
$checkbox->setValue(false);
} else {
$checkbox->uncheck();
}
$this->assertElementChangeCount('the-checked-checkbox');
}
/**
* @return iterable<array{mixed}>
*/
public static function checkboxTestWayDataProvider(): iterable
{
yield [true];
yield [false];
}
private function assertElementChangeCount(string $elementId, string $message = ''): void
{
$counterElement = $this->getSession()->getPage()->findById($elementId . '-result');
$actualCount = null === $counterElement ? 0 : $counterElement->getText();
$this->assertEquals('1', $actualCount, $message);
}
}