Skip to content

Commit ed5972e

Browse files
committed
Overhaul the Selenium Grid code
1 parent 914b518 commit ed5972e

17 files changed

+325
-105
lines changed

integrations/selenium_grid/ReadMe.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
1-
## Notes on using the Selenium Grid Hub
1+
## The Selenium Grid Hub
22

3-
The Selenium Grid Hub allows you to distribute tests to run in parallel across multiple machines. Each machine can then run its own allocation of tests in parallel. This allows you to run an entire test suite quickly, which may be important if you have a lot of tests to run. Machines can be personal computers, data centers, or virtual machines in the cloud. You can also create your own virtual machine by using a tool such as Docker (see the [Docker ReadMe](https://github.com/seleniumbase/SeleniumBase/blob/master/integrations/docker/ReadMe.md)).
3+
The Selenium Grid Hub lets you distribute tests to run in parallel across multiple node machines. Each node machine can then run its own allocation of tests. This allows you to run a large suite of tests very quickly.
44

55
### Running the Selenium Grid Hub
66

7-
First, download the latest selenium-server-standalone jar file to this folder (integrations/selenium_grid):
8-
```bash
9-
python download_selenium.py
10-
```
11-
Now you can start up the Grid Hub:
12-
```bash
13-
./grid-hub start
14-
```
15-
Now you can add a Grid Node to the Grid Hub:
16-
```bash
17-
./grid-node start
18-
```
19-
(NOTE: If the Grid Node is not running on the same machine as the Grid Hub, update the address listed for WEBDRIVER_NODE_PARAMS in the [grid-node](https://github.com/seleniumbase/SeleniumBase/blob/master/integrations/selenium_grid/grid-node) script.)
20-
You should be able to see the Grid Console up and running from here: [http://0.0.0.0:4444/grid/console](http://0.0.0.0:4444/grid/console) (NOTE: That's the address if you're running locally from localhost.)
7+
The following commands will work once you've installed seleniumbase, which comes with the seleniumbase console scripts interface.
218

22-
You can remove a Grid Node from the Grid Hub with:
9+
Grid Hub server controls:
2310
```bash
24-
./grid-node stop
11+
seleniumbase grid-hub {start|stop|restart} [OPTIONS]
2512
```
26-
You can stop the Grid Hub at anytime with:
13+
Options:
14+
* ``-v``, ``--verbose`` (Increases verbosity of logging output.)
15+
16+
Grid node server controlls:
2717
```bash
28-
./grid-hub stop
18+
seleniumbase grid-node {start|stop|restart} --hub=[HUB_IP] [OPTIONS]
2919
```
20+
Options:
21+
* ``-v``, ``--verbose`` (Increases verbosity of logging output.)
22+
* ``--hub=[HUB_IP]`` (Specifies the Grid Hub to connect to. Default: "127.0.0.1".)
3023

31-
When running with nosetests, configure a "``setup.cfg``" file with your grid hub info. (See the example [selenium_server_config_example.cfg](https://github.com/seleniumbase/SeleniumBase/blob/master/integrations/selenium_grid/selenium_server_config_example.cfg) file.)
24+
When the Grid Hub Console is up and running, you'll be able to find it here: [http://127.0.0.1:4444/grid/console](http://127.0.0.1:4444/grid/console)
3225

33-
When running with pytest, add the server and port info to a "``pytest.ini``" file. (Or add that data directly on the command line when you run your tests.)
3426

35-
#### More detailed info about connecting to the Selenium Grid Hub can be found here:
27+
#### More info about the Selenium Grid Hub can be found here:
3628
* [https://github.com/SeleniumHQ/selenium/wiki/Grid2](https://github.com/SeleniumHQ/selenium/wiki/Grid2)
3729
* [https://github.com/SeleniumHQ/selenium/wiki](https://github.com/SeleniumHQ/selenium/wiki/Grid2)

integrations/selenium_grid/__init__.py

Whitespace-only changes.

integrations/selenium_grid/download_selenium.py

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
""" Downloads the Selenium Server JAR file and renames it. """
2+
3+
import os
4+
import sys
5+
if sys.version_info[0] == 2:
6+
from urllib import urlopen
7+
else:
8+
from urllib.request import urlopen
9+
10+
SELENIUM_JAR = ("http://selenium-release.storage.googleapis.com"
11+
"/3.11/selenium-server-standalone-3.11.0.jar")
12+
JAR_FILE = "selenium-server-standalone-3.11.0.jar"
13+
RENAMED_JAR_FILE = "selenium-server-standalone.jar"
14+
15+
dir_path = os.path.dirname(os.path.realpath(__file__))
16+
FULL_EXPECTED_PATH = dir_path + "/" + RENAMED_JAR_FILE
17+
FULL_DOWNLOAD_PATH = os.getcwd() + '/' + RENAMED_JAR_FILE
18+
19+
20+
def download_selenium_server():
21+
"""
22+
Downloads the Selenium Server JAR file from its
23+
online location and stores it locally.
24+
"""
25+
try:
26+
local_file = open(JAR_FILE, 'wb')
27+
remote_file = urlopen(SELENIUM_JAR)
28+
print('Downloading the Selenium Server JAR file...\n')
29+
local_file.write(remote_file.read())
30+
local_file.close()
31+
remote_file.close()
32+
print('Download Complete!')
33+
except Exception:
34+
raise Exception("Error downloading the Selenium Server JAR file.\n"
35+
"Details: %s" % sys.exc_info()[1])
36+
37+
38+
def is_available_locally():
39+
return os.path.isfile(FULL_EXPECTED_PATH)
40+
41+
42+
def main():
43+
if not is_available_locally():
44+
download_selenium_server()
45+
for filename in os.listdir("."):
46+
# If multiple copies exist, keep only the latest and rename it.
47+
if filename.startswith("selenium-server-standalone-"):
48+
os.rename(filename, RENAMED_JAR_FILE)
49+
if FULL_DOWNLOAD_PATH != FULL_EXPECTED_PATH:
50+
os.rename(RENAMED_JAR_FILE, FULL_EXPECTED_PATH)
51+
print("%s\n" % FULL_EXPECTED_PATH)
52+
53+
54+
if __name__ == "__main__":
55+
main()

integrations/selenium_grid/grid-hub

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
#
4-
# Usage: grid-hub {start|stop}
4+
# Usage: grid-hub {start|stop|restart}
55
#
66

77
source $(dirname $0)/font_color
@@ -10,7 +10,7 @@ EXPECTED_ARGS=1
1010
E_BADARGS=65
1111

1212
DO_showUsage() {
13-
echo "Usage: $(basename $0) {start|stop}"
13+
echo "Usage: $(basename $0) {start|stop|restart}"
1414
exit $E_BADARGS
1515
}
1616

@@ -20,8 +20,21 @@ fi
2020

2121
################################################################################
2222

23-
WEBDRIVER_SERVER_JAR=./selenium-server-standalone.jar
24-
WEBDRIVER_HUB_PARAMS="-role hub -port 4444"
23+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
24+
GRID_HUB_VERBOSE_LOGS_FILE=${DIR}/verbose_hub_server.dat
25+
26+
GRID_HUB_VERBOSE_LOGS="False"
27+
if [ -f $GRID_HUB_VERBOSE_LOGS_FILE ]; then
28+
GRID_HUB_VERBOSE_LOGS=$(cat $GRID_HUB_VERBOSE_LOGS_FILE)
29+
fi
30+
31+
VERBOSITY_STRING="-Djava.util.logging.config.file=test/logging.properties "
32+
if [ "$GRID_HUB_VERBOSE_LOGS" == "True" ]; then
33+
VERBOSITY_STRING=""
34+
fi
35+
36+
WEBDRIVER_SERVER_JAR=${DIR}/selenium-server-standalone.jar
37+
WEBDRIVER_HUB_PARAMS="-role hub -timeout 30 -browserTimeout 60 -port 4444"
2538
WEBDRIVER_HUB_PIDFILE="/tmp/webdriver_hub.pid"
2639

2740
if [ ! -f $WEBDRIVER_SERVER_JAR ]; then
@@ -31,35 +44,45 @@ fi
3144

3245
case "$1" in
3346
start)
34-
echo "Starting Selenium-WebDriver Grid hub..."
47+
echo "Starting Selenium-WebDriver Grid Hub..."
3548
if [ -f $WEBDRIVER_HUB_PIDFILE ]; then
36-
echo "${FAIL_MSG} Selenium-WebDriver Grid hub already running with PID $(cat $WEBDRIVER_HUB_PIDFILE). Run 'grid-hub stop' or 'grid-hub restart'."
49+
echo "${FAIL_MSG} Selenium-WebDriver Grid Hub already running with PID $(cat $WEBDRIVER_HUB_PIDFILE). Run 'grid-hub stop' or 'grid-hub restart'."
50+
echo ""
51+
echo "Grid Hub console: http://127.0.0.1:4444/grid/console"
52+
echo ""
3753
exit 1
3854
else
39-
START_HUB_CMD="java -Djava.util.logging.config.file=test/logging.properties -jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_HUB_PARAMS}"
55+
# START_HUB_CMD="java -Djava.util.logging.config.file=test/logging.properties -jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_HUB_PARAMS}"
56+
START_HUB_CMD="java ${VERBOSITY_STRING}-jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_HUB_PARAMS}"
57+
echo ""
58+
echo $START_HUB_CMD
59+
echo ""
4060
$START_HUB_CMD &
4161
PID=$!
4262
echo $PID > "${WEBDRIVER_HUB_PIDFILE}"
43-
echo "${SUCCESS_MSG} Selenium-WebDriver Grid hub started successfully."
44-
echo "To see full log output, remove the java.util.logging.config.file parameter from script/grid-hub"
63+
echo "${SUCCESS_MSG} Selenium-WebDriver Grid Hub started successfully."
64+
echo ""
65+
echo "Grid Hub console: http://127.0.0.1:4444/grid/console"
66+
echo ""
67+
# echo "To see full log output, remove the java.util.logging.config.file parameter from script/grid-hub"
4568
fi
4669
;;
4770
stop)
48-
echo "Stopping Selenium-WebDriver Grid hub..."
71+
echo "Stopping Selenium-WebDriver Grid Hub..."
4972
if [ -f $WEBDRIVER_HUB_PIDFILE ]; then
5073
PID=$(cat $WEBDRIVER_HUB_PIDFILE)
5174
kill $PID
5275
rm $WEBDRIVER_HUB_PIDFILE
5376
sleep 1
5477
if [[ $(ps -A | egrep "^${PID}") ]]; then
55-
echo "${FAIL_MSG} Tried to kill the hub with PID ${PID}, but was unsuccessful. You need to kill it with something stronger, like 'kill -9'"
78+
echo "${FAIL_MSG} Tried to kill the Grid Hub with PID ${PID}, but was unsuccessful. You need to kill it with something stronger, like 'kill -9'"
5679
exit 1
5780
else
58-
echo "${SUCCESS_MSG} Selenium-WebDriver Grid hub stopped successfully."
81+
echo "${SUCCESS_MSG} Selenium-WebDriver Grid Hub stopped successfully."
5982
exit 0
6083
fi
6184
else
62-
echo "${SUCCESS_MSG} Selenium-WebDriver Grid hub has already been stopped."
85+
echo "${SUCCESS_MSG} Selenium-WebDriver Grid Hub has already been stopped."
6386
exit 0
6487
fi
6588
;;

integrations/selenium_grid/grid-node

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
#
4-
# Usage: grid-node {start|stop}
4+
# Usage: grid-node {start|stop|restart}
55
#
66

77
source $(dirname $0)/font_color
@@ -10,7 +10,7 @@ EXPECTED_ARGS=1
1010
E_BADARGS=65
1111

1212
DO_showUsage() {
13-
echo "Usage: $(basename $0) {start|stop}"
13+
echo "Usage: $(basename $0) {start|stop|restart}"
1414
exit $E_BADARGS
1515
}
1616

@@ -20,8 +20,28 @@ fi
2020

2121
################################################################################
2222

23-
WEBDRIVER_SERVER_JAR=./selenium-server-standalone.jar
24-
WEBDRIVER_NODE_PARAMS="-role webdriver -hubHost 127.0.0.1 -hubPort 4444 -host 127.0.0.1 -browser browserName=firefox,maxInstances=5,version=ANY,platform=ANY -browser browserName=chrome,maxInstances=5,version=ANY,platform=ANY"
23+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
24+
GRID_HUB_SERVER_IP_FILE=${DIR}/ip_of_grid_hub.dat
25+
GRID_NODE_VERBOSE_LOGS_FILE=${DIR}/verbose_node_server.dat
26+
27+
GRID_HUB_SERVER_IP="127.0.0.1"
28+
if [ -f $GRID_HUB_SERVER_IP_FILE ]; then
29+
GRID_HUB_SERVER_IP=$(cat $GRID_HUB_SERVER_IP_FILE)
30+
fi
31+
32+
GRID_NODE_VERBOSE_LOGS="False"
33+
if [ -f $GRID_NODE_VERBOSE_LOGS_FILE ]; then
34+
GRID_NODE_VERBOSE_LOGS=$(cat $GRID_NODE_VERBOSE_LOGS_FILE)
35+
fi
36+
37+
VERBOSITY_STRING="-Djava.util.logging.config.file=test/logging.properties "
38+
if [ "$GRID_NODE_VERBOSE_LOGS" == "True" ]; then
39+
VERBOSITY_STRING=""
40+
fi
41+
42+
WEBDRIVER_SERVER_JAR=${DIR}/selenium-server-standalone.jar
43+
# WEBDRIVER_NODE_PARAMS="-role node -hubHost ${GRID_HUB_SERVER_IP} -hubPort 4444 -host 127.0.0.1 -browser browserName=chrome,maxInstances=5,version=ANY,platform=ANY -browser browserName=firefox,maxInstances=5,version=ANY,platform=ANY -browser browserName=MicrosoftEdge,maxInstances=1,version=ANY,platform=WIN10"
44+
WEBDRIVER_NODE_PARAMS="-role node -hub http://${GRID_HUB_SERVER_IP}:4444/grid/register -browser browserName=chrome,maxInstances=5,version=ANY,seleniumProtocol=WebDriver -browser browserName=firefox,maxInstances=5,version=ANY,seleniumProtocol=WebDriver -browser browserName=MicrosoftEdge,maxInstances=1,version=ANY,platform=WIN10,seleniumProtocol=WebDriver"
2545
WEBDRIVER_NODE_PIDFILE="/tmp/webdriver_node.pid"
2646

2747
if [ ! -f $WEBDRIVER_SERVER_JAR ]; then
@@ -36,12 +56,19 @@ case "$1" in
3656
echo "${FAIL_MSG} Selenium-WebDriver Grid node already running with PID $(cat $WEBDRIVER_NODE_PIDFILE). Run 'grid-node stop' or 'grid-node restart'."
3757
exit 1
3858
else
39-
START_NODE_CMD="java -Djava.util.logging.config.file=test/logging.properties -jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_NODE_PARAMS}"
59+
# START_NODE_CMD="java -Djava.util.logging.config.file=test/logging.properties -jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_NODE_PARAMS}"
60+
START_NODE_CMD="java ${VERBOSITY_STRING}-jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_NODE_PARAMS}"
61+
echo ""
62+
echo $START_NODE_CMD
63+
echo ""
4064
$START_NODE_CMD &
4165
PID=$!
4266
echo $PID > "${WEBDRIVER_NODE_PIDFILE}"
4367
echo "${SUCCESS_MSG} Selenium-WebDriver Grid node started successfully."
44-
echo "To see full log output, remove the java.util.logging.config.file parameter from script/grid-node"
68+
echo ""
69+
echo "Browser Sessions: http://${GRID_HUB_SERVER_IP}:5555/wd/hub/static/resource/hub.html"
70+
echo ""
71+
# echo "To see full log output, remove the java.util.logging.config.file parameter from script/grid-node"
4572
fi
4673
;;
4774
stop)

0 commit comments

Comments
 (0)