-
Notifications
You must be signed in to change notification settings - Fork 23
/
test-browser.sh
executable file
·94 lines (77 loc) · 1.57 KB
/
test-browser.sh
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
#!/bin/bash
## Run tests against a local browser on OSX.
set -euo pipefail
PORT=8000
DOWNLOADS=~/Downloads
PIDFILE=/tmp/http-cache-test-server.pid
function usage {
if [[ -n "${1}" ]]; then
echo "${1}"
fi
echo "Usage: $0 [ browser-name ... ]" >&2
}
function run {
BROWSERS=( "$@" )
# start test server
npm run --silent server --port=$PORT --pidfile=$PIDFILE &
trap cleanup EXIT
sleep 2
for browser in "${BROWSERS[@]}"
do
test_browser "${browser}"
done
}
function cleanup {
# stop test server
kill "$(cat $PIDFILE)" > /dev/null 2>&1
rm $PIDFILE
}
function test_browser {
BROWSER=${1}
URL="http://localhost:${PORT}/test-browser.html?auto=1&download=${BROWSER}"
case ${BROWSER} in
safari)
BROWSER_CMD="/Applications/Safari.app"
;;
firefox)
BROWSER_CMD="/Applications/Firefox.app"
;;
chrome)
BROWSER_CMD="/Applications/Google Chrome.app"
;;
*)
usage "Browser ${BROWSER} not recognised."
return
;;
esac
# remove target file
TARGET="${DOWNLOADS}/${BROWSER}.json"
rm -f "${TARGET}"
# run tests
open -g -a "${BROWSER_CMD}" "${URL}"
# wait for the target to be created
i=0
while [ ! -f "${TARGET}" ]
do
sleep 1
i=$((i+1))
if [ "$i" -gt "60" ] ; then
echo "Timeout." >&2
exit 1
fi
done
sleep 1
if [ -f "${TARGET}" ] ; then
mv "${TARGET}" results/
fi
}
OS=$(uname)
if [[ "${OS}" != "Darwin" ]]; then
usage "This script must be run on Mac OSX."
exit 1
fi
if [[ $# -eq 0 ]]; then
run safari firefox chrome
else
run "$@"
fi