forked from open-xml-templating/docxtemplater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdriver.bash
executable file
·143 lines (133 loc) · 2.96 KB
/
webdriver.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
set -euo pipefail
if ! [ -f test/mocha.html ]
then
echo "test/mocha.html does not exist" 1>&2
exit 1
fi
port4444used() {
netstat -tnlp 2>/dev/null | grep 4444 >/dev/null
}
install_selenium() {
rm node_modules/selenium-standalone/.selenium/*driver -rf
echo "Installing selenium"
selenium-standalone install 2>&1 | tee /tmp/selenium_install.log
if grep --quiet -E '(Error)|(Could not download)' </tmp/selenium_install.log
then
echo "Error downloading selenium drivers"
exit 1
fi
rsync node_modules/selenium-standalone/.selenium/ "$HOME/tmp/.selenium/"
}
export GRAY='[90m'
export NORMAL='[m'
selenium_pid=""
start_selenium() {
{ selenium-standalone start 2>&1 | tee /tmp/webdriver.log | sed -E "s/^.*$/$GRAY\0$NORMAL/g" ; } &
selenium_pid="$!"
}
stop_selenium() {
if [ "$selenium_pid" != "" ]
then
while true
do
kill "$selenium_pid" 1>/dev/null 2>&1 || break
sleep 1
done
fi
}
force_stop_selenium() {
fuser -k 4444/tcp || true
}
cleanup() {
stop_selenium
}
trap "cleanup" EXIT INT
BROWSER="${BROWSER:-CHROME|FIREFOX|}"
PATH="$PATH:./node_modules/.bin/"
selenium_cache_dir="$HOME/tmp/.selenium"
selenium_dir="node_modules/selenium-standalone/.selenium"
if grep '|' <<<"$BROWSER" >/dev/null
then
messages=""
while read -r -d '|' browser
do
result=0
BROWSER="$browser" ./webdriver.bash || result="$?"
if [ "$result" != "0" ]
then
messages="$messages"$'\n'"Fail for $browser"
fi
done <<<"$BROWSER"
if [ "$messages" != "" ]
then
echo "$messages"
exit 1
fi
exit 0
fi
export -f stop_selenium
isempty() {
[ ! "$(ls -A "$1")" ]
}
if [ "$BROWSER" != "SAUCELABS" ]
then
retries=2
while [ "$retries" -gt 0 ]
do
retries="$((retries - 1))"
if port4444used
then
echo "Using existing selenium for $BROWSER"
else
if ! [ -d "$selenium_dir" ] || isempty "$selenium_dir"
then
rm -rf "$selenium_dir"
mkdir -p "$HOME/tmp/"
if [ -d "$selenium_cache_dir" ] && ! isempty "$selenium_cache_dir"
then
echo "Copying selenium from cache"
cp -r "$selenium_cache_dir" node_modules/selenium-standalone/.selenium
else
install_selenium
fi
fi
echo "Starting selenium"
start_selenium
while ! port4444used;
do
if grep 'Missing.*driver' </tmp/webdriver.log
then
echo "missing driver"
rm /tmp/webdriver.log
force_stop_selenium
install_selenium
start_selenium
fi
sleep 0.5
done
fi
result=0
{ FORCE_COLOR=1 node webdriver.js | tee /tmp/test.log; } || result="$?"
if [ "$result" = "0" ]
then
exit 0
fi
if grep 'This version of ChromeDriver only supports Chrome version ' </tmp/test.log
then
exit 1
fi
if grep 'This version of ChromeDriver only supports Chrome version ' </tmp/test.log ||
grep 'Error: Failed to create session.' </tmp/test.log
then
echo "Retrying by restarting selenium: $retries"
force_stop_selenium
install_selenium
start_selenium
else
exit "$result"
fi
done
else
bash webdriver-saucelabs.bash
fi