Skip to content

Instantly share code, notes, and snippets.

@MogulChris
Created April 10, 2019 22:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MogulChris/6f2facf768ac3f280e9ad765e531dd55 to your computer and use it in GitHub Desktop.
Save MogulChris/6f2facf768ac3f280e9ad765e531dd55 to your computer and use it in GitHub Desktop.
Automation and taking full-screen screenshots using Laravel Dusk and Chrome webdriver
<?php
/**
* This script is using Laravel Dusk to take screenshots of a URL, but not in the context of a regular Dusk test.
* My Laravel application is taking screenshots of *other* sites, but you could adapt this to a Dusk test taking screenshots of your Laravel site.
* 1. Install Dusk https://laravel.com/docs/5.8/dusk#installation
* 2. Make sure it works with regular Dusk tests first.
* 3. Use the code below for a stand-alone Chrome webdriver instance to do automation things.
*/
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;
class MyScreenshot {
/**
* Screenshot a URL
* @param string $url The URL to screenshot
* @return string|boolean Screenshot path or false
*/
public function screenshot($url){
//Make a chrome browser
$process = (new ChromeProcess)->toProcess();
$process->start();
$options = (new ChromeOptions)->addArguments(['--disable-gpu', '--headless']);
$capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = retry(5, function () use($capabilities) {
return RemoteWebDriver::create('http://localhost:9515', $capabilities);
}, 50);
$browser = new Browser($driver);
//Start by setting your full desired width and an arbitrary height
$size = new WebDriverDimension(1920, 1080);
$browser->driver->manage()->window()->setSize($size);
$browser->visit($url);
//Resize to full height for a complete screenshot
$body = $browser->driver->findElement(WebDriverBy::tagName('body'));
if (!empty($body)) {
$currentSize = $body->getSize();
//optional: scroll to bottom and back up, to trigger image lazy loading
$browser->driver->executeScript('window.scrollTo(0, ' . $currentSize->getHeight() . ');');
$browser->pause(1000); //wait a sec
$browser->driver->executeScript('window.scrollTo(0, 0);'); //scroll back to top of the page
//set window to full height
$size = new WebDriverDimension(1920, $currentSize->getHeight()); //make browser full height for complete screenshot
$browser->driver->manage()->window()->setSize($size);
}
$browser->pause(3000); //wait for 3s to give everything time to finish loading - probably better to actually check
$image = $browser->driver->TakeScreenshot(); //$image is now the image data in PNG format
//save the image somewhere useful
$filename = date('Y-m-d-H-i-s') . '.png'; //timestamp as a filename
file_put_contents('/path/to/image/storage/' . $filename, $image);
return file_exists('/path/to/image/storage/' . $filename) ? '/path/to/image/storage/' . $filename : false;
}//screenshot()
}//class
@mpemburn
Copy link

This is completely awesome! I am incorporating it into an app to archive old websites for the library of the university I work for. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment