38
loading...
This website collects cookies to deliver better user experience
/* $wHandle is the Window Handle (or ID) of the Window to which the switch has to be performed */
$this->webDriver->switchTo()->window($wHandle);
/* Switch can also be done by obtaining the number of windows using getWindowHandles or getWindowHandle */
$this->webDriver->switchTo()->window($HandleCount[win-number]);
$wHandle = $this->webDriver->getWindowHandle();
/* For a web page that opens a new tab, getWindowHandles returns an array of comprising of 2 window handles (i.e. handle of parent and child window) */
$HandleCount = $this->webDriver->getWindowHandles();
/* Returns the size of the Window Handle array. In the current example, it will be 2 */
echo ("\n Total number of window handles are " . sizeof($HandleCount));
/* Print the Window Handle of the Parent Window */
echo ("\n Window 0: " . $HandleCount[0]);
/* Print the Window Handle of the Child Window */
echo ("\n Window 0: " . $HandleCount[1]);
{
"require":{
"php":">=7.1",
"phpunit/phpunit":"^9",
"phpunit/phpunit-selenium": "*",
"php-webdriver/webdriver":"1.8.0",
"symfony/symfony":"4.4",
"brianium/paratest": "dev-master"
}
}
<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
$GLOBALS['LT_USERNAME'] = "user-name";
# accessKey: AccessKey can be generated from automation dashboard or profile section
$GLOBALS['LT_APPKEY'] = "access-key";
class WindowSwitchTest extends TestCase
{
protected $webDriver;
public function build_browser_capabilities(){
/* $capabilities = DesiredCapabilities::chrome(); */
$capabilities = array(
"build" => "[PHP] Window Switching with Chrome on Windows 10",
"name" => "[PHP] Window Switching with Chrome on Windows 10",
"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();
/* Download the Selenium Server 3.141.59 from
https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
*/
/* $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_SwitchToNewWindow()
{
$test_url_1 = "https://www.lambdatest.com";
$title_1 = "Most Powerful Cross Browser Testing Tool Online | LambdaTest";
$test_url_2 = "https://www.lambdatest.com/blog/";
$title_2 = "LambdaTest | A Cross Browser Testing Blog";
$this->webDriver->get($test_url_1);
$this->webDriver->manage()->window()->maximize();
$wHandle = $this->webDriver->getWindowHandle();
/* echo ("\n Primary Window Handle is " . $wHandle ); */
sleep(5);
/* Open the second window */
/* $link = "window.open('https://www.lambdatest.com/blog/', '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,width=800,height=800')"; */
$link = "window.open('". $test_url_2 ."', '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,width=1200,height=1200')";
$this->webDriver->executeScript($link);
/* $this->webDriver->manage()->window()->maximize(); */
/* The focus is now on the second window */
/* The Handle count will be two */
$HandleCount = $this->webDriver->getWindowHandles();
echo ("\n Total number of window handles are " . sizeof($HandleCount));
echo ("\n Window 0: " . $HandleCount[0]);
echo ("\n Window 1: " . $HandleCount[1]);
sleep(10);
/* Assert if the Window Count is not 2 */
$this->assertEquals(2, sizeof($HandleCount));
/* Check if the Window titles match */
$this->webDriver->switchTo()->window($HandleCount[1]);
$win_title_2 = $this->webDriver->getTitle();
echo ("\n Title of the window 1 is " . $win_title_2);
sleep(10);
$this->assertEquals($win_title_2, $title_2);
/* Close the newly opened Window and return to the old window */
$this->webDriver->close();
sleep(10);
/* Return to the window with handle = 0 */
$this->webDriver->switchTo()->window($wHandle);
/* Check if the Window titles match */
$win_title_1 = $this->webDriver->getTitle();
echo ("\n Title of the window 0 is " . $win_title_1);
$this->assertEquals($win_title_1, $title_1);
sleep(10);
}
}
?>
$GLOBALS['LT_USERNAME'] = "user-name";
# accessKey: AccessKey can be generated from automation dashboard or profile section
$GLOBALS['LT_APPKEY'] = "access-key";
$capabilities = array(
"build" => "[PHP] Window Switching with Chrome on Windows 10",
"name" => "[PHP] Window Switching with Chrome on Windows 10",
"platform" => "Windows 10",
"browserName" => "Chrome",
"version" => "85.0"
);
$url = "https://". $GLOBALS['LT_USERNAME'] .":" . $GLOBALS['LT_APPKEY'] ."@hub.lambdatest.com/wd/hub";
$capabilities = $this->build_browser_capabilities();
$this->webDriver = RemoteWebDriver::create($url, $capabilities);
public function test_SwitchToNewWindow()
{
$test_url_1 = "https://www.lambdatest.com";
$title_1 = "Most Powerful Cross Browser Testing Tool Online | LambdaTest";
...............................................
...............................................
$this->webDriver->get($test_url_1);
$wHandle = $this->webDriver->getWindowHandle();
...............................................
}
$link = "window.open('". $test_url_2 ."', '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,width=1200,height=1200')";
$this->webDriver->executeScript($link);
$HandleCount = $this->webDriver->getWindowHandles();
echo ("\n Total number of window handles are " . sizeof($HandleCount));
$this->assertEquals(2, sizeof($HandleCount));
$this->webDriver->switchTo()->window($HandleCount[1]);
$win_title_2 = $this->webDriver->getTitle();
$this->assertEquals($win_title_2, $title_2);
$this->webDriver->close();
$test_url_1 = "https://www.lambdatest.com";
$title_1 = "Most Powerful Cross Browser Testing Tool Online | LambdaTest";
...........................................
$this->webDriver->get($test_url_1);
...........................................
...........................................
$wHandle = $this->webDriver->getWindowHandle();
/* Return to the window with handle = 0 */
$this->webDriver->switchTo()->window($wHandle);
/* Check if the Window titles match */
$win_title_1 = $this->webDriver->getTitle();
$this->assertEquals($win_title_1, $title_1);
public function tearDown(): void
{
$this->webDriver->quit();
}
<?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 TabSwitchTest extends TestCase
{
protected $webDriver;
public function build_browser_capabilities(){
/* $capabilities = DesiredCapabilities::chrome(); */
$capabilities = array(
"build" => "[PHP] Tab Switching with Chrome on Windows 10",
"name" => "[PHP] Tab Switching with Chrome on Windows 10",
"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();
/* Download the Selenium Server 3.141.59 from
https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
*/
/* $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_SwitchToNewTab()
{
$test_url = "http://automationpractice.com/index.php";
$title_1 = "My Store";
$title_2 = "Selenium Framework - YouTube";
$this->webDriver->get($test_url);
$this->webDriver->manage()->window()->maximize();
sleep(5);
$HandleCount = $this->webDriver->getWindowHandles();
echo ("\n Total number of window handles are " . sizeof($HandleCount));
echo ("\n Window 0: " . $HandleCount[0]);
$win_title = $this->webDriver->getTitle();
echo ("\n Title of the window 0 is " . $win_title);
$this->assertEquals($win_title, $title_1);
/* Go to the end of the Page since we are looking to click the YouTube button */
$link = "window.scrollTo(0, document.body.scrollHeight)";
$this->webDriver->executeScript($link);
/* $browser_button = $this->webDriver->findElement(WebDriverBy::XPath("//a[.='Click Here']")); */
$browser_button = $this->webDriver->findElement(WebDriverBy::XPath("//a[contains(.,'Youtube')]"));
if($browser_button) {
$browser_button->click();
/* The Window Count is now 2 */
$HandleCount = $this->webDriver->getWindowHandles();
echo ("\n Total number of window handles are " . sizeof($HandleCount));
echo ("\n Window 0: " . $HandleCount[0]);
echo ("\n Window 1: " . $HandleCount[1]);
/* Check if the Window titles match */
$this->webDriver->switchTo()->window($HandleCount[1]);
$win_title_2 = $this->webDriver->getTitle();
echo ("\n Title of the window 1 is " . $win_title_2);
sleep(5);
$this->assertEquals($win_title_2, $title_2);
/* Close the newly opened Window and return to the old window */
$this->webDriver->close();
sleep(5);
/* Return to the window with handle = 0 */
$this->webDriver->switchTo()->window($HandleCount[0]);
}
/* Check if the Window titles match */
$win_title_1 = $this->webDriver->getTitle();
echo ("\n Title of the window 0 is " . $win_title_1);
$this->assertEquals($win_title_1, $title_1);
sleep(5);
}
}
?>
public function test_SwitchToNewTab()
{
$test_url = "http://automationpractice.com/index.php";
$title_1 = "My Store";
$title_2 = "Selenium Framework - YouTube";
......................................
......................................
$this->webDriver->get($test_url);
$this->webDriver->manage()->window()->maximize();
......................................
......................................
$HandleCount = $this->webDriver->getWindowHandles();
$link = "window.scrollTo(0, document.body.scrollHeight)";
$this->webDriver->executeScript($link);
$browser_button = $this->webDriver->findElement(WebDriverBy::XPath("//a[contains(.,'Youtube')]"));
$browser_button->click();
$HandleCount = $this->webDriver->getWindowHandles();
echo ("\n Total number of window handles are " . sizeof($HandleCount));
echo ("\n Window 0: " . $HandleCount[0]);
echo ("\n Window 1: " . $HandleCount[1]);
$this->webDriver->switchTo()->window($HandleCount[1]);
$win_title_2 = $this->webDriver->getTitle();
$this->assertEquals($win_title_2, $title_2);
$this->webDriver->close();
$this->webDriver->switchTo()->window($HandleCount[0]);
$win_title_1 = $this->webDriver->getTitle();
$this->assertEquals($win_title_1, $title_1);
http://www.popuptest.com:80
<?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 PopUpTest extends TestCase
{
protected $webDriver;
public function build_browser_capabilities(){
/* $capabilities = DesiredCapabilities::chrome(); */
$capabilities = array(
"build" => "[PHP] Pop Up Testing with Chrome on Windows 10",
"name" => "[PHP] Pop Up Testing with Chrome on Windows 10",
"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();
/* Download the Selenium Server 3.141.59 from
https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
*/
/* $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_SwitchToNewWindow()
{
$test_url = "http://www.popuptest.com/popuptest1.html";
$title = "PopupTest 1 - test your popup killer software";
$this->webDriver->get($test_url);
sleep(5);
/* This will open-up main window and six pop-up windows */
/* Once the page is loaded, the total window count will be 7 */
$HandleCount = $this->webDriver->getWindowHandles();
/* This is the ID of the parent window */
$mainHandle = $HandleCount[0];
echo ("\n Total number of window handles are " . sizeof($HandleCount));
echo ("\n Window 0: " . $HandleCount[0]);
$win_title = $this->webDriver->getTitle();
echo ("\n Title of the parent window is " . $win_title);
foreach( $HandleCount as $handle)
{
if($handle != $mainHandle)
{
echo ("\n Window handle of the current window: " . $handle);
$this->webDriver->switchTo()->window($handle);
echo ("\n Title of the current window: " . $this->webDriver->getTitle());
/* Close the pop-up window and return to the old window */
$this->webDriver->close();
sleep(2);
}
}
$this->webDriver->switchTo()->window($mainHandle);
$this->webDriver->manage()->window()->maximize();
sleep(5);
$curr_window_title = $this->webDriver->getTitle();
echo ("\n\n Title of the only left window: " . $curr_window_title);
$this->assertEquals($curr_window_title, $title);
sleep(5);
}
}
?>
$test_url = "http://www.popuptest.com/popuptest1.html";
$title = "PopupTest 1 - test your popup killer software";
$this->webDriver->get($test_url);
/* This will open-up main window and six pop-up windows */
/* Once the page is loaded, the total window count will be 7 */
$HandleCount = $this->webDriver->getWindowHandles();
/* This is the ID of the parent window */
$mainHandle = $HandleCount[0];
echo ("\n Total number of window handles are " . sizeof($HandleCount));
foreach($HandleCount as $handle)
{
if($handle != $mainHandle)
{
echo ("\n Window handle of the current window: " . $handle);
$this->webDriver->switchTo()->window($handle);
$this->webDriver->close();
sleep(2);
}
}
$this->webDriver->switchTo()->window($mainHandle);
$this->webDriver->manage()->window()->maximize();
$curr_window_title = $this->webDriver->getTitle();
$this->assertEquals($curr_window_title, $title);