Skip to content

Commit 4e1cf8f

Browse files
committed
add support for apple's safaridriver to java #2475
defaults to using the driver if on macOS Sierra
1 parent 0a4b995 commit 4e1cf8f

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import org.openqa.selenium.Capabilities;
2121
import org.openqa.selenium.OutputType;
2222
import org.openqa.selenium.WebDriverException;
23+
import org.openqa.selenium.remote.CommandExecutor;
2324
import org.openqa.selenium.remote.DesiredCapabilities;
2425
import org.openqa.selenium.remote.DriverCommand;
2526
import org.openqa.selenium.remote.FileDetector;
2627
import org.openqa.selenium.remote.RemoteWebDriver;
28+
import org.openqa.selenium.remote.service.DriverCommandExecutor;
2729

2830
import java.io.IOException;
2931

@@ -35,6 +37,20 @@
3537
*/
3638
public class SafariDriver extends RemoteWebDriver {
3739

40+
/**
41+
* Capability to force usage of the deprecated SafariDriver extension while running
42+
* on macOS Sierra.
43+
*
44+
* <pre>
45+
* DesiredCapabilities safariCap = DesiredCapabilities.Safari();
46+
* safariCap.setCapability(SafariDriver.USE_LEGACY_DRIVER_CAPABILITY, true);
47+
* WebDriver driver = new SafariDriver(safariCap);
48+
* </pre>
49+
*/
50+
public static final String USE_LEGACY_DRIVER_CAPABILITY = "useLegacyDriver";
51+
52+
private SafariDriverService service;
53+
3854
/**
3955
* Initializes a new SafariDriver} class with default {@link SafariOptions}.
4056
*/
@@ -59,7 +75,16 @@ public SafariDriver(Capabilities desiredCapabilities) {
5975
* @param safariOptions safari specific options / capabilities for the driver
6076
*/
6177
public SafariDriver(SafariOptions safariOptions) {
62-
super(new SafariDriverCommandExecutor(safariOptions), safariOptions.toCapabilities());
78+
super(getExecutor(safariOptions), safariOptions.toCapabilities());
79+
}
80+
81+
private static CommandExecutor getExecutor(SafariOptions options) {
82+
Object useLegacy = options.toCapabilities().getCapability(USE_LEGACY_DRIVER_CAPABILITY);
83+
SafariDriverService service = SafariDriverService.createDefaultService(options);
84+
if ((useLegacy == null || !(Boolean)useLegacy) && service != null) {
85+
return new DriverCommandExecutor(service);
86+
}
87+
return new SafariDriverCommandExecutor(options);
6388
}
6489

6590
@Override
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableMap;
22+
23+
import org.openqa.selenium.WebDriverException;
24+
import org.openqa.selenium.remote.service.DriverService;
25+
26+
import java.io.File;
27+
import java.io.IOException;
28+
29+
public class SafariDriverService extends DriverService {
30+
31+
private static final File SAFARI_DRIVER_EXECUTABLE = new File("/usr/bin/safaridriver");
32+
33+
public SafariDriverService(File executable, int port, ImmutableList<String> args,
34+
ImmutableMap<String, String> environment) throws IOException {
35+
super(executable, port, args, environment);
36+
}
37+
38+
public static SafariDriverService createDefaultService(SafariOptions options) {
39+
if (SAFARI_DRIVER_EXECUTABLE.exists()) {
40+
return new Builder().usingPort(options.getPort()).build();
41+
}
42+
return null;
43+
}
44+
45+
public static class Builder extends DriverService.Builder<
46+
SafariDriverService, SafariDriverService.Builder> {
47+
48+
protected File findDefaultExecutable() {
49+
return SAFARI_DRIVER_EXECUTABLE;
50+
}
51+
52+
protected ImmutableList<String> createArgs() {
53+
return ImmutableList.of("--port", String.valueOf(getPort()));
54+
}
55+
56+
protected SafariDriverService createDriverService(File exe, int port, ImmutableList<String> args,
57+
ImmutableMap<String, String> environment) {
58+
try {
59+
return new SafariDriverService(exe, port, args, environment);
60+
} catch (IOException e) {
61+
throw new WebDriverException(e);
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)