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

Commit 2a7208b

Browse files
andrewnicolsOndraM
authored andcommitted
Add unit tests for takeElementScreenshot
1 parent dab6507 commit 2a7208b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/functional/RemoteWebElementTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use Facebook\WebDriver\Exception\NoSuchElementException;
1919
use Facebook\WebDriver\Remote\RemoteWebElement;
20+
use Facebook\WebDriver\Remote\WebDriverBrowserType;
2021

2122
/**
2223
* @coversDefaultClass \Facebook\WebDriver\Remote\RemoteWebElement
@@ -336,4 +337,58 @@ public function testShouldFindMultipleChildElements()
336337
$this->assertCount(3, $childElements); // but we should find only subelements of one <ul>
337338
$this->assertContainsOnlyInstancesOf(RemoteWebElement::class, $childElements);
338339
}
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+
}
339394
}

0 commit comments

Comments
 (0)