Skip to content

Instantly share code, notes, and snippets.

@dan2k3k4
Last active June 8, 2017 19:53
Show Gist options
  • Save dan2k3k4/ca1e2fc7f22f0b0f851305dbb625f404 to your computer and use it in GitHub Desktop.
Save dan2k3k4/ca1e2fc7f22f0b0f851305dbb625f404 to your computer and use it in GitHub Desktop.
A simple pre-commit hook to generate a screenshot and diff it with previous screenshot using Headless Chrome and reading URL from a docker-compose.yml file. Don't forget to run chmod a+w on the pre-commit file.
#!/usr/local/bin/php
<?php
/**
* Just a quick script for Headless Chrome testing
*/
if (!is_dir('tests-visual')) {
mkdir('tests-visual', 0777);
}
// Rename older screenshot to screenshot-old
if (is_file('tests-visual/screenshot.png')) {
rename('tests-visual/screenshot.png', 'tests-visual/screenshot-old.png');
}
// Get URL to test
$pattern = '/hostname: &hostname ([^ ]+)/';
preg_match($pattern, file_get_contents('docker-compose.yml'), $matches);
if (isset($matches[1])) {
// We can now test our app
$url = trim($matches[1]);
// Generate screenshot of frontpage
exec(sprintf('/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --disable-gpu --screenshot --window-size=1920,1080 http://%s', $url));
// Move screenshot.png to tests-visual/ directory
if (is_file('screenshot.png')) {
rename('screenshot.png', 'tests-visual/screenshot.png');
}
// Lets diff our screenshot and open that too
if (is_file('tests-visual/screenshot-old.png')) {
exec('compare tests-visual/screenshot-old.png tests-visual/screenshot.png tests-visual/diff.png');
exec('open tests-visual/diff.png');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment