Skip to content

Commit c2a4dab

Browse files
committed
[java] Allow the SafariDriver Technology Preview to be used.
1 parent fdd21f9 commit c2a4dab

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818
package org.openqa.selenium.safari;
1919

2020
import org.openqa.selenium.Capabilities;
21-
import org.openqa.selenium.OutputType;
2221
import org.openqa.selenium.WebDriverException;
2322
import org.openqa.selenium.remote.CommandExecutor;
2423
import org.openqa.selenium.remote.DesiredCapabilities;
25-
import org.openqa.selenium.remote.DriverCommand;
2624
import org.openqa.selenium.remote.FileDetector;
2725
import org.openqa.selenium.remote.RemoteWebDriver;
2826
import org.openqa.selenium.remote.service.DriverCommandExecutor;
2927

30-
import java.io.IOException;
31-
3228
/**
3329
* A WebDriver implementation that controls Safari using a browser extension
3430
* (consequently, only Safari 5.1+ is supported).

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@
2929
import java.io.File;
3030
import java.io.IOException;
3131
import java.net.MalformedURLException;
32-
import java.util.concurrent.ExecutionException;
3332

3433
public class SafariDriverService extends DriverService {
3534

3635
private static final File SAFARI_DRIVER_EXECUTABLE = new File("/usr/bin/safaridriver");
36+
private static final File TP_SAFARI_DRIVER_EXECUTABLE =
37+
new File("/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver");
3738

3839
public SafariDriverService(File executable, int port, ImmutableList<String> args,
3940
ImmutableMap<String, String> environment) throws IOException {
4041
super(executable, port, args, environment);
4142
}
4243

4344
public static SafariDriverService createDefaultService(SafariOptions options) {
44-
if (SAFARI_DRIVER_EXECUTABLE.exists()) {
45-
return new Builder().usingPort(options.getPort()).build();
45+
File exe = options.getUseTechnologyPreview() ?
46+
TP_SAFARI_DRIVER_EXECUTABLE : SAFARI_DRIVER_EXECUTABLE;
47+
if (exe.exists()) {
48+
return new Builder().usingPort(options.getPort()).usingDriverExecutable(exe).build();
4649
}
4750
return null;
4851
}
@@ -59,6 +62,15 @@ protected void waitUntilAvailable() throws MalformedURLException {
5962
public static class Builder extends DriverService.Builder<
6063
SafariDriverService, SafariDriverService.Builder> {
6164

65+
public SafariDriverService.Builder usingTechnologyPreview(boolean useTechnologyPreview) {
66+
if (useTechnologyPreview) {
67+
usingDriverExecutable(TP_SAFARI_DRIVER_EXECUTABLE);
68+
} else {
69+
usingDriverExecutable(SAFARI_DRIVER_EXECUTABLE);
70+
}
71+
return this;
72+
}
73+
6274
protected File findDefaultExecutable() {
6375
return SAFARI_DRIVER_EXECUTABLE;
6476
}

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ private static class Option {
5656
private Option() {} // Utility class.
5757

5858
private static final String CLEAN_SESSION = "cleanSession";
59+
private static final String TECHNOLOGY_PREVIEW = "technologyPreview";
5960
private static final String PORT = "port";
6061
}
6162

@@ -69,6 +70,11 @@ private Option() {} // Utility class.
6970
*/
7071
private boolean useCleanSession = false;
7172

73+
/**
74+
* @see #setUseTechnologyPreview(boolean)
75+
*/
76+
private boolean useTechnologyPreview = false;
77+
7278
/**
7379
* Construct a {@link SafariOptions} instance from given capabilites.
7480
* When the {@link #CAPABILITY} capability is set, all other capabilities will be ignored!
@@ -120,6 +126,17 @@ public void setUseCleanSession(boolean useCleanSession) {
120126
this.useCleanSession = useCleanSession;
121127
}
122128

129+
/**
130+
* Instruct the SafariDriver to use the Safari Technology Preview if true, otherwise use the
131+
* release version of Safari. Defaults to using the release version of Safari.
132+
*
133+
* @param useTechnologyPreview If true, the SafariDriver will use the Safari Technology Preview,
134+
* otherwise will use the release version of Safari.
135+
*/
136+
public void setUseTechnologyPreview(boolean useTechnologyPreview) {
137+
this.useTechnologyPreview = useTechnologyPreview;
138+
}
139+
123140
// Getters
124141

125142
/**
@@ -139,6 +156,10 @@ public boolean getUseCleanSession() {
139156
return useCleanSession;
140157
}
141158

159+
public boolean getUseTechnologyPreview() {
160+
return useTechnologyPreview;
161+
}
162+
142163
// (De)serialization of the options
143164

144165
/**
@@ -151,6 +172,7 @@ public JsonObject toJson() throws IOException {
151172
JsonObject options = new JsonObject();
152173
options.addProperty(Option.PORT, port);
153174
options.addProperty(Option.CLEAN_SESSION, useCleanSession);
175+
options.addProperty(Option.TECHNOLOGY_PREVIEW, useTechnologyPreview);
154176
return options;
155177
}
156178

@@ -175,6 +197,12 @@ private static SafariOptions fromJsonMap(Map<?, ?> options) throws IOException {
175197
if (useCleanSession != null) {
176198
safariOptions.setUseCleanSession(useCleanSession);
177199
}
200+
201+
Boolean useTechnologyPreview = (Boolean) options.get(Option.TECHNOLOGY_PREVIEW);
202+
if (useTechnologyPreview != null) {
203+
safariOptions.setUseTechnologyPreview(useTechnologyPreview);
204+
}
205+
178206
return safariOptions;
179207
}
180208

@@ -198,11 +226,12 @@ public boolean equals(Object other) {
198226
}
199227
SafariOptions that = (SafariOptions) other;
200228
return this.port == that.port
201-
&& this.useCleanSession == that.useCleanSession;
229+
&& this.useCleanSession == that.useCleanSession
230+
&& this.useTechnologyPreview == that.useTechnologyPreview;
202231
}
203232

204233
@Override
205234
public int hashCode() {
206-
return Objects.hashCode(this.port, this.useCleanSession);
235+
return Objects.hashCode(this.port, this.useCleanSession, this.useTechnologyPreview);
207236
}
208237
}

java/client/test/org/openqa/selenium/safari/SafariDriverTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
AlertsTest.class,
3434
CleanSessionTest.class,
3535
CrossDomainTest.class,
36+
TechnologyPreviewTest.class,
3637
WebSocketConnectionTest.class
3738
})
3839
public class SafariDriverTests {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.openqa.selenium.safari;
2+
3+
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assume.assumeTrue;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.openqa.selenium.WebDriver;
10+
import org.openqa.selenium.testing.JUnit4TestBase;
11+
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
15+
16+
public class TechnologyPreviewTest extends JUnit4TestBase {
17+
18+
@Before
19+
public void checkTechnologyPreviewInstalled() {
20+
Path driverShim =
21+
Paths.get("/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver");
22+
assumeTrue("Technology Preview not installed", Files.exists(driverShim));
23+
}
24+
25+
@Test
26+
public void canStartTechnologyPreview() {
27+
SafariOptions options = new SafariOptions();
28+
options.setUseTechnologyPreview(true);
29+
30+
WebDriver driver = new SafariDriver(options);
31+
32+
driver.get(pages.xhtmlTestPage);
33+
assertEquals("XHTML Test Page", driver.getTitle());
34+
}
35+
36+
}

0 commit comments

Comments
 (0)