|
17 | 17 |
|
18 | 18 | package org.openqa.selenium.testing.drivers;
|
19 | 19 |
|
20 |
| -import static org.junit.Assert.assertTrue; |
21 |
| -import static org.junit.Assert.fail; |
22 | 20 | import static org.openqa.selenium.testing.DevMode.isInDevMode;
|
23 |
| -import static org.openqa.selenium.testing.InProject.locate; |
24 | 21 |
|
25 | 22 | import com.google.common.collect.ImmutableMap;
|
26 |
| -import com.google.common.io.Files; |
27 | 23 |
|
28 |
| -import org.openqa.selenium.Build; |
| 24 | +import org.openqa.selenium.BuckBuild; |
29 | 25 | import org.openqa.selenium.Capabilities;
|
30 | 26 | import org.openqa.selenium.firefox.FirefoxDriver;
|
31 | 27 | import org.openqa.selenium.firefox.FirefoxProfile;
|
32 | 28 | import org.openqa.selenium.remote.DesiredCapabilities;
|
| 29 | +import org.openqa.selenium.testing.DevMode; |
33 | 30 |
|
34 | 31 | import java.io.File;
|
35 | 32 | import java.io.IOException;
|
| 33 | +import java.io.InputStream; |
| 34 | +import java.net.URL; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
36 | 37 |
|
37 | 38 | public class SynthesizedFirefoxDriver extends FirefoxDriver {
|
38 | 39 |
|
39 | 40 | private static boolean runBuild = true;
|
| 41 | + private static File cachedExt = null; |
40 | 42 |
|
41 | 43 | public SynthesizedFirefoxDriver() {
|
42 | 44 | this(new DesiredCapabilities(), new DesiredCapabilities());
|
43 | 45 | }
|
44 | 46 |
|
45 | 47 | public SynthesizedFirefoxDriver(FirefoxProfile profile) throws IOException {
|
46 |
| - this(new DesiredCapabilities(), new DesiredCapabilities(ImmutableMap.of(PROFILE, profile))); |
| 48 | + this(new DesiredCapabilities(ImmutableMap.of(PROFILE, profile)), new DesiredCapabilities()); |
47 | 49 | }
|
48 | 50 |
|
49 | 51 | public SynthesizedFirefoxDriver(Capabilities desiredCapabilities) {
|
50 | 52 | this(desiredCapabilities, null);
|
51 | 53 | }
|
52 | 54 |
|
53 | 55 | public SynthesizedFirefoxDriver(Capabilities desiredCapabilities,
|
54 |
| - Capabilities requiredCapabilities) { |
| 56 | + Capabilities requiredCapabilities) { |
55 | 57 | super(tweakCapabilities(desiredCapabilities), requiredCapabilities);
|
56 | 58 | }
|
57 | 59 |
|
@@ -103,54 +105,46 @@ private static FirefoxProfile createTemporaryProfile() {
|
103 | 105 | }
|
104 | 106 |
|
105 | 107 | try {
|
106 |
| - File prefs = locate("build/javascript/firefox-driver/webdriver_prefs.json"); |
107 |
| - File noFocus = locate("build/cpp/i386/libnoblur.so"); |
108 |
| - File ime = locate("build/cpp/i386/libimehandler.so"); |
109 |
| - File noFocus64 = locate("build/cpp/amd64/libnoblur64.so"); |
110 |
| - File ime64 = locate("build/cpp/amd64/libimehandler64.so"); |
111 |
| - File dest = locate("java/client/build/production/org/openqa/selenium/firefox"); |
112 |
| - Files.copy(prefs, new File(dest, "webdriver_prefs.json")); |
113 |
| - |
114 |
| - File libDir = new File(dest, "x86"); |
115 |
| - if (!libDir.exists()) { |
116 |
| - assertTrue("Cannot create x86 library directory", libDir.mkdir()); |
117 |
| - } |
118 |
| - Files.copy(noFocus, new File(libDir, "x_ignore_nofocus.so")); |
119 |
| - Files.copy(ime, new File(libDir, "libibushandler.so")); |
120 |
| - |
121 |
| - libDir = new File(dest, "amd64"); |
122 |
| - if (!libDir.exists()) { |
123 |
| - assertTrue("Cannot create x86 library directory", libDir.mkdir()); |
124 |
| - } |
125 |
| - Files.copy(noFocus64, new File(libDir, "x_ignore_nofocus.so")); |
126 |
| - Files.copy(ime64, new File(libDir, "libibushandler.so")); |
127 |
| - |
128 | 108 | FirefoxProfile profile = new FirefoxProfile();
|
129 |
| - |
130 | 109 | if (Boolean.getBoolean("webdriver.debug")) {
|
| 110 | + |
131 | 111 | Firebug.addTo(profile);
|
132 | 112 | }
|
133 | 113 |
|
134 | 114 | profile.setEnableNativeEvents(Boolean.getBoolean("selenium.browser.native_events"));
|
135 | 115 | profile.setPreference("webdriver.log.file", "/dev/stdout");
|
136 | 116 |
|
137 | 117 | return copyExtensionTo(profile);
|
138 |
| - } catch (Exception e) { |
| 118 | + } catch (IOException e) { |
139 | 119 | e.printStackTrace();
|
140 |
| - fail(e.getMessage()); |
| 120 | + throw new RuntimeException(e); |
141 | 121 | }
|
142 |
| - return null; |
143 | 122 | }
|
144 | 123 |
|
145 | 124 | private static FirefoxProfile copyExtensionTo(FirefoxProfile profile) throws IOException {
|
146 |
| - File topDir = locate("Rakefile").getParentFile(); |
147 |
| - File ext = new File(topDir, |
148 |
| - "build/javascript/firefox-driver/webdriver.xpi"); |
149 |
| - if (!ext.exists() || runBuild) { |
150 |
| - ext.delete(); |
151 |
| - new Build().of("//javascript/firefox-driver:webdriver").go(); |
| 125 | + // Look for the xpi as a resource first. |
| 126 | + URL resource = FirefoxDriver.class.getResource("/org/openqa/selenium/firefox/webdriver.xpi"); |
| 127 | + File ext; |
| 128 | + if (resource != null || !DevMode.isInDevMode()) { |
| 129 | + Path path = Files.createTempFile("syn-firefox", ".xpi"); |
| 130 | + ext = path.toFile(); |
| 131 | + ext.deleteOnExit(); |
| 132 | + try (InputStream is = resource.openStream()) { |
| 133 | + Files.copy(is, path); |
| 134 | + } |
| 135 | + } else if (runBuild) { |
| 136 | + Path output = new BuckBuild().of("//javascript/firefox-driver:webdriver").go(); |
| 137 | + ext = output.toFile(); |
| 138 | + cachedExt = ext; |
152 | 139 | runBuild = false;
|
| 140 | + } else { |
| 141 | + ext = cachedExt; |
153 | 142 | }
|
| 143 | + |
| 144 | + if (ext == null) { |
| 145 | + throw new RuntimeException("Cannot compile firefox extension"); |
| 146 | + } |
| 147 | + |
154 | 148 | profile.addExtension(ext);
|
155 | 149 | return profile;
|
156 | 150 | }
|
|
0 commit comments