Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 98686d9

Browse files
committed
Add tests for switchTo()->window() of RemoteTargetLocator
1 parent 57f871e commit 98686d9

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Facebook\WebDriver;
17+
18+
/**
19+
* @covers \Facebook\WebDriver\Remote\RemoteTargetLocator
20+
*/
21+
class RemoteTargetLocatorTest extends WebDriverTestCase
22+
{
23+
public function testShouldSwitchToWindow()
24+
{
25+
$this->driver->get($this->getTestPageUrl('open_new_window.html'));
26+
$originalWindowHandle = $this->driver->getWindowHandle();
27+
$windowHandlesBefore = $this->driver->getWindowHandles();
28+
29+
$this->driver->findElement(WebDriverBy::cssSelector('a#open-new-window'))
30+
->click();
31+
32+
$this->driver->wait()->until(
33+
WebDriverExpectedCondition::numberOfWindowsToBe(2)
34+
);
35+
36+
// At first the window should not be switched
37+
$this->assertContains('open_new_window.html', $this->driver->getCurrentURL());
38+
$this->assertSame($originalWindowHandle, $this->driver->getWindowHandle());
39+
40+
/**
41+
* @see https://w3c.github.io/webdriver/#get-window-handles
42+
* > "The order in which the window handles are returned is arbitrary."
43+
* Thus we must first find out which window handle is the new one
44+
*/
45+
$windowHandlesAfter = $this->driver->getWindowHandles();
46+
$newWindowHandle = array_diff($windowHandlesAfter, $windowHandlesBefore);
47+
$newWindowHandle = reset($newWindowHandle);
48+
49+
$this->driver->switchTo()->window($newWindowHandle);
50+
51+
// After switchTo() is called, the active window should be changed
52+
$this->assertContains('index.html', $this->driver->getCurrentURL());
53+
$this->assertNotSame($originalWindowHandle, $this->driver->getWindowHandle());
54+
}
55+
}

tests/functional/WebDriverAlertTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
/**
2323
* @covers \Facebook\WebDriver\WebDriverAlert
24+
* @covers \Facebook\WebDriver\Remote\RemoteTargetLocator
2425
*/
2526
class WebDriverAlertTest extends WebDriverTestCase
2627
{

tests/functional/web/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ <h1 id='welcome'>Welcome to the facebook/php-webdriver testing page.</h1>
1111
|
1212
<a href="upload.html" id="a-form-upload">Form with file upload</a>
1313
|
14+
<a href="form_checkbox_radio.html" id="a-form-checkbox-radio">Form with checkboxes and radios</a>
15+
|
1416
<a href="open_new_window.html" id="a-new-window">New window opener test page</a>
1517
|
1618
<a href="delayed_element.html" id="a-delayed-element">Delayed render</a>

tests/functional/web/open_new_window.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title>php-webdriver test page</title>
66
</head>
77
<body>
8-
<a href="#" target="_blank">open new window</a>
8+
<a href="index.html" target="_blank" id="open-new-window">open new window</a>
99
</body>
1010
</html>

0 commit comments

Comments
 (0)