Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

stefanzweifel/screeenly

520 stars
6 code files
View stefanzweifel/screeenly on GitHub

modules/Screeenly/Contracts/CanCaptureScreenshot.php

Open in GitHub
use Screeenly\Entities\Url;
 
interface CanCaptureScreenshot
{
public function capture(Url $url, $storageUrl);
}

modules/Screeenly/Services/InMemoryBrowser.php

Open in GitHub
use Screeenly\Contracts\CanCaptureScreenshot;
use Screeenly\Entities\Screenshot;
use Screeenly\Entities\Url;
 
class InMemoryBrowser extends Browser implements CanCaptureScreenshot
{
public function capture(Url $url, $storageUrl)
{
return new Screenshot(storage_path('testing/test-screenshot.jpg'));
}
}

modules/Screeenly/Services/ChromeBrowser.php

Open in GitHub
use Illuminate\Support\Facades\Storage;
use Screeenly\Contracts\CanCaptureScreenshot;
use Screeenly\Entities\Screenshot;
use Screeenly\Entities\Url;
use Spatie\Browsershot\Browsershot;
 
class ChromeBrowser extends Browser implements CanCaptureScreenshot
{
public function capture(Url $url, $filename)
{
$browser = Browsershot::url($url->getUrl())
->ignoreHttpsErrors()
->windowSize($this->width, is_null($this->height) ? 768 : $this->height)
->timeout(30)
->setDelay($this->delay * 1000)
->userAgent('screeenly-bot 2.0');
Storage::disk(config('screeenly.filesystem_disk'))->put($filename, $browser->screenshot());
 
$path = Storage::disk(config('screeenly.filesystem_disk'))->path($filename);
 
return new Screenshot($path);
}
}

modules/Screeenly/Providers/ScreeenlyServiceProvider.php

Open in GitHub
// We specify in the Service Provider, that the class of CanCaptureScreenshot interface should be ChromeBrowser class
 
use Screeenly\Contracts\CanCaptureScreenshot;
use Screeenly\Services\ChromeBrowser;
 
class ScreeenlyServiceProvider extends ServiceProvider
{
public function boot()
{
// ...
$this->app->bind(CanCaptureScreenshot::class, ChromeBrowser::class);
}
}

modules/Screeenly/Services/CaptureService.php

Open in GitHub
use Screeenly\Contracts\CanCaptureScreenshot;
use Screeenly\Entities\Url;
 
class CaptureService
{
protected $browser;
 
// By this point, Laravel knows that CanCaptureScreenshot should be resolved as ChromeBrowser
public function __construct(CanCaptureScreenshot $browser)
{
$this->browser = $browser;
}
 
public function capture()
{
$filename = uniqid().'_'.Str::random(30) . ".png";
 
return $this->browser->capture(
$this->url,
$filename
);
}
}

tests/modules/Screeenly/integration/api/v2/ApiV2ScreenshotTest.php

Open in GitHub
use Screeenly\Contracts\CanCaptureScreenshot;
use Screeenly\Services\InMemoryBrowser;
 
class ApiV2ScreenshotTest extends BrowserKitTestCase
{
public function it_returns_base64_representation_of_screenshot()
{
// But in the automated test we can easily resolve the same interface with a DIFFERENT class - InMemoryBrowser
$this->app->bind(CanCaptureScreenshot::class, function ($app) {
return new InMemoryBrowser('http://foo.bar', '/path/to/storage');
});
 
$this->json('POST', '/api/v2/screenshot', [
'key' => $apiKey->key,
'url' => 'http://google.com',
])
->seeJsonStructure([
'data' => [
'path', 'base64',
],
]);
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.