Skip to content

Commit ef3072d

Browse files
major tidy up, moving the download logic into a separate script that is not run in the background, checking for required pacakges
1 parent 7a4e95e commit ef3072d

File tree

5 files changed

+200
-136
lines changed

5 files changed

+200
-136
lines changed

bin/download-binaries.bash

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env bash
2+
DIR=$(dirname $(readlink -f "$0"))
3+
cd $DIR;
4+
set -e
5+
set -u
6+
set -o pipefail
7+
standardIFS="$IFS"
8+
IFS=$'\n\t'
9+
echo "
10+
Downloading Binaries for Selenium
11+
"
12+
13+
majorVersion=2.53
14+
version=${majorVersion}.0
15+
jarFile=selenium-server-standalone-${version}.jar
16+
17+
18+
chromedriverVersion=`curl http://chromedriver.storage.googleapis.com/LATEST_RELEASE`
19+
chromedriverFile=chromedriver-${chromedriverVersion}
20+
currentChromedriverVersionFile=current_chromedriver_version.txt
21+
22+
firefoxdriverVersion=0.15.0
23+
firefoxdriverFile=geckodriver
24+
25+
cd $DIR/../binaries/
26+
if [[ $? != 0 ]]
27+
then
28+
echo "Failed cd-ing into the the binaries folder, aborting"
29+
exit 1
30+
fi
31+
32+
# Making sure that the chrome driver is up to date
33+
if [ -f ${currentChromedriverVersionFile} ]
34+
then
35+
currentChromedriverVersion=`cat ${currentChromedriverVersionFile}`
36+
else
37+
currentChromedriverVersion=false
38+
fi
39+
40+
echo ${chromedriverVersion} > ${currentChromedriverVersionFile}
41+
42+
if [[ ${currentChromedriverVersion} != ${chromedriverVersion} && -f ${chromedriverFile} ]]
43+
then
44+
rm -f ${chromedriverFile}
45+
rm -f chromedriver_linux64.zip
46+
fi
47+
48+
## Host File bug sanity check
49+
grep -P '127.0.0.1\s*localhost' /etc/hosts > /dev/null
50+
if [[ $? != 0 ]]
51+
then
52+
echo "
53+
54+
WARNING
55+
56+
Selenium won't work unless your hosts file localhost aliases start explictly with:
57+
58+
127.0.0.1 localhost ...other aliases here
59+
60+
Please edit your hosts file and try again
61+
62+
See:
63+
https://code.google.com/p/selenium/issues/detail?id=3280
64+
65+
Your hosts line is:
66+
"
67+
grep '127.0.0.1' /etc/hosts
68+
echo;
69+
exit 1
70+
fi
71+
72+
if [ ! -f $jarFile ]
73+
then
74+
echo "Selenium JAR file not found - trying to wget the file"
75+
downloadUrl="http://selenium-release.storage.googleapis.com/${majorVersion}/selenium-server-standalone-${version}.jar"
76+
echo $downloadUrl
77+
set +e
78+
wget $downloadUrl
79+
if [[ $? != 0 ]]
80+
then
81+
echo "Failed downloading, please grab it manually"
82+
exit 1
83+
fi
84+
set -e
85+
fi
86+
87+
if [ ! -f $chromedriverFile ]
88+
then
89+
echo "Chromedriver file not found - trying to wget the file"
90+
downloadUrl="http://chromedriver.storage.googleapis.com/${chromedriverVersion}/chromedriver_linux64.zip"
91+
echo $downloadUrl
92+
set +e
93+
wget $downloadUrl
94+
if [[ $? != 0 ]]
95+
then
96+
echo "Failed downloading, please grab it manually"
97+
exit 1
98+
fi
99+
set -e
100+
if [ -f chromedriver ]
101+
then
102+
rm chromedriver
103+
fi
104+
unzip chromedriver_linux64.zip
105+
mv chromedriver $chromedriverFile
106+
fi
107+
108+
if [ ! -f $firefoxdriverFile ] && [[ "$@" =~ .*firefox.* ]]
109+
then
110+
if [ $(echo "$majorVersion < 3.3" | bc -l) == 1 ]
111+
then
112+
echo "WARNING: the latest geckodriver requires selenium 3.3 and above";
113+
exit 1
114+
fi
115+
116+
echo "Firefoxdirver file not found - trying to wget the file"
117+
118+
downloadUrl="https://github.com/mozilla/geckodriver/releases/download/v${firefoxdriverVersion}/geckodriver-v${firefoxdriverVersion}-linux64.tar.gz"
119+
echo $downloadUrl
120+
set +e
121+
wget $downloadUrl
122+
if [[ $? != 0 ]]
123+
then
124+
echo "Failed downloading, please grab it manually"
125+
exit 1
126+
fi
127+
set -e
128+
tar -xzvf geckodriver-v${firefoxdriverVersion}-linux64.tar.gz
129+
mv geckodriver $firefoxdriverFile
130+
fi
131+
132+
echo "
133+
DONE Downloading Binaries for Selenium
134+
"

