I'm using Laravel Dusk in the controller for users to get screenshots of the any website using mine website.
My code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Laravel\Dusk\ElementResolver;
use Exception;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;
use Facebook\WebDriver\WebDriverBy;
use Mail;
class ScreenController extends Controller {
public function take_screenshoot() {
$process = (new ChromeProcess)->toProcess();
if ($process->isStarted()) {
$process->stop();
}
$process->start();
$options = (new ChromeOptions)->addArguments([
'--disable-gpu',
'--headless',
'--window-size=1920,1080',
'--no-sandbox'
]);
$capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = retry(5, function () use ($capabilities) {
return RemoteWebDriver::create('http://localhost:9515', $capabilities, 50000, 60000);
}, 50);
$browser = new Browser($driver, new ElementResolver($driver, ''));
$browser->resize(1920, 1080);
$browser->visit('http://www.example.com')->mouseover('iframe');
$screenshoot_name = md5(time());
$browser->driver->takeScreenshot(base_path('tests/Browser/screenshots/'.$screenshoot_name.'.png'));
echo "<img src='/tests/Browser/screenshots/".$screenshoot_name.".png></img>";
$process->stop();
}
Everything works well here as expected just memory usage is by each user request (user visit) higher and higher... How to solve that problem? Why $process->stop();
do not close the browser?
UPDATE I tried:
$browser->quit();
and
$browser->driver->quit();
but I got error:
Facebook \ WebDriver \ Exception \ WebDriverCurlException Curl error thrown for http DELETE to /session/8a4f02f2d13648ccfbdead97b338e4f4 Failed to connect to localhost port 9515: Connection refused
UPDATE 2.0 This is what I got when I run
# ps -aux | less
gc_collect_cycles()
– Misagh Laghaei