Skip to content

Commit 3b9c2e2

Browse files
authored
Improve wait for grid script (#1901)
1 parent e7f6c5b commit 3b9c2e2

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -999,23 +999,35 @@ A common problem known in docker is that a running container does not always mea
999999
A simple way to tackle this is by using a "wait-for-it" script, more information can be seen [here](https://docs.docker.com/compose/startup-order/).
10001000

10011001
The following script is an example of how this can be done using bash, but the same principle applies if you want to do this with the programming language used to write the tests.
1002+
In the example below, the script will poll the status endpoint every second. If the grid does not become ready within 30 seconds, the script will exit with an error code.
10021003

10031004
```bash
10041005
#!/bin/bash
10051006
# wait-for-grid.sh
10061007

10071008
set -e
1008-
1009-
cmd="$@"
1010-
1011-
while ! curl -sSL "http://localhost:4444/wd/hub/status" 2>&1 \
1012-
| jq -r '.value.ready' 2>&1 | grep "true" >/dev/null; do
1013-
echo 'Waiting for the Grid'
1014-
sleep 1
1009+
url="http://localhost:4444/wd/hub/status"
1010+
wait_interval_in_seconds=1
1011+
max_wait_time_in_seconds=30
1012+
end_time=$((SECONDS + max_wait_time_in_seconds))
1013+
time_left=$max_wait_time_in_seconds
1014+
1015+
while [ $SECONDS -lt $end_time ]; do
1016+
response=$(curl -sL "$url" | jq -r '.value.ready')
1017+
if [ -n "$response" ] && [ "$response" ]; then
1018+
echo "Selenium Grid is up - executing tests"
1019+
break
1020+
else
1021+
echo "Waiting for the Grid. Sleeping for $wait_interval_in_seconds second(s). $time_left seconds left until timeout."
1022+
sleep $wait_interval_in_seconds
1023+
time_left=$((time_left - wait_interval_in_seconds))
1024+
fi
10151025
done
10161026

1017-
>&2 echo "Selenium Grid is up - executing tests"
1018-
exec $cmd
1027+
if [ $SECONDS -ge $end_time ]; then
1028+
echo "Timeout: The Grid was not started within $max_wait_time_in_seconds seconds."
1029+
exit 1
1030+
fi
10191031
```
10201032
> Will require `jq` installed via `apt-get`, else the script will keep printing `Waiting` without completing the execution.
10211033

0 commit comments

Comments
 (0)