Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 11b1d4d

Browse files
committed
Add functional tests for findElement for child elements
1 parent 3581da1 commit 11b1d4d

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

tests/functional/RemoteWebDriverFindElementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
class RemoteWebDriverFindElementTest extends WebDriverTestCase
2626
{
27-
public function testShouldThrowExceptionOfElementCannotBeFound()
27+
public function testShouldThrowExceptionIfElementCannotBeFound()
2828
{
2929
$this->driver->get($this->getTestPageUrl('index.html'));
3030

@@ -58,7 +58,7 @@ public function testShouldFindMultipleElements()
5858
$elements = $this->driver->findElements(WebDriverBy::cssSelector('ul > li'));
5959

6060
$this->assertInternalType('array', $elements);
61-
$this->assertCount(3, $elements);
61+
$this->assertCount(5, $elements);
6262
$this->assertContainsOnlyInstancesOf(RemoteWebElement::class, $elements);
6363
}
6464
}

tests/functional/RemoteWebElementTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace Facebook\WebDriver;
1717

18+
use Facebook\WebDriver\Exception\NoSuchElementException;
19+
use Facebook\WebDriver\Remote\RemoteWebElement;
20+
1821
/**
1922
* @coversDefaultClass Facebook\WebDriver\Remote\RemoteWebElement
2023
*/
@@ -282,4 +285,53 @@ public function testShouldCompareEqualsElement()
282285
$this->assertFalse($firstElement->equals($differentElement));
283286
$this->assertFalse($differentElement->equals($againTheFirstElement));
284287
}
288+
289+
/**
290+
* @covers ::findElement
291+
*/
292+
public function testShouldThrowExceptionIfChildElementCannotBeFound()
293+
{
294+
$this->driver->get($this->getTestPageUrl('index.html'));
295+
$element = $this->driver->findElement(WebDriverBy::cssSelector('ul.list'));
296+
297+
$this->expectException(NoSuchElementException::class);
298+
$element->findElement(WebDriverBy::id('not_existing'));
299+
}
300+
301+
public function testShouldFindChildElementIfExistsOnAPage()
302+
{
303+
$this->driver->get($this->getTestPageUrl('index.html'));
304+
$element = $this->driver->findElement(WebDriverBy::cssSelector('ul.list'));
305+
306+
$childElement = $element->findElement(WebDriverBy::cssSelector('li'));
307+
308+
$this->assertInstanceOf(RemoteWebElement::class, $childElement);
309+
$this->assertSame('li', $childElement->getTagName());
310+
$this->assertSame('First', $childElement->getText());
311+
}
312+
313+
public function testShouldReturnEmptyArrayIfChildElementsCannotBeFound()
314+
{
315+
$this->driver->get($this->getTestPageUrl('index.html'));
316+
$element = $this->driver->findElement(WebDriverBy::cssSelector('ul.list'));
317+
318+
$childElements = $element->findElements(WebDriverBy::cssSelector('not_existing'));
319+
320+
$this->assertInternalType('array', $childElements);
321+
$this->assertCount(0, $childElements);
322+
}
323+
324+
public function testShouldFindMultipleChildElements()
325+
{
326+
$this->driver->get($this->getTestPageUrl('index.html'));
327+
$element = $this->driver->findElement(WebDriverBy::cssSelector('ul.list'));
328+
329+
$allElements = $this->driver->findElements(WebDriverBy::cssSelector('li'));
330+
$childElements = $element->findElements(WebDriverBy::cssSelector('li'));
331+
332+
$this->assertInternalType('array', $childElements);
333+
$this->assertCount(5, $allElements); // there should be 5 <li> elements on page
334+
$this->assertCount(3, $childElements); // but we should find only subelements of one <ul>
335+
$this->assertContainsOnlyInstancesOf(RemoteWebElement::class, $childElements);
336+
}
285337
}

tests/functional/web/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ <h1 id='welcome'>Welcome to the facebook/php-webdriver testing page.</h1>
3030
<li>Third</li>
3131
</ul>
3232

33+
<ul class="other-list">
34+
<li>Foo</li>
35+
<li>Bar</li>
36+
</ul>
37+
3338
<p id="text-simple" role="note" style="height: 5em; border: 1px solid black;">Foo bar text</p>
3439
<p id="text-with-spaces">
3540
Multiple spaces are
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Facebook\WebDriver\Remote;
17+
18+
/**
19+
* Unit part of RemoteWebDriver tests. Ie. tests for behavior which do not interact with the real remote server.
20+
*
21+
* @coversDefaultClass Facebook\WebDriver\Remote\RemoteWebElement
22+
*/
23+
class RemoteWebElementTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @covers ::__construct
27+
* @covers ::getId
28+
*/
29+
public function testShouldConstructNewInstance()
30+
{
31+
$executeMethod = $this->createMock(RemoteExecuteMethod::class);
32+
$element = new RemoteWebElement($executeMethod, 333);
33+
34+
$this->assertSame(333, $element->getID());
35+
}
36+
}

0 commit comments

Comments
 (0)