View Javadoc

1   package com.atlassian.selenium.browsers;
2   
3   import java.io.IOException;
4   import java.net.ServerSocket;
5   import java.net.Socket;
6   
7   import com.atlassian.browsers.BrowserAutoInstaller;
8   import com.atlassian.browsers.BrowserConfig;
9   import com.atlassian.browsers.InstallConfigurator;
10  import com.atlassian.selenium.AbstractSeleniumConfiguration;
11  import com.atlassian.selenium.browsers.firefox.DisplayAwareFirefoxChromeLauncher;
12  
13  import org.openqa.selenium.server.browserlaunchers.BrowserLauncherFactory;
14  
15  /**
16   * Configures Selenium by detecting if installation of a browserStartString is required, and if so, installs it for this session.
17   *
18   * @since 2.0
19   */
20  class AutoInstallConfiguration extends AbstractSeleniumConfiguration
21  {
22  
23      private static final String LOCATION = System.getProperty("selenium.location", "localhost");
24      private static final int PORT = Integer.getInteger("selenium.port", pickFreePort());
25  
26      //private static final String BROWSER = System.getProperty("selenium.browser", "firefox-3.5");
27  
28      private static final String BASE_URL = System.getProperty("baseurl", "http://localhost:8080/");
29      private static final String BROWSER_PROFILE = System.getProperty("selenium.browser.profile");
30  
31      private static final long MAX_WAIT_TIME = 10000;
32      private static final long CONDITION_CHECK_INTERVAL = 100;
33  
34      private String firefoxProfileTemplate = BROWSER_PROFILE;
35      private String baseUrl;
36  
37      private String browserStartString = SeleniumBrowserConfiguration.BROWSER;
38  
39      AutoInstallConfiguration()
40      {
41  
42          BrowserAutoInstaller browserInstaller = new BrowserAutoInstaller(new SeleniumBrowserConfiguration(),
43              new InstallConfigurator() {
44                  public void setupFirefoxBrowser(BrowserConfig browserConfig)
45                  {
46                      firefoxProfileTemplate = browserConfig.getProfilePath();
47                      browserStartString = "*" + BrowserAutoInstaller.CHROME_XVFB + " " + browserConfig.getBinaryPath();
48                  }
49  
50                  public void setupChromeBrowser(BrowserConfig browserConfig)
51                  {
52  
53                  }
54              });
55  
56          browserInstaller.setupBrowser();
57  
58          BrowserLauncherFactory.addBrowserLauncher(BrowserAutoInstaller.CHROME_XVFB, DisplayAwareFirefoxChromeLauncher.class);
59  
60          if (browserStartString == null)
61          {
62              browserStartString = SeleniumBrowserConfiguration.BROWSER;
63          }
64          if (!BASE_URL.endsWith("/"))
65          {
66              baseUrl = BASE_URL + "/";
67          }
68          else
69          {
70              baseUrl = BASE_URL;
71          }
72      }
73  
74      public String getServerLocation()
75      {
76          return LOCATION;
77      }
78  
79      public int getServerPort()
80      {
81          return PORT;
82      }
83  
84      public String getBrowserStartString()
85      {
86          return browserStartString;
87      }
88  
89      public String getBaseUrl()
90      {
91          return baseUrl;
92      }
93  
94      public boolean getStartSeleniumServer()
95      {
96          return !isPortInUse();
97      }
98  
99      private boolean isPortInUse()
100     {
101         try
102         {
103             new Socket("localhost", getServerPort());
104             return true;
105         }
106         catch (Exception e)
107         {
108             return false;
109         }
110     }
111 
112     public long getActionWait()
113     {
114         return MAX_WAIT_TIME;
115     }
116 
117     public long getPageLoadWait()
118     {
119         return MAX_WAIT_TIME;
120     }
121 
122     public long getConditionCheckInterval()
123     {
124         return CONDITION_CHECK_INTERVAL;
125     }
126 
127     @Override
128     public String getFirefoxProfileTemplate()
129     {
130         return firefoxProfileTemplate;
131     }
132 
133     static int pickFreePort()
134     {
135         ServerSocket socket = null;
136         try
137         {
138             socket = new ServerSocket(0);
139             return socket.getLocalPort();
140         }
141         catch (IOException e)
142         {
143             throw new RuntimeException("Error opening socket", e);
144         }
145         finally
146         {
147             if (socket != null)
148             {
149                 try
150                 {
151                     socket.close();
152                 }
153                 catch (IOException e)
154                 {
155                     throw new RuntimeException("Error closing socket", e);
156                 }
157             }
158         }
159     }
160 }