|
| 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 | +} |
0 commit comments