diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php index 096d737393..f33708090f 100644 --- a/src/Appwrite/Screenshots/Client.php +++ b/src/Appwrite/Screenshots/Client.php @@ -9,6 +9,17 @@ use Utopia\Psr7\Request\Factory; class Client { + public const VIEWPORT_WIDTH = 1280; + + public const VIEWPORT_HEIGHT = 720; + + /** + * Captured pixels per CSS pixel. Above 1 the page is rendered at a higher + * resolution than the viewport, so previews stay sharp when the console + * displays them on high-density screens or scales them up. + */ + public const SCALE = 1.5; + private Factory $factory; /** @@ -33,6 +44,11 @@ class Client 'theme' => $theme, 'headers' => $headers, 'sleep' => $sleep, + 'viewport' => [ + 'width' => self::VIEWPORT_WIDTH, + 'height' => self::VIEWPORT_HEIGHT, + ], + 'deviceScaleFactor' => self::SCALE, ])); $status = $response->getStatusCode(); diff --git a/tests/resources/sites/static-themed/screenshot-dark.png b/tests/resources/sites/static-themed/screenshot-dark.png index 199a09b902..52b5319b9c 100644 Binary files a/tests/resources/sites/static-themed/screenshot-dark.png and b/tests/resources/sites/static-themed/screenshot-dark.png differ diff --git a/tests/resources/sites/static-themed/screenshot-light.png b/tests/resources/sites/static-themed/screenshot-light.png index 4eae73c5b7..4b451b353f 100644 Binary files a/tests/resources/sites/static-themed/screenshot-light.png and b/tests/resources/sites/static-themed/screenshot-light.png differ diff --git a/tests/unit/Screenshots/ClientTest.php b/tests/unit/Screenshots/ClientTest.php index 6dcfdce28e..ac5efba65f 100644 --- a/tests/unit/Screenshots/ClientTest.php +++ b/tests/unit/Screenshots/ClientTest.php @@ -58,9 +58,41 @@ final class ClientTest extends TestCase 'theme' => 'dark', 'headers' => ['x-appwrite-hostname' => 'example.com'], 'sleep' => 500, + 'viewport' => ['width' => 1280, 'height' => 720], + 'deviceScaleFactor' => 1.5, ], \json_decode((string)$this->request->getBody(), true)); } + public function testCreateCapturesAboveViewportResolution(): void + { + $response = (new Response(200, body: new Stream('png-bytes'))) + ->withHeader('Content-Type', 'image/png'); + + $this->client($response)->create('http://appwrite/', 'light'); + + $body = \json_decode((string)$this->request->getBody(), true); + + // Left unset, the browser service defaults to a scale factor of 1, which + // captures exactly one pixel per CSS pixel and renders soft previews. + $this->assertGreaterThan(1, $body['deviceScaleFactor']); + $this->assertSame(1280, $body['viewport']['width']); + $this->assertSame(720, $body['viewport']['height']); + } + + public function testCreateDoesNotBlockOnSubresources(): void + { + $response = (new Response(200, body: new Stream('png-bytes'))) + ->withHeader('Content-Type', 'image/png'); + + $this->client($response)->create('http://appwrite/', 'light'); + + // The browser service navigates with `domcontentloaded` unless told + // otherwise. Waiting for `load` risks exhausting the request timeout on + // sites with slow or hanging subresources, which yields no screenshot at + // all rather than an early one. + $this->assertArrayNotHasKey('waitUntil', \json_decode((string)$this->request->getBody(), true)); + } + public function testCreateError(): void { $response = (new Response(400, body: new Stream('{"error":"Timeout exceeded"}')))