|
15 | 15 |
|
16 | 16 | namespace Facebook\WebDriver; |
17 | 17 |
|
| 18 | +use Facebook\WebDriver\Exception\NoSuchElementException; |
| 19 | +use Facebook\WebDriver\Remote\RemoteWebElement; |
| 20 | + |
18 | 21 | /** |
19 | 22 | * @coversDefaultClass Facebook\WebDriver\Remote\RemoteWebElement |
20 | 23 | */ |
@@ -282,4 +285,53 @@ public function testShouldCompareEqualsElement() |
282 | 285 | $this->assertFalse($firstElement->equals($differentElement)); |
283 | 286 | $this->assertFalse($differentElement->equals($againTheFirstElement)); |
284 | 287 | } |
| 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 | + } |
285 | 337 | } |
0 commit comments