Skip to content

Commit bec8562

Browse files
committed
Implementing pure WebDriver grid server (v.3)
1 parent fa001b6 commit bec8562

File tree

6 files changed

+326
-23
lines changed

6 files changed

+326
-23
lines changed

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ task :support => [
151151
desc 'Build the standalone server'
152152
task 'selenium-server-standalone' => '//java/server/src/org/openqa/grid/selenium:selenium:uber'
153153

154+
task 'selenium-server-standalone-v3' => '//java/server/src/org/openqa/grid/selenium:selenium-v3:uber'
155+
154156
task :ide => [ "//ide:selenium-ide-multi" ]
155157
task :ide_proxy_setup => [ "//javascript/selenium-atoms", "se_ide:setup_proxy" ]
156158
task :ide_proxy_remove => [ "se_ide:remove_proxy" ]

java/server/src/org/openqa/grid/selenium/GridLauncher.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public static void main(String[] args) throws Exception {
112112
}
113113

114114
if (helper.isParamPresent("-help") || helper.isParamPresent("-h")) {
115-
printInfoAboutOptionsForRole(role);
115+
if (launchers.containsKey(role)) {
116+
launchers.get(role).printUsage();
117+
} else {
118+
printInfoAboutRoles(helper);
119+
}
116120
return;
117121
}
118122

@@ -153,23 +157,6 @@ private static void printInfoAboutRoles(CommandLineOptionHelper helper) {
153157
+ " with -help option and the corresponding -role option value");
154158
}
155159

