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

Commit 0c36f13

Browse files
committed
Improve take element screenshot unit tests
1 parent 2a7208b commit 0c36f13

File tree

3 files changed

+33
-35
lines changed

3 files changed

+33
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ This project versioning adheres to [Semantic Versioning](http://semver.org/).
55

66
### Added
77
- Experimental W3C WebDriver protocol support. The protocol will be used automatically when remote end (like Geckodriver, newer Chromedriver etc.) supports it.
8-
- `getStatus()` method of RemoteWebDriver to get information about remote-end readiness to create new sessions.
8+
- `getStatus()` method of `RemoteWebDriver` to get information about remote-end readiness to create new sessions.
9+
- `takeElementScreenshot()` method of `RemoteWebElement` to do the obvious - take screenshot of the particular element.
910

1011
### Changed
1112
- Revert no longer needed workaround for Chromedriver bug [2943](https://bugs.chromium.org/p/chromedriver/issues/detail?id=2943).

lib/Remote/RemoteWebElement.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,13 @@ public function takeElementScreenshot($save_as = null)
445445
[':id' => $this->id]
446446
)
447447
);
448+
448449
if ($save_as) {
450+
$directoryPath = dirname($save_as);
451+
if (!file_exists($directoryPath)) {
452+
mkdir($directoryPath, 0777, true);
453+
}
454+
449455
file_put_contents($save_as, $screenshot);
450456
}
451457

tests/functional/RemoteWebElementTest.php

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -339,56 +339,47 @@ public function testShouldFindMultipleChildElements()
339339
}
340340

341341
/**
342-
* @covers ::takeScreenshot
342+
* @covers ::takeElementScreenshot
343+
* @group exclude-saucelabs
343344
*/
344-
public function testShouldTakeElementScreenshot()
345+
public function testShouldTakeAndSaveElementScreenshot()
345346
{
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();
347+
self::skipForJsonWireProtocol('Take element screenshot is only part of W3C protocol');
357348

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-
{
371349
if (!extension_loaded('gd')) {
372350
$this->markTestSkipped('GD extension must be enabled');
373351
}
374352
if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::HTMLUNIT) {
375353
$this->markTestSkipped('Screenshots are not supported by HtmlUnit browser');
376354
}
377355

378-
$screenshotPath = sys_get_temp_dir() . '/selenium-screenshot.png';
356+
// Intentionally save screenshot to subdirectory to tests it is being created
357+
$screenshotPath = sys_get_temp_dir() . '/' . uniqid('php-webdriver-') . '/element-screenshot.png';
379358

380359
$this->driver->get($this->getTestPageUrl('index.html'));
381360

382-
$firstElement = $this->driver->findElement(WebDriverBy::cssSelector('ul.list'));
383-
$firstElement->takeElementScreenshot($screenshotPath);
361+
$element = $this->driver->findElement(WebDriverBy::id('red-box'));
362+
363+
$outputPngString = $element->takeElementScreenshot($screenshotPath);
384364

385-
$image = imagecreatefrompng($screenshotPath);
386-
$this->assertInternalType('resource', $image);
365+
// Assert file output
366+
$imageFromFile = imagecreatefrompng($screenshotPath);
367+
$this->assertEquals(5, imagesx($imageFromFile));
368+
$this->assertEquals(5, imagesy($imageFromFile));
369+
370+
// Validate element is actually red
371+
$this->assertSame(
372+
['red' => 255, 'green' => 0, 'blue' => 0, 'alpha' => 0],
373+
imagecolorsforindex($imageFromFile, imagecolorat($imageFromFile, 0, 0))
374+
);
387375

388-
$size = $firstElement->getSize();
389-
$this->assertEquals($size->getWidth(), imagesx($image));
390-
$this->assertEquals($size->getHeight(), imagesy($image));
376+
// Assert string output
377+
$imageFromString = imagecreatefromstring($outputPngString);
378+
$this->assertInternalType('resource', $imageFromString);
379+
$this->assertEquals(5, imagesx($imageFromString));
380+
$this->assertEquals(5, imagesy($imageFromString));
391381

392382
unlink($screenshotPath);
383+
rmdir(dirname($screenshotPath));
393384
}
394385
}

0 commit comments

Comments
 (0)