Laravel Chrome browser provides simple browser interaction for automation.

Fund package maintenance!
madeitbelgium

1.6.1 2022-10-24 19:09 UTC

This package is auto-updated.

Last update: 2024-12-25 00:30:40 UTC


README

Build Status Coverage Status Latest Stable Version Latest Unstable Version Total Downloads License

With this Laravel package you interact with a Chrome headless webbrowser. This package is based on Laravel Dusk.

Installation

Require this package in your composer.json and update composer.

"madeitbelgium/chrome": "^1.3"

Documentation

Usage

$chromebrowser = new \MadeITBelgium\Chrome\ChromeBrowser();
$chromebrowser->setUp($url, false); //False = desktop
$chromebrowser->startChromeDriver();

$chromebrowser->browse(function (Browser $browser) {
    $browser->visit('https://www.example.com');
    $browser->screenshot('screenshot');
});


$chromebrowser->closeAll();
$chromebrowser->stopChromeDriver();

Override default settings

To override the default settings you can create your own class that extends the MadeITBelgium\Chrome\ChromeBrowser class. In your own class you need to override the driver function.

<?php

namespace App;

use MadeITBelgium\Chrome\ChromeBrowser as ChromeBrowserParent;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use MadeITBelgium\Chrome\Chrome\SupportsChrome;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;

class ChromeBrowser extends ChromeBrowserParent
{
    public function driver()
    {
        $driverLocation = 'http://localhost:9515';
        //$driverLocation = 'http://localhost:4444/wd/hub';
        
        $args = [
            '--disable-gpu',
            '--headless',
            '--no-sandbox',
        ];
        
        $options = (new ChromeOptions())->addArguments($args);
         $ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) CriOS/67.0.3396.87 Mobile/15E216 Safari/604.1';
        $options->setExperimentalOption('mobileEmulation', ['userAgent' => $ua]);
        $capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);
        $capabilities->setCapability('proxy', [
            'proxyType' => 'manual',
            'httpProxy' => 'http://proxyserver:3128',
            'sslProxy' => 'http://proxyserver:3128',
        ]);
        
        return RemoteWebDriver::create($driverLocation, $capabilities);
    }
}
$chromebrowser = new \App\ChromeBrowser();
$chromebrowser->setUp($url);
$chromebrowser->startChromeDriver();

$chromebrowser->browse(function (Browser $browser) {
    $browser->visit('https://www.example.com');
});


$chromebrowser->closeAll();
$chromebrowser->stopChromeDriver();

Change location

$chromebrowser = new \MadeITBelgium\Chrome\ChromeBrowser();
$chromebrowser->setUp($url, false); //False = desktop
$chromebrowser->startChromeDriver();

$chromebrowser->browse(function (Browser $browser) {
    $devTools = new \Facebook\WebDriver\Chrome\ChromeDevToolsDriver($browser->driver);
    $coordinates = [
        'latitude' => 39.913818,
        'longitude' => 116.363625,
        'accuracy' => 1,
    ];
    $devTools->execute('Emulation.setGeolocationOverride', $coordinates);
    $browser->visit('https://www.example.com');
    $browser->screenshot('screenshot');
});


$chromebrowser->closeAll();
$chromebrowser->stopChromeDriver();

The complete documentation can be found at: http://www.madeit.be/

Support

Support github or mail: tjebbe.lievens@madeit.be

Contributing

Please try to follow the psr-2 coding style guide. http://www.php-fig.org/psr/psr-2/

License

This package is licensed under LGPL. You are free to use it in personal and commercial projects. The code can be forked and modified, but the original copyright author should always be included!