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