Handling alerts in Selenium in php

Below example shows how to handle alerts in Selenium in PHP.
 
<?php
class MyTest extends PHPUnit_Framework_TestCase {

protected $driver;

public function setUp()
{
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->driver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);
$this->driver->manage()->window()->maximize();
}

public function testAlerts()
{
$this->driver->get(“https://www.softpost.org/selenium-test-page”);

try{
//To accept alert, you can use below line of code
$this->driver->switchTo()->alert()->accept();

//To dismiss alert, you can use below line of code
$this->driver->switchTo()->alert()->dismiss();

}catch(Exception $ex){
echo (“Exception occured while trying to accept the alert” . $ex->getMessage());
}

}

public function tearDown()
{
//Quit the driver
$this->driver->quit();
}
}
?>

Web development and Automation testing

solutions delivered!!