mahara_behat.sh 4.58 KB
Newer Older
Son Nguyen's avatar
Son Nguyen committed
1
2
#!/bin/bash

3
4
5
# Quit on error
set -e

6
7
8
9
10
11
# Get action and Mahara dir
ACTION=$1
SCRIPTPATH=`readlink -f "${BASH_SOURCE[0]}"`
MAHARAROOT=`dirname $( dirname $( dirname "$SCRIPTPATH" ))`
SERVER=0

12
# Wait and check if the selenium server is running in maximum 15 seconds
Son Nguyen's avatar
Son Nguyen committed
13
function is_selenium_running {
14
15
16
17
18
19
20
21
    for i in `seq 1 15`; do
        sleep 1
        res=$(curl -o /dev/null --silent --write-out '%{http_code}\n' http://localhost:4444/wd/hub/status)
        if [ $res == "200" ]; then
            return 0;
        fi
    done
    return 1;
Son Nguyen's avatar
Son Nguyen committed
22
23
}

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function cleanup {
    echo "Shutdown Selenium"
    curl -o /dev/null --silent http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

    if [[ $SERVER ]]
    then
        echo "Shutdown PHP server"
        kill $SERVER
    fi

    if [[ $1 ]]
    then
        exit $1
    else
        exit 255
    fi
}

Son Nguyen's avatar
Son Nguyen committed
42
43
44
45
46
47
48
49
50
# Check we are not running as root for some weird reason
if [[ "$USER" = "root" ]]
then
    echo "This script should not be run as root"
    exit 1
fi

cd $MAHARAROOT

51
52
53
54
# Trap errors so we can cleanup
trap cleanup ERR
trap cleanup INT

Son Nguyen's avatar
Son Nguyen committed
55
56
57
58
59
60
61
62
if [ "$ACTION" = "action" ]
then

    # Wrap the util.php script

    PERFORM=$2
    php htdocs/testing/frameworks/behat/cli/util.php --$PERFORM

63
elif [ "$ACTION" = "run" -o "$ACTION" = "runheadless" -o "$ACTION" = "rundebug" -o "$ACTION" = "runfresh" -o $ACTION = 'rundebugheadless' ]
Son Nguyen's avatar
Son Nguyen committed
64
65
then

66
67
68
69
70
71
    if [ $ACTION = 'runheadless' -o "$ACTION" = "runfresh" ]
    then
        echo "Drop the old test site if exist"
        php htdocs/testing/frameworks/behat/cli/util.php --drop
    fi

Son Nguyen's avatar
Son Nguyen committed
72
73
74
75
76
77
78
79
80
81
82
    # Initialise the behat environment
    php htdocs/testing/frameworks/behat/cli/init.php

    # Run the Behat tests themselves (after any intial setup)
    TAGS=$2

    if is_selenium_running; then
        echo "Selenium is running"
    else
        echo "Start Selenium..."

83
        SELENIUM_VERSION_MAJOR=2.45
84
        SELENIUM_VERSION_MINOR=0
Son Nguyen's avatar
Son Nguyen committed
85
86
87
88
89
90
91
92
93
94
95

        SELENIUM_FILENAME=selenium-server-standalone-$SELENIUM_VERSION_MAJOR.$SELENIUM_VERSION_MINOR.jar
        SELENIUM_PATH=./test/behat/$SELENIUM_FILENAME

        # If no Selenium installed, download it
        if [ ! -f $SELENIUM_PATH ]; then
            echo "Downloading Selenium..."
            wget -q -O $SELENIUM_PATH http://selenium-release.storage.googleapis.com/$SELENIUM_VERSION_MAJOR/$SELENIUM_FILENAME
            echo "Downloaded"
        fi

96
        if [ $ACTION = 'runheadless' -o $ACTION = 'rundebugheadless' ]
97
98
99
100
101
102
103
104
105
        then
            # we want to run selenium headless on a different display - this allows for that ;)
            echo "Starting Xvfb ..."
            Xvfb :10 -ac > /dev/null 2>&1 & echo "PID [$!]"

            DISPLAY=:10 nohup java -jar $SELENIUM_PATH > /dev/null 2>&1 & echo $!
        else
            java -jar $SELENIUM_PATH &> /dev/null &
        fi
Son Nguyen's avatar
Son Nguyen committed
106
107
108
109
110
111
112
113
114
115

        if is_selenium_running; then
            echo "Selenium started"
        else
            echo "Selenium can't be started"
            exit 1
        fi
    fi

    echo "Start PHP server"
116
    php --server localhost:8000 --docroot $MAHARAROOT/htdocs &>/dev/null &
Son Nguyen's avatar
Son Nguyen committed
117
118
119
120
121
    SERVER=$!

    BEHATCONFIGFILE=`php htdocs/testing/frameworks/behat/cli/util.php --config`
    echo "Run Behat..."

122
    OPTIONS=''
Son Nguyen's avatar
Son Nguyen committed
123
124
125
    if [ "$TAGS" ]
    then
        echo "Only run tests with the tag: $TAGS"
126
        OPTIONS=$OPTIONS" --tags "$TAGS
Son Nguyen's avatar
Son Nguyen committed
127
128
129
130
131
132
133
134
    else
        echo "Run all tests"
    fi

    echo
    echo "=================================================="
    echo

135
    if [ $ACTION = 'rundebug' -o $ACTION = 'rundebugheadless' ]
Son Nguyen's avatar
Son Nguyen committed
136
    then
137
        OPTIONS=$OPTIONS" --format=pretty"
Son Nguyen's avatar
Son Nguyen committed
138
139
    fi

140
141
    ./external/vendor/bin/behat --config $BEHATCONFIGFILE $OPTIONS

Son Nguyen's avatar
Son Nguyen committed
142
143
144
145
    echo
    echo "=================================================="
    echo
    echo "Shutdown"
146
    cleanup 0
Son Nguyen's avatar
Son Nguyen committed
147
148
149
150
151
152
153
154
155
156
else
    # Help text if we got an unexpected (or empty) first param
    echo "Expected something like one of the following:"
    echo
    echo "# Run all tests:"
    echo "mahara_behat run"
    echo ""
    echo "# Run tests with specific tag:"
    echo "mahara_behat run @tagname"
    echo ""
157
158
159
160
    echo "# Run tests with extra debug output:"
    echo "mahara_behat rundebug"
    echo "mahara_behat rundebug @tagname"
    echo ""
161
162
163
164
165
166
    echo "# Run in headless mode (requires xvfb):"
    echo "mahara_behat runheadless"
    echo ""
    echo "# Run in headless mode with extra debug output:"
    echo "mahara_behat rundebugheadless"
    echo ""
Son Nguyen's avatar
Son Nguyen committed
167
168
169
170
171
172
173
174
175
176
    echo "# Enable test site:"
    echo "mahara_behat action enable"
    echo ""
    echo "# Disable test site:"
    echo "mahara_behat action disable"
    echo ""
    echo "# List other actions you can perform:"
    echo "mahara_behat action help"
    exit 1
fi