Skip to content

Commit e2b3b6b

Browse files
committed
Simulating switchTo().window by name
1 parent 2f80b1d commit e2b3b6b

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openqa.selenium.HasCapabilities;
3434
import org.openqa.selenium.JavascriptExecutor;
3535
import org.openqa.selenium.NoSuchFrameException;
36+
import org.openqa.selenium.NoSuchWindowException;
3637
import org.openqa.selenium.OutputType;
3738
import org.openqa.selenium.Platform;
3839
import org.openqa.selenium.Point;
@@ -955,9 +956,27 @@ public WebDriver parentFrame() {
955956
return RemoteWebDriver.this;
956957
}
957958

958-
public WebDriver window(String windowName) {
959-
execute(DriverCommand.SWITCH_TO_WINDOW, ImmutableMap.of("name", windowName, "handle", windowName));
960-
return RemoteWebDriver.this;
959+
public WebDriver window(String windowHandleOrName) {
960+
if (getW3CStandardComplianceLevel() == 0) {
961+
execute(DriverCommand.SWITCH_TO_WINDOW, ImmutableMap.of("name", windowHandleOrName));
962+
return RemoteWebDriver.this;
963+
} else {
964+
try {
965+
execute(DriverCommand.SWITCH_TO_WINDOW, ImmutableMap.of("handle", windowHandleOrName));
966+
return RemoteWebDriver.this;
967+
} catch (NoSuchWindowException nsw) {
968+
// simulate search by name
969+
String original = getWindowHandle();
970+
for (String handle : getWindowHandles()) {
971+
switchTo().window(handle);
972+
if (windowHandleOrName.equals(executeScript("return window.name"))) {
973+
return RemoteWebDriver.this; // found by name
974+
}
975+
}
976+
switchTo().window(original);
977+
throw nsw;
978+
}
979+
}
961980
}
962981

963982
public WebDriver defaultContent() {

java/client/test/org/openqa/selenium/JavascriptEnabledDriverTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public void testShouldBeAbleToGetTheLocationOfAnElement() {
238238
* running: "ImplicitWaitTest", "TemporaryFilesystemTest", "JavascriptEnabledDriverTest".
239239
* SimonStewart 2010-10-04
240240
*/
241-
@Ignore(value = {SAFARI, MARIONETTE}, reason = "Safari: issue 3693")
241+
@Ignore(value = {SAFARI}, reason = "Safari: issue 3693")
242242
@JavascriptEnabled
243243
@NeedsFreshDriver
244244
@Test

java/client/test/org/openqa/selenium/WindowSwitchingTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public void testShouldSwitchFocusToANewWindowWhenItIsOpenedAndNotStopFutureOpera
8080
}
8181

8282
@Test
83-
@Ignore(MARIONETTE)
8483
public void testShouldThrowNoSuchWindowException() {
8584
driver.get(pages.xhtmlTestPage);
8685
String current = driver.getWindowHandle();
@@ -296,7 +295,6 @@ public void testCanObtainAWindowHandle() {
296295
}
297296

298297
@Test
299-
@Ignore(MARIONETTE)
300298
public void testFailingToSwitchToAWindowLeavesTheCurrentWindowAsIs() {
301299
driver.get(pages.xhtmlTestPage);
302300
String current = driver.getWindowHandle();

0 commit comments

Comments
 (0)