From e5b4de4e8dfc6c2ea016653fca2ed247b42ef0e2 Mon Sep 17 00:00:00 2001 From: Thomas Off Date: Thu, 22 Feb 2018 13:28:12 +0100 Subject: [PATCH] Add support for custom binary/browser script to make adjustments easier/possible. (#162) --- README.md | 9 +++++++++ src/Browsershot.php | 10 +++++++++- tests/BrowsershotTest.php | 14 +++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 268e9ffb..4f5c5290 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,15 @@ Browsershot::html('Foo') ->setNodeModulePath("/path/to/my/project/node_modules/") ``` +### Custom binary path + +If you want to use an alternative script source you can set it using the `setBinPath` method. + +```php +Browsershot::html('Foo') + ->setBinPath("/path/to/my/project/my_script.js") +``` + ### Custom chrome/chromium executable path If you want to use an alternative chrome or chromium executable from what is installed by puppeteer you can set it using the `setChromePath` method. diff --git a/src/Browsershot.php b/src/Browsershot.php index 474e981d..3f04f296 100644 --- a/src/Browsershot.php +++ b/src/Browsershot.php @@ -16,6 +16,7 @@ class Browsershot protected $npmBinary = null; protected $nodeModulePath = null; protected $includePath = '$PATH:/usr/local/bin'; + protected $binPath = null; protected $html = ''; protected $noSandbox = false; protected $proxyServer = ''; @@ -79,6 +80,13 @@ public function setIncludePath(string $includePath) return $this; } + public function setBinPath(string $binPath) + { + $this->binPath = $binPath; + + return $this; + } + public function setNodeModulePath(string $nodeModulePath) { $this->nodeModulePath = $nodeModulePath; @@ -471,7 +479,7 @@ protected function callBrowser(array $command) $setNodePathCommand = $this->getNodePathCommand($nodeBinary); - $binPath = __DIR__.'/../bin/browser.js'; + $binPath = $this->binPath ?: __DIR__.'/../bin/browser.js'; $fullCommand = $setIncludePathCommand.' ' diff --git a/tests/BrowsershotTest.php b/tests/BrowsershotTest.php index e57a6fd5..fa5ddd46 100644 --- a/tests/BrowsershotTest.php +++ b/tests/BrowsershotTest.php @@ -461,7 +461,7 @@ public function it_can_set_another_node_binary() $targetPath = __DIR__.'/temp/testScreenshot.png'; Browsershot::html('Foo') - ->setNodeBinary('non-existant/bin/wich/causes/an/exception') + ->setBinPath(__DIR__.'/../browser.js') ->save($targetPath); } @@ -477,6 +477,18 @@ public function it_can_set_another_chrome_executable_path() ->save($targetPath); } + /** @test */ + public function it_can_set_another_bin_path() + { + $this->expectException(ProcessFailedException::class); + + $targetPath = __DIR__.'/temp/testScreenshot.png'; + + Browsershot::html('Foo') + ->setChromePath('non-existant/bin/wich/causes/an/exception') + ->save($targetPath); + } + /** @test */ public function it_can_set_the_include_path_and_still_works() {