-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathNodeElementTest.php
545 lines (423 loc) · 14.4 KB
/
NodeElementTest.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
namespace Behat\Mink\Tests\Element;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\DriverException;
class NodeElementTest extends ElementTestCase
{
public function testGetXpath()
{
$node = new NodeElement('some custom xpath', $this->session);
$this->assertEquals('some custom xpath', $node->getXpath());
$this->assertNotEquals('not some custom xpath', $node->getXpath());
}
public function testGetText()
{
$expected = 'val1';
$node = new NodeElement('text_tag', $this->session);
$this->driver
->expects($this->once())
->method('getText')
->with('text_tag')
->will($this->returnValue($expected));
$this->assertEquals($expected, $node->getText());
}
public function testGetOuterHtml()
{
$expected = 'val1';
$node = new NodeElement('text_tag', $this->session);
$this->driver
->expects($this->once())
->method('getOuterHtml')
->with('text_tag')
->will($this->returnValue($expected));
$this->assertEquals($expected, $node->getOuterHtml());
}
public function testElementIsValid()
{
$elementXpath = 'some xpath';
$node = new NodeElement($elementXpath, $this->session);
$this->driver
->expects($this->once())
->method('find')
->with($elementXpath)
->willReturn(array($this->createStub(NodeElement::class)));
$this->assertTrue($node->isValid());
}
public function testElementIsNotValid()
{
$node = new NodeElement('some xpath', $this->session);
$this->driver
->expects($this->once())
->method('find')
->with('some xpath')
->willReturn(array());
$this->assertFalse($node->isValid(), 'no elements found is invalid element');
}
public function testElementIsNotValidWithMultipleFound()
{
$node = new NodeElement('some xpath', $this->session);
$this->driver
->expects($this->once())
->method('find')
->with('some xpath')
->willReturn(array($this->createStub(NodeElement::class), $this->createStub(NodeElement::class)));
$this->assertFalse($node->isValid(), 'more then 1 element found is invalid element');
}
public function testWaitForSuccess()
{
$callCounter = 0;
$node = new NodeElement('some xpath', $this->session);
$result = $node->waitFor(5, function ($givenNode) use (&$callCounter) {
++$callCounter;
if (1 === $callCounter) {
return null;
} elseif (2 === $callCounter) {
return false;
} elseif (3 === $callCounter) {
return array();
}
return $givenNode;
});
$this->assertEquals(4, $callCounter, '->waitFor() tries to locate element several times before failing');
$this->assertSame($node, $result, '->waitFor() returns node found in callback');
}
/**
* @medium
*/
public function testWaitForTimeout()
{
$node = new NodeElement('some xpath', $this->session);
$expectedTimeout = 2;
$startTime = microtime(true);
$result = $node->waitFor($expectedTimeout, function () {
return null;
});
$endTime = microtime(true);
$this->assertNull($result, '->waitFor() returns whatever callback gives');
$this->assertEquals($expectedTimeout, round($endTime - $startTime));
}
public function testHasAttribute()
{
$node = new NodeElement('input_tag', $this->session);
$this->driver
->expects($this->exactly(2))
->method('getAttribute')
->with('input_tag', 'href')
->will($this->onConsecutiveCalls(null, 'http://...'));
$this->assertFalse($node->hasAttribute('href'));
$this->assertTrue($node->hasAttribute('href'));
}
public function testGetAttribute()
{
$expected = 'http://...';
$node = new NodeElement('input_tag', $this->session);
$this->driver
->expects($this->once())
->method('getAttribute')
->with('input_tag', 'href')
->will($this->returnValue($expected));
$this->assertEquals($expected, $node->getAttribute('href'));
}
public function testHasClass()
{
$node = new NodeElement('input_tag', $this->session);
$this->driver
->expects($this->any())
->method('getAttribute')
->with('input_tag', 'class')
->will($this->returnValue('
class1 class2
'));
$this->assertTrue($node->hasClass('class1'), 'The "class1" was found');
$this->assertTrue($node->hasClass('class2'), 'The "class2" was found');
$this->assertFalse($node->hasClass('class3'), 'The "class3" was not found');
}
public function testHasClassWithoutArgument()
{
$node = new NodeElement('input_tag', $this->session);
$this->driver
->expects($this->once())
->method('getAttribute')
->with('input_tag', 'class')
->will($this->returnValue(null));
$this->assertFalse($node->hasClass('class3'));
}
public function testGetValue()
{
$expected = 'val1';
$node = new NodeElement('input_tag', $this->session);
$this->driver
->expects($this->once())
->method('getValue')
->with('input_tag')
->will($this->returnValue($expected));
$this->assertEquals($expected, $node->getValue());
}
public function testSetValue()
{
$expected = 'new_val';
$node = new NodeElement('input_tag', $this->session);
$this->driver
->expects($this->once())
->method('setValue')
->with('input_tag', $expected);
$node->setValue($expected);
}
public function testClick()
{
$node = new NodeElement('link_or_button', $this->session);
$this->driver
->expects($this->once())
->method('click')
->with('link_or_button');
$node->click();
}
public function testPress()
{
$node = new NodeElement('link_or_button', $this->session);
$this->driver
->expects($this->once())
->method('click')
->with('link_or_button');
$node->press();
}
public function testRightClick()
{
$node = new NodeElement('elem', $this->session);
$this->driver
->expects($this->once())
->method('rightClick')
->with('elem');
$node->rightClick();
}
public function testDoubleClick()
{
$node = new NodeElement('elem', $this->session);
$this->driver
->expects($this->once())
->method('doubleClick')
->with('elem');
$node->doubleClick();
}
public function testCheck()
{
$node = new NodeElement('checkbox_or_radio', $this->session);
$this->driver
->expects($this->once())
->method('check')
->with('checkbox_or_radio');
$node->check();
}
public function testUncheck()
{
$node = new NodeElement('checkbox_or_radio', $this->session);
$this->driver
->expects($this->once())
->method('uncheck')
->with('checkbox_or_radio');
$node->uncheck();
}
public function testSelectOption()
{
$node = new NodeElement('select', $this->session);
$option = $this->createMock(NodeElement::class);
$option
->expects($this->once())
->method('getValue')
->will($this->returnValue('item1'));
$this->driver
->expects($this->once())
->method('getTagName')
->with('select')
->will($this->returnValue('select'));
$this->elementFinder->expects($this->once())
->method('findAll')
->with('named', array('option', 'item1'), 'select')
->willReturn(array($option));
$this->driver
->expects($this->once())
->method('selectOption')
->with('select', 'item1', false);
$node->selectOption('item1');
}
public function testSelectOptionNotFound()
{
$node = new NodeElement('select', $this->session);
$this->driver
->expects($this->once())
->method('getTagName')
->with('select')
->will($this->returnValue('select'));
$this->elementFinder->expects($this->once())
->method('findAll')
->with('named', array('option', 'item1'), 'select')
->willReturn(array());
$this->expectException('\Behat\Mink\Exception\ElementNotFoundException');
$node->selectOption('item1');
}
public function testSelectOptionOtherTag()
{
$node = new NodeElement('div', $this->session);
$this->driver
->expects($this->once())
->method('getTagName')
->with('div')
->will($this->returnValue('div'));
$this->driver
->expects($this->once())
->method('selectOption')
->with('div', 'item1', false);
$node->selectOption('item1');
}
public function testGetTagName()
{
$node = new NodeElement('html//h3', $this->session);
$this->driver
->expects($this->once())
->method('getTagName')
->with('html//h3')
->will($this->returnValue('h3'));
$this->assertEquals('h3', $node->getTagName());
}
public function testGetParent()
{
$node = new NodeElement('elem', $this->session);
$parent = $this->getMockBuilder('Behat\Mink\Element\NodeElement')
->disableOriginalConstructor()
->getMock();
$this->elementFinder->expects($this->once())
->method('findAll')
->with('xpath', '..', 'elem')
->willReturn(array($parent));
$this->assertSame($parent, $node->getParent());
}
public function testGetParentNotFound()
{
$node = new NodeElement('elem', $this->session);
$this->elementFinder->expects($this->once())
->method('findAll')
->with('xpath', '..', 'elem')
->willReturn(array());
$this->expectException(DriverException::class);
$this->expectExceptionMessage('Could not find the element parent. Maybe the element has been removed from the page.');
$node->getParent();
}
public function testAttachFile()
{
$node = new NodeElement('elem', $this->session);
$this->driver
->expects($this->once())
->method('attachFile')
->with('elem', 'path');
$node->attachFile('path');
}
public function testIsVisible()
{
$node = new NodeElement('some_xpath', $this->session);
$this->driver
->expects($this->exactly(2))
->method('isVisible')
->with('some_xpath')
->will($this->onConsecutiveCalls(true, false));
$this->assertTrue($node->isVisible());
$this->assertFalse($node->isVisible());
}
public function testIsChecked()
{
$node = new NodeElement('some_xpath', $this->session);
$this->driver
->expects($this->exactly(2))
->method('isChecked')
->with('some_xpath')
->will($this->onConsecutiveCalls(true, false));
$this->assertTrue($node->isChecked());
$this->assertFalse($node->isChecked());
}
public function testIsSelected()
{
$node = new NodeElement('some_xpath', $this->session);
$this->driver
->expects($this->exactly(2))
->method('isSelected')
->with('some_xpath')
->will($this->onConsecutiveCalls(true, false));
$this->assertTrue($node->isSelected());
$this->assertFalse($node->isSelected());
}
public function testFocus()
{
$node = new NodeElement('some-element', $this->session);
$this->driver
->expects($this->once())
->method('focus')
->with('some-element');
$node->focus();
}
public function testBlur()
{
$node = new NodeElement('some-element', $this->session);
$this->driver
->expects($this->once())
->method('blur')
->with('some-element');
$node->blur();
}
public function testMouseOver()
{
$node = new NodeElement('some-element', $this->session);
$this->driver
->expects($this->once())
->method('mouseOver')
->with('some-element');
$node->mouseOver();
}
public function testDragTo()
{
$node = new NodeElement('some_tag1', $this->session);
$target = $this->getMockBuilder('Behat\Mink\Element\ElementInterface')->getMock();
$target->expects($this->any())
->method('getXPath')
->will($this->returnValue('some_tag2'));
$this->driver
->expects($this->once())
->method('dragTo')
->with('some_tag1', 'some_tag2');
$node->dragTo($target);
}
public function testKeyPress()
{
$node = new NodeElement('elem', $this->session);
$this->driver
->expects($this->once())
->method('keyPress')
->with('elem', 'key');
$node->keyPress('key');
}
public function testKeyDown()
{
$node = new NodeElement('elem', $this->session);
$this->driver
->expects($this->once())
->method('keyDown')
->with('elem', 'key');
$node->keyDown('key');
}
public function testKeyUp()
{
$node = new NodeElement('elem', $this->session);
$this->driver
->expects($this->once())
->method('keyUp')
->with('elem', 'key');
$node->keyUp('key');
}
public function testSubmitForm()
{
$node = new NodeElement('some_xpath', $this->session);
$this->driver
->expects($this->once())
->method('submitForm')
->with('some_xpath');
$node->submit();
}
}