View Javadoc

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   * A helper utility for obtaining a FirefoxDriver.
18   * @since 2.0
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       * Gets the default FirefoxDriver that tries to use the default system paths
31       * for where the firefox binary should be
32       * @return Default configured FirefoxDriver
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       * Configures a FirefoxDriver based on the browserConfig
43       * @param browserConfig browser config that points to the binary path of the browser and
44       * optional profile
45       * @return A configured FirefoxDriver based on the browserConfig passed in
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          // Fall back on default firefox driver
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         // Filter the extentions path to only include extensions.
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      * Gets a firefox driver based on the browser path based in
129      * @param browserPath the path to the firefox binary to use for the firefox driver.
130      * @return A FirefoxDriver that is using the binary at the browserPath
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         // Fall back on default firefox driver
144         log.info("Browser path was null, falling back to default firefox driver.");
145         return getFirefoxDriver();
146     }
147 
148     /**
149      * Sets up system properties on the firefox driver.
150      * @param firefox
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 }