bin/selenium-background-run.bash

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
#!/bin/bash
2-
SOURCE="${BASH_SOURCE[0]}"
3-
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
4-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
5-
SOURCE="$(readlink "$SOURCE")"
6-
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
7-
done
8-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
1+
#!/usr/bin/env bash
2+
DIR=$(dirname $(readlink -f "$0"))
93
cd $DIR
104

5+
source ./download-binaries.bash
6+
117
echo '' > nohup.out
12-
nohup ./selenium-run.bash "$@" &
8+
echo "
9+
Starting Selenium in the Background
10+
"
11+
nohup $DIR/selenium-run.bash "$@" &
12+
1313
sleep 2
1414

1515
cat nohup.out
16-
echo "
1716

18-
Selenium Running"
17+
echo "
18+
Selenium Running
19+
"

bin/selenium-run.bash

Lines changed: 52 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,70 @@
1-
#!/bin/bash
2-
#set -x
3-
4-
MAJOR_VERSION=2.53
5-
VERSION=${MAJOR_VERSION}.0
6-
JAR_FILE=selenium-server-standalone-${VERSION}.jar
7-
8-
9-
CHROMEDRIVER_VERSION=`curl http://chromedriver.storage.googleapis.com/LATEST_RELEASE`
10-
CHROMEDRIVER_FILE=chromedriver-${CHROMEDRIVER_VERSION}
11-
CURRENT_CHROMEDRIVER_VERSION_FILE=current_chromedriver_version.txt
12-
13-
FIREFOXDRIVER_VERSION=0.15.0
14-
FIREFOXDRIVER_FILE=geckodriver
15-
16-
SOURCE="${BASH_SOURCE[0]}"
17-
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
18-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
19-
SOURCE="$(readlink "$SOURCE")"
20-
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
21-
done
22-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
23-
cd $DIR/../binaries/
24-
if [[ $? != 0 ]]
25-
then
26-
echo "Failed cd-ing into the the binaries folder, aborting"
27-
exit 1
28-
fi
29-
30-
# Making sure that the chrome driver is up to date
31-
if [ -f ${CURRENT_CHROMEDRIVER_VERSION_FILE} ]
32-
then
33-
CURRENT_CHROMEDRIVER_VERSION=`cat ${CURRENT_CHROMEDRIVER_VERSION_FILE}`
34-
else
35-
CURRENT_CHROMEDRIVER_VERSION=false
36-
fi
37-
38-
echo ${CHROMEDRIVER_VERSION} > ${CURRENT_CHROMEDRIVER_VERSION_FILE}
39-
40-
if [[ ${CURRENT_CHROMEDRIVER_VERSION} != ${CHROMEDRIVER_VERSION} && -f ${CHROMEDRIVER_FILE} ]]
41-
then
42-
rm -f ${CHROMEDRIVER_FILE}
43-
rm -f chromedriver_linux64.zip
44-
fi
45-
46-
## Host File bug sanity check
47-
grep -P '127.0.0.1\s*localhost' /etc/hosts > /dev/null
48-
if [[ $? != 0 ]]
49-
then
50-
echo "
51-
52-
WARNING
53-
54-
Selenium won't work unless your hosts file localhost aliases start explictly with:
55-
56-
127.0.0.1 localhost ...other aliases here
57-
58-
Please edit your hosts file and try again
59-
60-
See:
61-
https://code.google.com/p/selenium/issues/detail?id=3280
62-
63-
Your hosts line is:
64-
"
65-
grep '127.0.0.1' /etc/hosts
66-
echo;
67-
exit 1
68-
fi
69-
70-
if [ ! -f $JAR_FILE ]
71-
then
72-
echo "Selenium JAR file not found - trying to wget the file"
73-
DOWNLOAD_URL="http://selenium-release.storage.googleapis.com/${MAJOR_VERSION}/selenium-server-standalone-${VERSION}.jar"
74-
echo $DOWNLOAD_URL
75-
wget $DOWNLOAD_URL
76-
if [[ $? != 0 ]]
77-
then
78-
echo "Failed downloading, please grab it manually"
79-
exit 1
80-
fi
81-
fi
82-
83-
if [ ! -f $CHROMEDRIVER_FILE ]
84-
then
85-
echo "Chromedriver file not found - trying to wget the file"
86-
DOWNLOAD_URL="http://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip"
87-
echo $DOWNLOAD_URL
88-
wget $DOWNLOAD_URL
89-
if [[ $? != 0 ]]
1+
#!/usr/bin/env bash
2+
DIR=$(dirname $(readlink -f "$0"))
3+
cd $DIR;
4+
set -e
5+
set -u
6+
set -o pipefail
7+
standardIFS="$IFS"
8+
IFS=$'\n\t'
9+
echo "
10+
===========================================
11+
$(hostname) $0 $@
12+
===========================================
13+
"
14+
# Error Handling
15+
backTraceExit () {
16+
local err=$?
17+
set +o xtrace
18+
local code="${1:-1}"
19+
printf "\n\nError in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}'\n\n exited with status: \n\n$err\n\n"
20+
# Print out the stack trace described by $function_stack
21+
if [ ${#FUNCNAME[@]} -gt 2 ]
9022
then
91-
echo "Failed downloading, please grab it manually"
92-
exit 1
23+
echo "Call tree:"
24+
for ((i=1;i<${#FUNCNAME[@]}-1;i++))
25+
do
26+
echo " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)"
27+
done
9328
fi
94-
if [ -f chromedriver ]
29+
echo "Exiting with status ${code}"
30+
exit "${code}"
31+
}
32+
trap 'backTraceExit' ERR
33+
set -o errtrace
34+
missingPackages=false
35+
for package in unzip java
36+
do
37+
if [[ "" == "$(which $package)" ]]
9538
then
96-
rm chromedriver
39+
echo "Package $package is missing";
40+
missingPackages=true;
9741
fi
98-
unzip chromedriver_linux64.zip
99-
mv chromedriver $CHROMEDRIVER_FILE
100-
fi
42+
done
10143

102-
if [ ! -f $FIREFOXDRIVER_FILE ] && [[ "$@" =~ .*firefox.* ]]
44+
if [[ "false" != "$missingPackages" ]]
10345
then
104-
if [ $(echo "$MAJOR_VERSION < 3.3" | bc -l) == 1 ]
105-
then
106-
echo "WARNING: the latest geckodriver requires selenium 3.3 and above";
107-
exit 1
108-
fi
46+
echo "Packages are missing, please install them"
47+
exit 1
48+
fi
10949

110-
echo "Firefoxdirver file not found - trying to wget the file"
50+
jarFile=${1:-'false'}
11151

112-
DOWNLOAD_URL="https://github.com/mozilla/geckodriver/releases/download/v${FIREFOXDRIVER_VERSION}/geckodriver-v${FIREFOXDRIVER_VERSION}-linux64.tar.gz"
113-
echo $DOWNLOAD_URL
114-
wget $DOWNLOAD_URL
115-
if [[ $? != 0 ]]
116-
then
117-
echo "Failed downloading, please grab it manually"
118-
exit 1
119-
fi
120-
if [ -f $FIREFOXDRIVER_FILE ]
121-
then
122-
rm $FIREFOXDRIVER_FILE
123-
fi
124-
tar -xzvf geckodriver-v${FIREFOXDRIVER_VERSION}-linux64.tar.gz
125-
mv geckodriver $FIREFOXDRIVER_FILE
52+
if [[ "false" == "$jarFile" ]]
53+
then
54+
source ./download-binaries.bash
12655
fi
12756

128-
echo "Starting Selenium"
57+
echo "Now Starting Selenium"
12958

13059
echo "Killing if already running:"
131-
source $DIR/selenium-stop.bash
60+
bash $DIR/selenium-stop.bash
13261

13362

13463
if [[ "$@" =~ .*firefox.* ]]
13564
then
136-
echo "starting firefox selenium
137-
"
138-
java -jar $JAR_FILE
65+
echo "starting firefox selenium"
66+
java -jar $jarFile
13967
else
140-
echo "starting chrome selenium
141-
"
142-
java -jar $JAR_FILE -Dwebdriver.chrome.driver=${CHROMEDRIVER_FILE}
68+
echo "starting chrome selenium"
69+
java -jar $jarFile -Dwebdriver.chrome.driver=${chromedriverFile}
14370
fi

binaries/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

binaries/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)