|
17 | 17 |
|
18 | 18 | use Facebook\WebDriver\Exception\NoSuchElementException;
|
19 | 19 | use Facebook\WebDriver\Remote\RemoteWebElement;
|
| 20 | +use Facebook\WebDriver\Remote\WebDriverBrowserType; |
20 | 21 |
|
21 | 22 | /**
|
22 | 23 | * @coversDefaultClass \Facebook\WebDriver\Remote\RemoteWebElement
|
@@ -336,4 +337,58 @@ public function testShouldFindMultipleChildElements()
|
336 | 337 | $this->assertCount(3, $childElements); // but we should find only subelements of one <ul>
|
337 | 338 | $this->assertContainsOnlyInstancesOf(RemoteWebElement::class, $childElements);
|
338 | 339 | }
|
| 340 | + |
| 341 | + /** |
| 342 | + * @covers ::takeScreenshot |
| 343 | + */ |
| 344 | + public function testShouldTakeElementScreenshot() |
| 345 | + { |
| 346 | + if (!extension_loaded('gd')) { |
| 347 | + $this->markTestSkipped('GD extension must be enabled'); |
| 348 | + } |
| 349 | + if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::HTMLUNIT) { |
| 350 | + $this->markTestSkipped('Screenshots are not supported by HtmlUnit browser'); |
| 351 | + } |
| 352 | + |
| 353 | + $this->driver->get($this->getTestPageUrl('index.html')); |
| 354 | + |
| 355 | + $firstElement = $this->driver->findElement(WebDriverBy::cssSelector('ul.list')); |
| 356 | + $outputPng = $firstElement->takeElementScreenshot(); |
| 357 | + |
| 358 | + $image = imagecreatefromstring($outputPng); |
| 359 | + $this->assertInternalType('resource', $image); |
| 360 | + |
| 361 | + $size = $firstElement->getSize(); |
| 362 | + $this->assertEquals($size->getWidth(), imagesx($image)); |
| 363 | + $this->assertEquals($size->getHeight(), imagesy($image)); |
| 364 | + } |
| 365 | + |
| 366 | + /** |
| 367 | + * @covers ::takeScreenshot |
| 368 | + */ |
| 369 | + public function testShouldSaveElementScreenshotToFile() |
| 370 | + { |
| 371 | + if (!extension_loaded('gd')) { |
| 372 | + $this->markTestSkipped('GD extension must be enabled'); |
| 373 | + } |
| 374 | + if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::HTMLUNIT) { |
| 375 | + $this->markTestSkipped('Screenshots are not supported by HtmlUnit browser'); |
| 376 | + } |
| 377 | + |
| 378 | + $screenshotPath = sys_get_temp_dir() . '/selenium-screenshot.png'; |
| 379 | + |
| 380 | + $this->driver->get($this->getTestPageUrl('index.html')); |
| 381 | + |
| 382 | + $firstElement = $this->driver->findElement(WebDriverBy::cssSelector('ul.list')); |
| 383 | + $firstElement->takeElementScreenshot($screenshotPath); |
| 384 | + |
| 385 | + $image = imagecreatefrompng($screenshotPath); |
| 386 | + $this->assertInternalType('resource', $image); |
| 387 | + |
| 388 | + $size = $firstElement->getSize(); |
| 389 | + $this->assertEquals($size->getWidth(), imagesx($image)); |
| 390 | + $this->assertEquals($size->getHeight(), imagesy($image)); |
| 391 | + |
| 392 | + unlink($screenshotPath); |
| 393 | + } |
339 | 394 | }
|
0 commit comments