Skip to content

Commit 22c2558

Browse files
committed
SafariDriver supports opening new windows and tabs
This uses an experimental API provided by Apple, and so is marked as a `@Beta` feature.
1 parent 1f795e0 commit 22c2558

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

java/client/src/org/openqa/selenium/safari/SafariDriver.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
package org.openqa.selenium.safari;
1919

20+
import com.google.common.collect.ImmutableMap;
21+
22+
import org.openqa.selenium.Beta;
2023
import org.openqa.selenium.Capabilities;
2124
import org.openqa.selenium.WebDriverException;
2225
import org.openqa.selenium.remote.FileDetector;
2326
import org.openqa.selenium.remote.RemoteWebDriver;
24-
import org.openqa.selenium.remote.service.DriverCommandExecutor;
27+
import org.openqa.selenium.remote.Response;
2528

2629
/**
2730
* A WebDriver implementation that controls Safari using a browser extension
@@ -75,7 +78,7 @@ public SafariDriver(SafariDriverService safariService) {
7578
* @param safariOptions safari specific options / capabilities for the driver
7679
*/
7780
public SafariDriver(SafariDriverService safariServer, SafariOptions safariOptions) {
78-
super(new DriverCommandExecutor(safariServer), safariOptions);
81+
super(new SafariDriverCommandExecutor(safariServer), safariOptions);
7982
}
8083

8184
@Override
@@ -84,4 +87,24 @@ public void setFileDetector(FileDetector detector) {
8487
"Setting the file detector only works on remote webdriver instances obtained " +
8588
"via RemoteWebDriver");
8689
}
90+
91+
/**
92+
* Open either a new tab or window, depending on what is requested, and return the window handle
93+
* without switching to it.
94+
*
95+
* @return The handle of the new window.
96+
*/
97+
@Beta
98+
public String newWindow(WindowType type) {
99+
Response response = execute(
100+
"SAFARI_NEW_WINDOW",
101+
ImmutableMap.of("newTab", type == WindowType.TAB));
102+
103+
return (String) response.getValue();
104+
}
105+
106+
public enum WindowType {
107+
TAB,
108+
WINDOW,
109+
}
87110
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.safari;
19+
20+
import static org.openqa.selenium.remote.http.HttpMethod.POST;
21+
22+
import com.google.common.collect.ImmutableMap;
23+
24+
import org.openqa.selenium.remote.CommandInfo;
25+
import org.openqa.selenium.remote.service.DriverCommandExecutor;
26+
import org.openqa.selenium.remote.service.DriverService;
27+
28+
import java.util.Map;
29+
30+
class SafariDriverCommandExecutor extends DriverCommandExecutor {
31+
32+
private static final Map<String, CommandInfo> SAFARI_COMMANDS = ImmutableMap.of(
33+
"SAFARI_NEW_WINDOW", new CommandInfo("/session/:sessionId/apple/window/new", POST));
34+
35+
SafariDriverCommandExecutor(DriverService service) {
36+
super(service, SAFARI_COMMANDS);
37+
}
38+
}

0 commit comments

Comments
 (0)