1 package com.atlassian.selenium;
2
3 import org.apache.log4j.Logger;
4 import org.openqa.selenium.server.SeleniumServer;
5 import org.openqa.selenium.server.RemoteControlConfiguration;
6
7 import java.io.File;
8 import java.lang.reflect.InvocationHandler;
9 import java.lang.reflect.Proxy;
10 import java.util.List;
11
12
13
14
15 public class SeleniumStarter
16 {
17 private static final Logger log = Logger.getLogger(SeleniumStarter.class);
18
19 private static SeleniumStarter instance = new SeleniumStarter();
20 private SeleniumClient client;
21 private SeleniumServer server;
22 private String userAgent;
23 private boolean manual = true;
24
25 private SeleniumStarter()
26 {
27 }
28
29 public static SeleniumStarter getInstance()
30 {
31 return instance;
32 }
33
34 public synchronized SeleniumClient getSeleniumClient(SeleniumConfiguration config)
35 {
36 if (client == null)
37 {
38 client = new SingleBrowserSeleniumClient(config);
39 }
40 return client;
41 }
42
43 public SeleniumClient getSeleniumClient(List<SeleniumConfiguration> configs) {
44 return getSeleniumClient(configs, false);
45 }
46
47
48
49
50
51
52 public synchronized SeleniumClient getSeleniumClient(List<SeleniumConfiguration> configs, boolean parallel)
53 {
54 if (client == null)
55 {
56 InvocationHandler ih = new MultiBrowserSeleniumClientInvocationHandler(configs, 10000000L, true, parallel);
57 client = (SeleniumClient)Proxy.newProxyInstance(SeleniumClient.class.getClassLoader(),
58 new Class[] {SeleniumClient.class}, ih );
59 }
60 return client;
61
62 }
63
64 public synchronized SeleniumServer getSeleniumServer(SeleniumConfiguration config)
65 {
66 if (server == null)
67 {
68 try
69 {
70 RemoteControlConfiguration rcConfig = new RemoteControlConfiguration();
71 rcConfig.setPort(config.getServerPort());
72 rcConfig.setDebugMode(true);
73 rcConfig.setSingleWindow(config.getSingleWindowMode());
74 if (config.getFirefoxProfileTemplate() != null) {
75 rcConfig.setFirefoxProfileTemplate(new File(config.getFirefoxProfileTemplate()));
76 }
77 server = new SeleniumServer(rcConfig);
78 } catch (Exception e)
79 {
80 log.error("Error creating SeleniumServer!", e);
81 }
82 }
83 return server;
84 }
85
86
87 private void startServer(SeleniumConfiguration config)
88 {
89 try
90 {
91 if(config.getStartSeleniumServer())
92 {
93 log.info("Starting Selenium Server");
94 getSeleniumServer(config).start();
95 log.info("Selenium Server Started");
96 }
97 else
98 {
99 log.info("Not starting Selenium Server");
100 }
101
102 } catch (Exception e)
103 {
104 log.error("Error starting SeleniumServer!", e);
105 }
106
107 }
108
109 public void start(SeleniumConfiguration config)
110 {
111 log.info("Starting Selenium");
112 startServer(config);
113 log.info("Starting Selenium Client");
114 getSeleniumClient(config).start();
115 log.info("Selenium Client Started");
116 log.info("Selenium startup complete");
117 }
118
119 public void start(List<SeleniumConfiguration> configs)
120 {
121 log.info("Starting Selenium");
122 startServer(configs.get(0));
123 log.info("Starting Selenium Client");
124 getSeleniumClient(configs).start();
125 log.info("Selenium Client Started");
126 log.info("Selenium startup complete");
127
128 }
129
130 public void stop()
131 {
132 if(client != null)
133 {
134 client.stop();
135 }
136
137 if(server != null)
138 {
139 server.stop();
140
141
142 server = null;
143 }
144 }
145
146 public boolean isManual()
147 {
148 return manual;
149 }
150
151 public void setManual(boolean manual)
152 {
153 this.manual = manual;
154 }
155 }