Executing JS in Selenium in php

Below example shows how to execute the JavaScript 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 testJS()
{
$this->driver->get(“https://www.softpost.org/selenium-test-page”);

$src = $this->driver->executeScript(‘return document.body.innerHTML;’);

echo (“Source of page -> ” . $src);

//click on the radio button using java script
//Notice how we have passed the argument to the java script code.
$elements = $this->driver->findElements(WebDriverBy::xpath(“//input[@value=’female’]”));
$this->driver->executeScript(‘arguments[0].click();’,$elements);

sleep(2);

}

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

Web development and Automation testing

solutions delivered!!