zenstruck / browser

A fluent interface for your Symfony functional tests.
MIT License
185 stars 17 forks source link

Configure screenshots_dir in code, expose savedScreenshots #148

Open tacman opened 4 months ago

tacman commented 4 months ago

I want to use this awesome library to navigate my site and save screenshots, not necessarily as part of a functional test, but just a regular app.

final class AppScreenshotsCommand extends InvokableServiceCommand
{
    use PantherTestCaseTrait;
    use HasBrowser;

    public function __invoke(
        IO $io,
    ): void {
        $browser = $this->pantherBrowser(['screenshot_dir' => './screenshots']);
        $browser->visit('/')
            ->wait(4000) // wait for css??
            ->takeScreenshot('homepage.jpg');
        dd($browser->getSavedScreenshots());

How can I set the screenshot_dir in my console command? The above does not work, $options is different in that call.

Also, to get the screenshots at the end, I needed to add a getter to the PantherBrowser.php, would you accept a PR for that?

    public function getSavedScreenshots(): array
    {
        return $this->savedScreenshots;
    }

I am trying to use this to solve the problem of creating screenshots requiring authenticated users, e.g.

https://github.com/Spomky-Labs/pwa-bundle/issues/134

tacman commented 4 months ago

There's probably a more elegant way, but I fixed the directory like this:

        $_SERVER['BROWSER_SCREENSHOT_DIR'] = './screenshots';
        $browser = $this->pantherBrowser();
kbond commented 4 months ago

$_SERVER['BROWSER_SCREENSHOT_DIR'] = './screenshots';

Yeah, that's the way currently. The env var options got a bit out of control as I added features - I'd like to normalize this at some point.

I needed to add a getter to the PantherBrowser.php, would you accept a PR for that?

Yep, sure!