View Javadoc

1   /*
2    * Copyright 2006 ThoughtWorks, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package com.atlassian.selenium.browsers.firefox;
18  
19  import org.openqa.selenium.Capabilities;
20  import org.openqa.selenium.browserlaunchers.BrowserLauncher;
21  import org.openqa.selenium.browserlaunchers.locators.*;
22  import org.openqa.selenium.remote.BrowserType;
23  import org.openqa.selenium.remote.CapabilityType;
24  import org.openqa.selenium.server.ApplicationRegistry;
25  import org.openqa.selenium.server.RemoteControlConfiguration;
26  import org.openqa.selenium.server.browserlaunchers.FirefoxCustomProfileLauncher;
27  import org.openqa.selenium.server.browserlaunchers.InvalidBrowserExecutableException;
28  import org.openqa.selenium.server.browserlaunchers.ProxyInjectionFirefoxCustomProfileLauncher;
29  
30  /**
31   * This is an override of the {@link org.openqa.selenium.server.browserlaunchers.FirefoxLauncher}
32   * class from selenium. This allows us to control the display system property
33   * for xvfb.
34   */
35  public class DisplayAwareFirefoxLauncher implements BrowserLauncher {
36  
37    final BrowserLauncher realLauncher;
38  
39    public DisplayAwareFirefoxLauncher(Capabilities browserOptions, RemoteControlConfiguration configuration,
40        String sessionId, String browserLaunchLocation)
41        throws InvalidBrowserExecutableException {
42      String browserName = BrowserType.FIREFOX;
43      BrowserLocator locator = new CombinedFirefoxLocator();
44      String version = (String) browserOptions.getCapability(CapabilityType.VERSION);
45      if ("2".equals(version)) {
46        browserName = BrowserType.FIREFOX_2;
47        locator = new Firefox2Locator();
48      }
49      if ("3".equals(version)) {
50        browserName = BrowserType.FIREFOX_3;
51        locator = new Firefox3Locator();
52      }
53      String mode = (String) browserOptions.getCapability("mode");
54      if (mode == null) {
55        mode = "chrome";
56      }
57      if ("default".equals(mode)) {
58        mode = "chrome";
59      }
60  
61      BrowserInstallation installation =
62          ApplicationRegistry.instance().browserInstallationCache().locateBrowserInstallation(
63              browserName, browserLaunchLocation, locator);
64  
65      if (installation == null) {
66        throw new InvalidBrowserExecutableException(
67            "The specified path to the browser executable is invalid.");
68      }
69  
70      if ("chrome".equals(mode)) {
71        realLauncher =
72            new DisplayAwareFirefoxChromeLauncher(browserOptions, configuration, sessionId, installation);
73        return;
74      }
75  
76      boolean proxyInjectionMode =
77          browserOptions.is("proxyInjectionMode") || "proxyInjection".equals(mode);
78  
79      // You can't just individually configure a browser for PI mode; it's a server-level
80      // configuration parameter
81      boolean globalProxyInjectionMode = configuration.getProxyInjectionModeArg();
82      if (proxyInjectionMode && !globalProxyInjectionMode) {
83        if (proxyInjectionMode) {
84          throw new RuntimeException(
85              "You requested proxy injection mode, but this server wasn't configured with -proxyInjectionMode on the command line");
86        }
87      }
88  
89      // if user didn't request PI, but the server is configured that way, just switch up to PI
90      proxyInjectionMode = globalProxyInjectionMode;
91      if (proxyInjectionMode) {
92        realLauncher =
93            new ProxyInjectionFirefoxCustomProfileLauncher(browserOptions, configuration, sessionId,
94                installation);
95        return;
96      }
97  
98      // the mode isn't "chrome" or "proxyInjection"; at this point it had better be
99      // CapabilityType.PROXY
100     if (!CapabilityType.PROXY.equals(mode)) {
101       throw new RuntimeException("Unrecognized browser mode: " + mode);
102     }
103 
104     realLauncher =
105         new FirefoxCustomProfileLauncher(browserOptions, configuration, sessionId, installation);
106 
107   }
108 
109   public void close() {
110     realLauncher.close();
111   }
112 
113   public void launchHTMLSuite(String suiteUrl, String baseUrl) {
114     realLauncher.launchHTMLSuite(suiteUrl, baseUrl);
115   }
116 
117   public void launchRemoteSession(String url) {
118     realLauncher.launchRemoteSession(url);
119   }
120 
121 }