Skip to content

Commit f0d03fb

Browse files
authored
Merge pull request #662 from SjorsO/screenshots-in-subdir
[5.0] Allow saving screenshots in a subdirectory
2 parents 9af5a65 + 691fc76 commit f0d03fb

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/Browser.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,15 @@ public function move($x, $y)
287287
*/
288288
public function screenshot($name)
289289
{
290-
$this->driver->takeScreenshot(
291-
sprintf('%s/%s.png', rtrim(static::$storeScreenshotsAt, '/'), $name)
292-
);
290+
$filePath = sprintf('%s/%s.png', rtrim(static::$storeScreenshotsAt, '/'), $name);
291+
292+
$directoryPath = dirname($filePath);
293+
294+
if (! is_dir($directoryPath)) {
295+
mkdir($directoryPath, 0777, true);
296+
}
297+
298+
$this->driver->takeScreenshot($filePath);
293299

294300
return $this;
295301
}

tests/BrowserTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212
class BrowserTest extends TestCase
1313
{
14+
/** @var \Mockery\MockInterface */
15+
private $driver;
16+
17+
/** @var Browser */
18+
private $browser;
19+
20+
protected function setUp(): void
21+
{
22+
$this->driver = m::mock(stdClass::class);
23+
24+
$this->browser = new Browser($this->driver);
25+
}
26+
1427
protected function tearDown(): void
1528
{
1629
m::close();
@@ -155,6 +168,36 @@ public function test_disable_console()
155168

156169
$browser->storeConsoleLog('file');
157170
}
171+
172+
public function test_screenshot()
173+
{
174+
$this->driver->shouldReceive('takeScreenshot')->andReturnUsing(function ($filePath) {
175+
touch($filePath);
176+
});
177+
178+
Browser::$storeScreenshotsAt = sys_get_temp_dir();
179+
180+
$this->browser->screenshot(
181+
$name = 'screenshot-01'
182+
);
183+
184+
$this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png');
185+
}
186+
187+
public function test_screenshot_in_subdirectory()
188+
{
189+
$this->driver->shouldReceive('takeScreenshot')->andReturnUsing(function ($filePath) {
190+
touch($filePath);
191+
});
192+
193+
Browser::$storeScreenshotsAt = sys_get_temp_dir();
194+
195+
$this->browser->screenshot(
196+
$name = uniqid('random').'/sub/dir/screenshot-01'
197+
);
198+
199+
$this->assertFileExists(Browser::$storeScreenshotsAt.'/'.$name.'.png');
200+
}
158201
}
159202

160203
class BrowserTestPage extends Page

0 commit comments

Comments
 (0)