156-
private static void printInfoAboutOptionsForRole(GridRole role) {
157-
String separator = "\n-------------------------------\n";
158-
switch (role) {
159-
case NOT_GRID:
160-
SeleniumServer.usage(separator + "Running as a standalone server:" + separator);
161-
break;
162-
case HUB:
163-
GridDocHelper.printHubHelp(separator + "Running as a grid hub:" + separator, false);
164-
break;
165-
case NODE:
166-
GridDocHelper.printNodeHelp(separator + "Running as a grid node:" + separator, false);
167-
break;
168-
default:
169-
throw new GridConfigurationException("Unknown role: " + role);
170-
}
171-
}
172-
173160
private static void configureLogging(CommandLineOptionHelper helper) {
174161
Level logLevel =
175162
helper.isParamPresent("-debug")
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.grid.selenium;
19+
20+
import com.google.common.collect.ImmutableMap;
21+
22+
import org.openqa.grid.common.CommandLineOptionHelper;
23+
import org.openqa.grid.common.GridDocHelper;
24+
import org.openqa.grid.common.GridRole;
25+
import org.openqa.grid.common.RegistrationRequest;
26+
import org.openqa.grid.common.exception.GridConfigurationException;
27+
import org.openqa.grid.internal.utils.GridHubConfiguration;
28+
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
29+
import org.openqa.grid.shared.CliUtils;
30+
import org.openqa.grid.web.Hub;
31+
import org.openqa.selenium.remote.server.SeleniumServer;
32+
import org.openqa.selenium.remote.server.log.LoggingOptions;
33+
import org.openqa.selenium.remote.server.log.TerseFormatter;
34+
35+
import java.io.File;
36+
import java.io.IOException;
37+
import java.util.logging.ConsoleHandler;
38+
import java.util.logging.FileHandler;
39+
import java.util.logging.Handler;
40+
import java.util.logging.Level;
41+
import java.util.logging.Logger;
42+
43+
public class GridLauncherV3 {
44+
45+
private static final Logger log = Logger.getLogger(GridLauncherV3.class.getName());
46+
47+
public interface GridItemLauncher {
48+
void launch(String[] args, Logger log) throws Exception;
49+
void printUsage();
50+
}
51+
52+
private static ImmutableMap<GridRole, GridItemLauncher> launchers
53+
= new ImmutableMap.Builder<GridRole, GridItemLauncher>()
54+
.put(GridRole.NOT_GRID, new GridItemLauncher() {
55+
@Override
56+
public void launch(String[] args, Logger log) throws Exception {
57+
log.info("Launching a standalone Selenium Server");
58+
SeleniumServer.main(args);
59+
log.info("Selenium Server is up and running");
60+
}
61+
62+
@Override
63+
public void printUsage() {
64+
String separator = "\n-------------------------------\n";
65+
SeleniumServer.usage(separator + "Running as a standalone server:" + separator);
66+
}
67+
})
68+
.put(GridRole.HUB, new GridItemLauncher() {
69+
@Override
70+
public void launch(String[] args, Logger log) throws Exception {
71+
log.info("Launching Selenium Grid hub");
72+
GridHubConfiguration c = GridHubConfiguration.build(args);
73+
Hub h = new Hub(c);
74+
h.start();
75+
log.info("Nodes should register to " + h.getRegistrationURL());
76+
log.info("Selenium Grid hub is up and running");
77+
}
78+
79+
@Override
80+
public void printUsage() {
81+
String separator = "\n-------------------------------\n";
82+
GridDocHelper.printHubHelp(separator + "Running as a grid hub:" + separator, false);
83+
}
84+
})
85+
.put(GridRole.NODE, new GridItemLauncher() {
86+
@Override
87+
public void launch(String[] args, Logger log) throws Exception {
88+
log.info("Launching a Selenium Grid node");
89+
RegistrationRequest c = RegistrationRequest.build(args);
90+
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
91+
remote.setRemoteServer(new SeleniumServer(c.getConfigAsInt("port", 4444)));
92+
remote.startRemoteServer();
93+
log.info("Selenium Grid node is up and ready to register to the hub");
94+
remote.startRegistrationProcess();
95+
}
96+
97+
@Override
98+
public void printUsage() {
99+
String separator = "\n-------------------------------\n";
100+
GridDocHelper.printNodeHelp(separator + "Running as a grid node:" + separator, false);
101+
}
102+
})
103+
.build();
104+
105+
public static void main(String[] args) throws Exception {
106+
CommandLineOptionHelper helper = new CommandLineOptionHelper(args);
107+
GridRole role = GridRole.find(args);
108+
109+
if (role == null) {
110+
printInfoAboutRoles(helper);
111+
return;
112+
}
113+
114+
if (helper.isParamPresent("-help") || helper.isParamPresent("-h")) {
115+
if (launchers.containsKey(role)) {
116+
launchers.get(role).printUsage();
117+
} else {
118+
printInfoAboutRoles(helper);
119+
}
120+
return;
121+
}
122+
123+
configureLogging(helper);
124+
125+
if (launchers.containsKey(role)) {
126+
try {
127+
launchers.get(role).launch(args, log);
128+
} catch (Exception e) {
129+
launchers.get(role).printUsage();
130+
e.printStackTrace();
131+
}
132+
} else {
133+
throw new GridConfigurationException("Unknown role: " + role);
134+
}
135+
}
136+
137+
private static void printInfoAboutRoles(CommandLineOptionHelper helper) {
138+
if (helper.hasParamValue("-role")) {
139+
CliUtils.printWrappedLine(
140+
"",
141+
"Error: the role '" + helper.getParamValue("-role") + "' does not match a recognized server role\n");
142+
} else {
143+
CliUtils.printWrappedLine(
144+
"",
145+
"Error: -role option needs to be followed by the value that defines role of this component in the grid\n");
146+
}
147+
System.out.println(
148+
"Selenium server can run in one of the following roles:\n" +
149+
" hub as a hub of a Selenium grid\n" +
150+
" node as a node of a Selenium grid\n" +
151+
" standalone as a standalone server not being a part of a grid\n" +
152+
"\n" +
153+
"If -role option is omitted the server runs standalone\n");
154+
CliUtils.printWrappedLine(
155+
"",
156+
"To get help on the options available for a specific role run the server"
157+
+ " with -help option and the corresponding -role option value");
158+
}
159+
160+
private static void configureLogging(CommandLineOptionHelper helper) {
161+
Level logLevel =
162+
helper.isParamPresent("-debug")
163+
? Level.FINE
164+
: LoggingOptions.getDefaultLogLevel();
165+
if (logLevel == null) {
166+
logLevel = Level.INFO;
167+
}
168+
Logger.getLogger("").setLevel(logLevel);
169+
Logger.getLogger("org.openqa.jetty").setLevel(Level.WARNING);
170+
171+
String logFilename =
172+
helper.isParamPresent("-log")
173+
? helper.getParamValue("-log")
174+
: LoggingOptions.getDefaultLogOutFile();
175+
if (logFilename != null) {
176+
for (Handler handler : Logger.getLogger("").getHandlers()) {
177+
if (handler instanceof ConsoleHandler) {
178+
Logger.getLogger("").removeHandler(handler);
179+
}
180+
}
181+
try {
182+
Handler logFile = new FileHandler(new File(logFilename).getAbsolutePath(), true);
183+
logFile.setFormatter(new TerseFormatter(true));
184+
logFile.setLevel(logLevel);
185+
Logger.getLogger("").addHandler(logFile);
186+
} catch (IOException e) {
187+
throw new RuntimeException(e);
188+
}
189+
} else {
190+
boolean logLongForm = helper.isParamPresent("-logLongForm");
191+
for (Handler handler : Logger.getLogger("").getHandlers()) {
192+
if (handler instanceof ConsoleHandler) {
193+
handler.setLevel(logLevel);
194+
handler.setFormatter(new TerseFormatter(logLongForm));
195+
}
196+
}
197+
}
198+
}
199+
}

java/server/src/org/openqa/grid/selenium/build.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ java_binary(name = "selenium",
77
"//java/server/src/org/openqa/selenium/server",
88
"//java/server/src/org/openqa/grid",
99
])
10+
11+
java_binary(name = "selenium-v3",
12+
main_class = "org.openqa.grid.selenium.GridLauncherV3",
13+
srcs = [ "GridLauncherV3.java" ],
14+
deps = [
15+
"//java/server/src/org/openqa/selenium/remote/server:real-server",
16+
"//java/server/src/org/openqa/grid",
17+
])

0 commit comments

Comments
 (0)