From 4cf7979e2c322f7af47008247a2301076f8f8222 Mon Sep 17 00:00:00 2001 From: Richard Browne Date: Tue, 19 Mar 2024 09:31:14 +0000 Subject: [PATCH 1/5] Add Helper: `elementScreenshot` --- src/Browser.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Browser.php b/src/Browser.php index 229e42543..1bd97768f 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -448,6 +448,30 @@ public function responsiveScreenshots($name) return $this; } + /** + * Take a screenshot of a specific element and store it with the given name. + * + * @param string $selector + * @param string $name + * @return $this + */ + public function elementScreenshot($selector, $name) + { + $filePath = sprintf('%s/%s.png', rtrim(static::$storeScreenshotsAt, '/'), $name); + + $directoryPath = dirname($filePath); + + if (! is_dir($directoryPath)) { + mkdir($directoryPath, 0777, true); + } + + $this->scrollIntoView($selector) + ->driver->findElement(WebDriverBy::cssSelector($selector)) + ->takeElementScreenshot($filePath); + + return $this; + } + /** * Store the console output with the given name. * From 8334dd2acc233ae73c52b328eaee22a8d93a02d5 Mon Sep 17 00:00:00 2001 From: Richard Browne Date: Tue, 19 Mar 2024 09:33:30 +0000 Subject: [PATCH 2/5] Tests added for new helper `elementScreenshot` --- tests/Unit/BrowserTest.php | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/Unit/BrowserTest.php b/tests/Unit/BrowserTest.php index 4669a1588..ee476b27d 100644 --- a/tests/Unit/BrowserTest.php +++ b/tests/Unit/BrowserTest.php @@ -3,7 +3,10 @@ namespace Laravel\Dusk\Tests\Unit; use Facebook\WebDriver\Remote\RemoteKeyboard; +use Facebook\WebDriver\Remote\RemoteWebDriver; +use Facebook\WebDriver\Remote\RemoteWebElement; use Facebook\WebDriver\Remote\WebDriverBrowserType; +use Facebook\WebDriver\WebDriverBy; use Facebook\WebDriver\WebDriverKeys; use Laravel\Dusk\Browser; use Laravel\Dusk\Keyboard; @@ -241,6 +244,60 @@ public function test_screenshot_in_subdirectory() $this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png'); } + public function test_element_screenshot() + { + $elementMock = $this->createMock(RemoteWebElement::class); + $elementMock->expects($this->once()) + ->method('takeElementScreenshot') + ->willReturnCallback(function ($filePath) { + return touch($filePath); + }); + + $driverMock = $this->createMock(RemoteWebDriver::class); + $driverMock->expects($this->once()) + ->method('findElement') + ->with(WebDriverBy::cssSelector('#selector')) + ->willReturn($elementMock); + + $browser = new Browser($driverMock); + + $browser::$storeScreenshotsAt = sys_get_temp_dir(); + + $browser->elementScreenshot( + '#selector', + $name = 'screenshot-01', + ); + + $this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png'); + } + + public function test_element_screenshot_in_subdirectory() + { + $elementMock = $this->createMock(RemoteWebElement::class); + $elementMock->expects($this->once()) + ->method('takeElementScreenshot') + ->willReturnCallback(function ($filePath) { + return touch($filePath); + }); + + $driverMock = $this->createMock(RemoteWebDriver::class); + $driverMock->expects($this->once()) + ->method('findElement') + ->with(WebDriverBy::cssSelector('#selector')) + ->willReturn($elementMock); + + $browser = new Browser($driverMock); + + $browser::$storeScreenshotsAt = sys_get_temp_dir(); + + $browser->elementScreenshot( + '#selector', + $name = uniqid('random').'/sub/dir/screenshot-01', + ); + + $this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png'); + } + public function test_can_disable_fit_on_failure() { $this->browser->fitOnFailure = true; From 307130974890f908099531e37a91d7b98edd8026 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 19 Mar 2024 14:15:53 -0600 Subject: [PATCH 3/5] rename method --- src/Browser.php | 2 +- tests/Unit/BrowserTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Browser.php b/src/Browser.php index 1bd97768f..5a4b2d09a 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -455,7 +455,7 @@ public function responsiveScreenshots($name) * @param string $name * @return $this */ - public function elementScreenshot($selector, $name) + public function screenshotElement($selector, $name) { $filePath = sprintf('%s/%s.png', rtrim(static::$storeScreenshotsAt, '/'), $name); diff --git a/tests/Unit/BrowserTest.php b/tests/Unit/BrowserTest.php index ee476b27d..bf47f422c 100644 --- a/tests/Unit/BrowserTest.php +++ b/tests/Unit/BrowserTest.php @@ -263,7 +263,7 @@ public function test_element_screenshot() $browser::$storeScreenshotsAt = sys_get_temp_dir(); - $browser->elementScreenshot( + $browser->screenshotElement( '#selector', $name = 'screenshot-01', ); @@ -290,7 +290,7 @@ public function test_element_screenshot_in_subdirectory() $browser::$storeScreenshotsAt = sys_get_temp_dir(); - $browser->elementScreenshot( + $browser->screenshotElement( '#selector', $name = uniqid('random').'/sub/dir/screenshot-01', ); From a825b4a912c454c50e0e9e0dbb5c473d1c5d306a Mon Sep 17 00:00:00 2001 From: Richard Browne Date: Sat, 23 Mar 2024 16:41:13 +0000 Subject: [PATCH 4/5] Support Dusk Selectors in `screenshotElement` --- src/Browser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Browser.php b/src/Browser.php index 5a4b2d09a..7471226a2 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -466,7 +466,7 @@ public function screenshotElement($selector, $name) } $this->scrollIntoView($selector) - ->driver->findElement(WebDriverBy::cssSelector($selector)) + ->driver->findElement(WebDriverBy::cssSelector($this->resolver->format($selector))) ->takeElementScreenshot($filePath); return $this; From d7e627501d0b92175067c884bfe9a1156f42dc47 Mon Sep 17 00:00:00 2001 From: Richard Browne Date: Sun, 24 Mar 2024 18:54:48 +0000 Subject: [PATCH 5/5] Fix test for `screenshotElement` --- tests/Unit/BrowserTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Unit/BrowserTest.php b/tests/Unit/BrowserTest.php index bf47f422c..7d2d58907 100644 --- a/tests/Unit/BrowserTest.php +++ b/tests/Unit/BrowserTest.php @@ -244,7 +244,7 @@ public function test_screenshot_in_subdirectory() $this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png'); } - public function test_element_screenshot() + public function test_screenshot_element() { $elementMock = $this->createMock(RemoteWebElement::class); $elementMock->expects($this->once()) @@ -256,7 +256,7 @@ public function test_element_screenshot() $driverMock = $this->createMock(RemoteWebDriver::class); $driverMock->expects($this->once()) ->method('findElement') - ->with(WebDriverBy::cssSelector('#selector')) + ->with(WebDriverBy::cssSelector('body #selector')) ->willReturn($elementMock); $browser = new Browser($driverMock); @@ -271,7 +271,7 @@ public function test_element_screenshot() $this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png'); } - public function test_element_screenshot_in_subdirectory() + public function test_screenshot_element_in_subdirectory() { $elementMock = $this->createMock(RemoteWebElement::class); $elementMock->expects($this->once()) @@ -283,7 +283,7 @@ public function test_element_screenshot_in_subdirectory() $driverMock = $this->createMock(RemoteWebDriver::class); $driverMock->expects($this->once()) ->method('findElement') - ->with(WebDriverBy::cssSelector('#selector')) + ->with(WebDriverBy::cssSelector('body #selector')) ->willReturn($elementMock); $browser = new Browser($driverMock);