symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.91k stars 213 forks source link

Taking a Full Page screenshot using Symfony Panther? #587

Open solkad opened 1 year ago

solkad commented 1 year ago

I was able to use Panther to take a full page screenshot using the following code, which uses document.documentElement:

<?php

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Panther\Client;
use Facebook\WebDriver\WebDriverDimension;

$client = Client::createChromeClient();
$client->request('GET', 'http://bot.sannysoft.com');

$width = $client->executeScript('return document.documentElement.scrollWidth');
$height = $client->executeScript('return document.documentElement.scrollHeight');

$size = new WebDriverDimension($width, $height);
$client->manage()->window()->setSize($size);
$client->takeScreenshot('screenshot.png');

However, I'm not sure this will always work. Is there a default way in Panther to take a full page screenshot? I found references online to using larger window dimensions, but not anything about an automatically sized screenshot.

In Chrome-PHP, it says, "You can also take a screenshot for the full-page layout (not only the viewport) using $page->getFullPageClip with attribute captureBeyondViewport = true":

$screenshot = $page->screenshot([
    'captureBeyondViewport' => true,
    'clip' => $page->getFullPageClip(),
    'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg',
]);

In PuPHPeteer, you can set fullPage to true:

$page->screenshot(['path' => 'example.png', 'fullPage'=> true]);

I don't know if there is any sort of built in way to take a full page screenshot like that in Panther or not. Or if you have to set the width and height manually.