Skip to content

Instantly share code, notes, and snippets.

@andrewnicols
Created February 3, 2020 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewnicols/54a7eaa661ad790cf70fbd0e7167b02d to your computer and use it in GitHub Desktop.
Save andrewnicols/54a7eaa661ad790cf70fbd0e7167b02d to your computer and use it in GitHub Desktop.
<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
// start Chrome with 5 second timeout
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$options = new ChromeOptions();
$options->addArguments(array(
"--headless", "window-size=1024,768", "--no-sandbox"
));
$prefs = array(
'permissions.default.stylesheet' => 2,
'permissions.default.image' => 2
);
$options->setExperimentalOption('prefs', $prefs);
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$driver->get('http://localhost/test/');
function isAlertPresent($driver){
try{
$driver->wait(10)->until(WebDriverExpectedCondition::alertIsPresent());
$driver->switchTo()->alert()->accept();
return true;
} catch (Exception $ex) {
return false;
}
}
$alertFound = isAlertPresent($driver);
// close the browser
$driver->quit();
var_dump($alertFound);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment