1 package org.itracker.selenium;
2
3 import com.thoughtworks.selenium.DefaultSelenium;
4 import com.thoughtworks.selenium.Selenium;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Properties;
8
9 import com.thoughtworks.selenium.SeleniumException;
10 import org.apache.log4j.Logger;
11 import org.jfree.util.Log;
12
13
14
15
16
17 public class SeleniumManager {
18 private final static String PROPERTY_SELENIUM_BROWSER = "selenium.browser";
19 private final static String PROPERTY_SELENIUM_HOST = "selenium.host";
20 private final static String PROPERTY_SELENIUM_PORT = "selenium.port";
21 private final static String PROPERTY_APPLICATION_HOST = "application.host";
22 private final static String PROPERTY_APPLICATION_PORT = "application.port";
23 private final static String PROPERTY_APPLICATION_PATH = "application.path";
24
25 private static Selenium selenium = null;
26
27 private static String seleniumHost = null;
28 private static int seleniumPort = 4444;
29 private static String seleniumBrowser = null;
30 private static String applicationHost = null;
31 private static int applicationPort = 8080;
32 private static String applicationPath = null;
33
34 private static final Logger log = Logger.getLogger(SeleniumManager.class);
35
36 static {
37 Runtime.getRuntime().addShutdownHook(new Thread() {
38 @Override
39 public void run() {
40 if (null != SeleniumManager.selenium) {
41 try {
42 SeleniumManager.selenium.stop();
43 } catch (SeleniumException e) {
44 log.warn("could not stop running selenium: " + selenium);
45 log.debug("exception caught", e);
46 }
47
48 }
49 }
50 });
51 }
52
53 public static Selenium getSelenium() throws IOException {
54 if (null == selenium) {
55 log.info("starting new selenium");
56 final InputStream inputStream = SeleniumManager.class
57 .getResourceAsStream("SeleniumManager.properties");
58 final Properties properties = new Properties();
59 properties.load(inputStream);
60 seleniumBrowser =
61 properties.getProperty(PROPERTY_SELENIUM_BROWSER, "*firefox");
62 seleniumHost =
63 properties.getProperty(PROPERTY_SELENIUM_HOST, "localhost");
64 seleniumPort =
65 Integer.valueOf(properties.getProperty(PROPERTY_SELENIUM_PORT, "5555"));
66 applicationHost =
67 properties.getProperty(PROPERTY_APPLICATION_HOST, "localhost");
68 applicationPort =
69 Integer.valueOf(properties.getProperty(PROPERTY_APPLICATION_PORT, "8888"));
70 applicationPath =
71 properties.getProperty(PROPERTY_APPLICATION_PATH, "itracker");
72 selenium = new DefaultSelenium(seleniumHost, seleniumPort,
73 seleniumBrowser,
74 "http://" + applicationHost + ":" + applicationPort + "/"
75 + applicationPath);
76 selenium.start();
77 }
78 return selenium;
79 }
80
81
82
83
84 protected static void closeSession(Selenium selenium) {
85 if (log.isDebugEnabled()) {
86 log.debug("closeSession: " + selenium);
87 }
88 selenium.open("http://" + applicationHost + ":" + applicationPort + "/"
89 + applicationPath + "/logoff.do");
90
91 }
92
93 public static String getSeleniumHost() {
94 return seleniumHost;
95 }
96
97 public static int getSeleniumPort() {
98 return seleniumPort;
99 }
100
101 public static String getSeleniumBrowser() {
102 return seleniumBrowser;
103 }
104
105 public static String getApplicationHost() {
106 return applicationHost;
107 }
108
109 public static int getApplicationPort() {
110 return applicationPort;
111 }
112
113 public static String getApplicationPath() {
114 return applicationPath;
115 }
116 }