1 package com.atlassian.webdriver.browsers.firefox;
2
3 import com.atlassian.browsers.BrowserConfig;
4 import com.atlassian.webdriver.browsers.profile.ProfilePreferences;
5 import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;
6 import org.openqa.selenium.firefox.FirefoxBinary;
7 import org.openqa.selenium.firefox.FirefoxDriver;
8 import org.openqa.selenium.firefox.FirefoxProfile;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.io.File;
13 import java.io.FileFilter;
14 import java.io.IOException;
15 import java.util.Map;
16
17
18
19
20
21 public final class FirefoxBrowser
22 {
23 private static final Logger log = LoggerFactory.getLogger(FirefoxBrowser.class);
24
25 private FirefoxBrowser()
26 {
27 throw new IllegalStateException("FirefoxBrowser is not constructable");
28 }
29
30
31
32
33
34
35 public static FirefoxDriver getFirefoxDriver()
36 {
37 FirefoxBinary firefox = new FirefoxBinary();
38 setSystemProperties(firefox);
39 return constructFirefoxDriver(firefox, null);
40 }
41
42
43
44
45
46
47
48 public static FirefoxDriver getFirefoxDriver(BrowserConfig browserConfig)
49 {
50 FirefoxBinary firefox;
51 FirefoxProfile profile = null;
52
53 if (browserConfig != null)
54 {
55 firefox = new FirefoxBinary(new File(browserConfig.getBinaryPath()));
56 if (browserConfig.getProfilePath() != null)
57 {
58 File profilePath = new File(browserConfig.getProfilePath());
59
60 profile = new FirefoxProfile();
61
62 addExtensionsToProfile(profile, profilePath);
63 addPreferencesToProfile(profile, profilePath);
64 }
65
66 setSystemProperties(firefox);
67 return constructFirefoxDriver(firefox, profile);
68 }
69
70
71 return getFirefoxDriver();
72 }
73
74 private static void addPreferencesToProfile(FirefoxProfile profile, File profilePath)
75 {
76 File profilePreferencesFile = new File(profilePath, "profile.preferences");
77
78 if (profilePreferencesFile.exists())
79 {
80 ProfilePreferences profilePreferences = new ProfilePreferences(profilePreferencesFile);
81 Map<String, Object> preferences = profilePreferences.getPreferences();
82 for (String key : preferences.keySet())
83 {
84 Object value = preferences.get(key);
85 if (value instanceof Integer)
86 {
87 profile.setPreference(key, (Integer)value);
88 }
89 else if (value instanceof Boolean)
90 {
91 profile.setPreference(key, (Boolean)value);
92 }
93 else
94 {
95 profile.setPreference(key, (String)value);
96 }
97 }
98 }
99 }
100
101 private static void addExtensionsToProfile(FirefoxProfile profile, File profilePath)
102 {
103
104 for (File extension : profilePath.listFiles(new FileFilter()
105 {
106 public boolean accept(final File file)
107 {
108 if (file.getName().matches(".*\\.xpi$"))
109 {
110 return true;
111 }
112
113 return false;
114 }
115 }))
116 {
117 try
118 {
119 profile.addExtension(extension);
120 }
121 catch (IOException e)
122 {
123 log.error("Unable to load extension: " + extension, e);
124 }
125 }
126 }
127
128
129
130
131
132
133 public static FirefoxDriver getFirefoxDriver(String browserPath)
134 {
135 FirefoxBinary firefox;
136
137 if (browserPath != null)
138 {
139 firefox = new FirefoxBinary(new File(browserPath));
140 setSystemProperties(firefox);
141 return constructFirefoxDriver(firefox, null);
142 }
143
144
145 log.info("Browser path was null, falling back to default firefox driver.");
146 return getFirefoxDriver();
147 }
148
149 private static FirefoxDriver constructFirefoxDriver(final FirefoxBinary binary, FirefoxProfile profile)
150 {
151 if (profile == null)
152 {
153 profile = new FirefoxProfile();
154 }
155
156 try
157 {
158 JavaScriptError.addExtension(profile);
159 }
160 catch (IOException e)
161 {
162 log.error("Failed to add JavaScriptError extension to profile", e);
163 }
164
165 return new FirefoxDriver(binary, profile);
166 }
167
168
169
170
171
172 private static void setSystemProperties(FirefoxBinary firefox)
173 {
174 if (System.getProperty("DISPLAY") != null)
175 {
176 firefox.setEnvironmentProperty("DISPLAY", System.getProperty("DISPLAY"));
177 }
178 }
179 }