39
loading...
This website collects cookies to deliver better user experience
$js_command = "JS command to be executed";
/* For example - To get the Page Title using JavaScript in Selenium */
/* $js_command = "return document.domain;"; */
$return_var = $driver->executeScript($js_command);
$script_link = " JS command to be executed";
/* For example – Performing sleep in the browser under test */
/* $script_link = "window.setTimeout(arguments[arguments.length - 1], " . $asyncwaittime . ");"; */
$driver->executeAsyncScript($script_link);
<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
$GLOBALS['LT_USERNAME'] = "user-name";
# accessKey: AccessKey can be generated from automation dashboard or profile section
$GLOBALS['LT_APPKEY'] = "access-key";
class JS_Capture_DataTest extends TestCase
{
protected $webDriver;
public function build_browser_capabilities(){
/* $capabilities = DesiredCapabilities::chrome(); */
$capabilities = array(
"build" => "[PHP-1] Demonstration of executeScript method using Selenium PHP",
"name" => "[PHP-1] Demonstration of executeScript method using Selenium PHP",
"platform" => "Windows 10",
"browserName" => "Chrome",
"version" => "85.0"
);
return $capabilities;
}
public function setUp(): void
{
$url = "https://". $GLOBALS['LT_USERNAME'] .":" . $GLOBALS['LT_APPKEY'] ."@hub.lambdatest.com/wd/hub";
$capabilities = $this->build_browser_capabilities();
/* $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities); */
$this->webDriver = RemoteWebDriver::create($url, $capabilities);
}
public function tearDown(): void
{
$this->webDriver->quit();
}
/*
* @test
*/
public function test_Wait_Sleep()
{
$test_url = "https://www.lambdatest.com/blog/";
$title = "LambdaTest | A Cross Browser Testing Blog";
$driver = $this->webDriver;
$driver->get($test_url);
$driver->manage()->window()->maximize();
$this->assertEquals($title, $driver->getTitle());
$js_domain_name = "return document.domain;";
$domain_name = $driver->executeScript($js_domain_name);
echo ("\nDomain name is " .$domain_name);
/* $js_command = "return document.URL;"; */
$js_command = "return window.location.href;";
$domain_url = $driver->executeScript($js_command);
echo ("\nURL is " .$domain_url);
$doc_title_command = "return document.title;";
$window_title = $driver->executeScript($doc_title_command);
echo ("\nWindow Title is " .$window_title);
}
}
?>
$capabilities = array(
"build" => "[PHP-1] Demonstration of executeScript method using Selenium PHP",
"name" => "[PHP-1] Demonstration of executeScript method using Selenium PHP",
"platform" => "Windows 10",
"browserName" => "Chrome",
"version" => "85.0"
);
$this->webDriver = RemoteWebDriver::create($url, $capabilities);
$js_domain_name = "return document.domain;";
$domain_name = $driver->executeScript($js_domain_name);
$js_command = "return window.location.href;";
$domain_url = $driver->executeScript($js_command);
$doc_title_command = "return document.title;";
$window_title = $driver->executeScript($doc_title_command);
vendor\bin\phpunit tests\JS_Capture_DataTest.php
<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverKeys;
use Facebook\WebDriver\WebDriverBy;
$GLOBALS['LT_USERNAME'] = "user-name";
# accessKey: AccessKey can be generated from automation dashboard or profile section
$GLOBALS['LT_APPKEY'] = "access-key";
class JS_ExecuteScriptTest extends TestCase
{
protected $webDriver;
public function build_browser_capabilities(){
/* $capabilities = DesiredCapabilities::chrome(); */
$capabilities = array(
"build" => "[PHP-2] Use executeScript instead of traditional Selenium WebDriver APIs",
"name" => "[PHP-2] Use executeScript instead of traditional Selenium WebDriver APIs",
"platform" => "Windows 10",
"browserName" => "Chrome",
"version" => "85.0"
);
return $capabilities;
}
public function setUp(): void
{
$url = "https://". $GLOBALS['LT_USERNAME'] .":" . $GLOBALS['LT_APPKEY'] ."@hub.lambdatest.com/wd/hub";
$capabilities = $this->build_browser_capabilities();
/* $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities); */
$this->webDriver = RemoteWebDriver::create($url, $capabilities);
}
public function tearDown(): void
{
$this->webDriver->quit();
}
/*
* @test
*/
public function test_Wait_Sleep()
{
$test_url = "https://lambdatest.github.io/sample-todo-app/";
$title = "Sample page - lambdatest.com";
$itemName = 'Yey, Lets add it to list';
$driver = $this->webDriver;
$driver->get($test_url);
$driver->manage()->window()->maximize();
$elementli1 = $driver->findElements(WebDriverBy::name("li1"));
$driver->executeScript('arguments[0].click();',$elementli1);
$elementli2 = $driver->findElements(WebDriverBy::name("li2"));
$driver->executeScript('arguments[0].click();',$elementli2);
$elementtodotext = $driver->findElement(WebDriverBy::id("sampletodotext"));
$elementtodotext->sendKeys($itemName);
/* This did not work, hence, we used sendKeys method instead of the executeScript method */
/*
$elementtodotext = $driver->findElements(WebDriverBy::id("sampletodotext"));
$new_item_link = "arguments[0].value='" .$itemName. "';";
$driver->executeScript($new_item_link,$elementtodotext);
*/
sleep(2);
$addbutton = $driver->findElements(WebDriverBy::id("addbutton"));
$driver->executeScript('arguments[0].click();',$addbutton);
$driver->wait(10, 500)->until(function($driver) {
$elements = $driver->findElements(WebDriverBy::cssSelector("[class='list-unstyled'] li:nth-child(6) span"));
echo "\n New entry count " . count($elements);
$this->assertEquals(1, count($elements));
return count($elements) > 0;
}
);
}
}
?>
$elementli1 = $driver->findElements(WebDriverBy::name("li1"));
$driver->executeScript('arguments[0].click();',$elementli1);
$elementli2 = $driver->findElements(WebDriverBy::name("li2"));
$driver->executeScript('arguments[0].click();',$elementli2);
$elementtodotext = $driver->findElement(WebDriverBy::id("sampletodotext"));
$elementtodotext->sendKeys($itemName);
$elementtodotext = $driver->findElements(WebDriverBy::id("sampletodotext"));
$new_item_link = "arguments[0].value='" .$itemName. "';";
$driver->executeScript($new_item_link,$elementtodotext);
$addbutton = $driver->findElements(WebDriverBy::id("addbutton"));
$driver->executeScript('arguments[0].click();',$addbutton);
$driver->wait(10, 500)->until(function($driver) {
$elements = $driver->findElements(WebDriverBy::cssSelector("[class='list-unstyled'] li:nth-child(6) span"));
echo "\n New entry count " . count($elements);
$this->assertEquals(1, count($elements));
return count($elements) > 0;
}
);
vendor\bin\phpunit tests\JS_ExecuteScriptTest.php
<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
$GLOBALS['LT_USERNAME'] = "user-name";
# accessKey: AccessKey can be generated from automation dashboard or profile section
$GLOBALS['LT_APPKEY'] = "access-key";
class JS_AsyncExecuteScriptTest extends TestCase
{
protected $webDriver;
public function build_browser_capabilities(){
/* $capabilities = DesiredCapabilities::chrome(); */
$capabilities = array(
"build" => "[PHP-3] Demonstration of executeAsyncScript method using Selenium PHP",
"name" => "[PHP-3] Demonstration of executeAsyncScript method using Selenium PHP",
"platform" => "Windows 10",
"browserName" => "Chrome",
"version" => "85.0"
);
return $capabilities;
}
public function setUp(): void
{
$url = "https://". $GLOBALS['LT_USERNAME'] .":" . $GLOBALS['LT_APPKEY'] ."@hub.lambdatest.com/wd/hub";
$capabilities = $this->build_browser_capabilities();
/* $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities); */
$this->webDriver = RemoteWebDriver::create($url, $capabilities);
}
public function tearDown(): void
{
$this->webDriver->quit();
}
/*
* @test
*/
public function test_Wait_Sleep()
{
/* Set the script wait time to 5 seconds */
$asyncwaittime = 5000;
$test_url = "https://lambdatest.github.io/sample-todo-app/";
$title = "Sample page - lambdatest.com";
$itemName = 'Yey, Lets add it to list';
$driver = $this->webDriver;
$driver->get($test_url);
$driver->manage()->window()->maximize();
$this->assertEquals($title, $driver->getTitle());
$timeouts = $driver->manage()->timeouts();
$timeouts->setScriptTimeout(10);
$elementli1 = $driver->findElements(WebDriverBy::name("li1"));
$driver->executeScript('arguments[0].click();',$elementli1);
$elementli2 = $driver->findElements(WebDriverBy::name("li2"));
$driver->executeScript('arguments[0].click();',$elementli2);
$new_element = $this->webDriver->findElement(WebDriverBy::id("sampletodotext"));
$new_element->sendKeys($itemName);
$addbutton = $driver->findElements(WebDriverBy::id("addbutton"));
$driver->executeScript('arguments[0].click();',$addbutton);
/* Log the start time */
$start_time = microtime(true);
$script_link = "window.setTimeout(arguments[arguments.length - 1], " . $asyncwaittime . ");";
$driver->executeAsyncScript($script_link);
/* Log the end time */
$end_time = microtime(true);
$exec_time = $end_time - $start_time;
echo "\nExecution time = " . $exec_time;
echo("\n");
}
}
?>
/* Log the start time */
$start_time = microtime(true);
$script_link = "window.setTimeout(arguments[arguments.length - 1], " . $asyncwaittime . ");";
$driver->executeAsyncScript($script_link);
vendor\bin\phpunit tests\ JS_AsyncExecuteScriptTest.php