Skip to content

Commit 37666d6

Browse files
committed
feat: adds elementScreenshot
1 parent bf66a0f commit 37666d6

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

.temp/e2e/runtime.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { test, expect } from '@playwright/test';
22

33
test('runtime', async ({ page }) => {
44
await page.goto('https://laravel.com');
5-
await expect(page).toHaveTitle(/Laravel/);
5+
await page.locator('footer').screenshot({ path: '/Users/franciscobarrento/Codex/open-source/pest/pest-plugin-browser/tests/Feature/section.png' });
66
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pest\Browser\Operations;
6+
7+
use Pest\Browser\Contracts\Operation;
8+
9+
/**
10+
* @internal
11+
*/
12+
final readonly class ElementScreenshot implements Operation
13+
{
14+
/**
15+
* Creates an operation instance.
16+
*/
17+
public function __construct(
18+
private string $selector,
19+
private string $path,
20+
) {
21+
//
22+
}
23+
24+
public function compile(): string
25+
{
26+
return sprintf("await page.locator('%s').screenshot({ path: '%s' });", $this->selector, $this->path);
27+
}
28+
}

src/PendingTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public function build(): void
5050
expect($result->ok())->toBeTrue();
5151
}
5252

53+
public function elementScreenshot(string $selector, string $path): self
54+
{
55+
$this->operations[] = new Operations\ElementScreenshot($selector, $path);
56+
57+
return $this;
58+
}
59+
5360
/**
5461
* Ends the chain and builds the test result.
5562
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
use function Pest\Browser\visit;
4+
5+
test('it takes a screenshot of an element', function (): void {
6+
$filePath = __DIR__.'/footer.png';
7+
8+
visit('https://laravel.com')
9+
->elementScreenshot('footer', $filePath);
10+
11+
12+
expect(file_exists($filePath))
13+
->toBeTrue();
14+
15+
unlink('footer.png');
16+
});

0 commit comments

Comments
 (0)