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

3
4
# Get action and Mahara dir
ACTION=$1
5
REPORT=$3
6
7
SCRIPTPATH=`readlink -f "${BASH_SOURCE[0]}"`
MAHARAROOT=`dirname $( dirname $( dirname "$SCRIPTPATH" ))`
8
BEHATROOT=`grep -v '^//\|^#' $MAHARAROOT/htdocs/config.php | grep behat_dataroot | grep -o "['\"].*['\"];" | sed "s/['\";]//g"`
9
SERVER=0
10
test -z $SELENIUM_PORT && export SELENIUM_PORT=4444
11
12
13
14
15
test -z $PHP_PORT && export PHP_PORT=8000
test -z $XVFB_PORT && export XVFB_PORT=10

echo "S: $SELENIUM_PORT"
echo "P: $PHP_PORT"
16

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

29
30
function cleanup {
    echo "Shutdown Selenium"
31
    curl -o /dev/null --silent http://localhost:${SELENIUM_PORT}/selenium-server/driver/?cmd=shutDownSeleniumServer
32

33
34
35
36
37
    if [[ $REPORT ]]
    then
        xdg-open file://${BEHATROOT}/behat/html_results/index.html
    fi

38
39
40
41
42
43
44
45
46
47
48
49
    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
50
51
52

    echo "Disable behat test environment"
    php htdocs/testing/frameworks/behat/cli/util.php -d
53
54
}

Son Nguyen's avatar
Son Nguyen committed
55
56
57
58
59
60
61
62
63
# 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

64
65
66
67
# Trap errors so we can cleanup
trap cleanup ERR
trap cleanup INT

Son Nguyen's avatar
Son Nguyen committed
68
69
70
71
72
73
74
75
if [ "$ACTION" = "action" ]
then

    # Wrap the util.php script

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

76
elif [ "$ACTION" = "run" -o "$ACTION" = "runheadless" -o "$ACTION" = "rundebug" -o "$ACTION" = "runfresh" -o "$ACTION" = "rundebugheadless" ] && [ "$2" != "html" ]
Son Nguyen's avatar
Son Nguyen committed
77
78
then

79
80
81
82
83
84
85
86
87
88
89
90
91
92
    if [[ $2 == @* ]]; then
        TAGS=$2
        echo "Only run tests with the tag: $TAGS"
    elif [ $2 ]; then
        if [[ $2 == */* ]]; then
            FEATURE="test/behat/features/$2"
        else
            FEATURE=`find test/behat/features -name $2 | head -n 1`
        fi
        echo "Only run tests in file: $FEATURE"
    else
        echo "Run all tests"
    fi

Son Nguyen's avatar
Son Nguyen committed
93
    if [ "$ACTION" = "runfresh" ]
94
95
96
97
98
    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
99
    # Initialise the test site for behat (database, dataroot, behat yml config)
Son Nguyen's avatar
Son Nguyen committed
100
101
102
103
104
105
106
107
    php htdocs/testing/frameworks/behat/cli/init.php

    # Run the Behat tests themselves (after any intial setup)
    if is_selenium_running; then
        echo "Selenium is running"
    else
        echo "Start Selenium..."

Aaron Wells's avatar
Aaron Wells committed
108
109
        SELENIUM_VERSION_MAJOR=2.53
        SELENIUM_VERSION_MINOR=1
Son Nguyen's avatar
Son Nguyen committed
110
111
112

        SELENIUM_FILENAME=selenium-server-standalone-$SELENIUM_VERSION_MAJOR.$SELENIUM_VERSION_MINOR.jar
        SELENIUM_PATH=./test/behat/$SELENIUM_FILENAME
113
        # @todo make this more flexible, cross-platform?
114
        CHROMEDRIVER_PATH=./test/behat/chromedriver-2.35-linux64
Son Nguyen's avatar
Son Nguyen committed
115
116
117
118
119
120
121
122

        # 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

123
        if [ $ACTION = 'runheadless' -o $ACTION = 'rundebugheadless' ]
124
125
126
        then
            # we want to run selenium headless on a different display - this allows for that ;)
            echo "Starting Xvfb ..."
127
128
            Xvfb :${XVFB_PORT} -ac > /tmp/xvfb-${XVFB_PORT}.log 2>&1 & echo "PID [$!]"
            DISPLAY=:${XVFB_PORT} nohup java -Dwebdriver.chrome.driver=$CHROMEDRIVER_PATH -jar $SELENIUM_PATH -port ${SELENIUM_PORT} -log /tmp/selenium-${SELENIUM_PORT}.log > /tmp/selenium-${SELENIUM_PORT}.log 2>&1 & echo $!
129
        else
130
            java -Dwebdriver.chrome.driver=$CHROMEDRIVER_PATH -jar $SELENIUM_PATH -port ${SELENIUM_PORT} -log /tmp/selenium-${SELENIUM_PORT}.log > /tmp/selenium-${SELENIUM_PORT}.log 2>&1 &
131
        fi
Son Nguyen's avatar
Son Nguyen committed
132
133
134
135
136
137
138
139
140
141

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

    echo "Start PHP server"
142
    php --server localhost:${PHP_PORT} --docroot $MAHARAROOT/htdocs > /tmp/php-${PHP_PORT}.log 2>&1 &
Son Nguyen's avatar
Son Nguyen committed
143
144
145
146
147
    SERVER=$!

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

148
    #added html format for html report
149
    OPTIONS=''
150
151
152
153
154
155
156
157
158
    if [[ $REPORT == 'html' ]]
      then
      if [ "$ACTION" = "rundebug" -o "$ACTION" = "rundebugheadless" ]
      then
          OPTIONS=$OPTIONS" --format=pretty --format=html"
      else
          OPTIONS=$OPTIONS" --format=progress --format=html"
      fi
    elif [ "$ACTION" = "rundebug" -o "$ACTION" = "rundebugheadless" ]
Son Nguyen's avatar
Son Nguyen committed
159
    then
160
          OPTIONS=$OPTIONS" --format=pretty"
161
162
163
    fi

    if [ "$TAGS" ]; then
164
        OPTIONS=$OPTIONS" --tags "$TAGS
165
166
    elif [ "$FEATURE" ]; then
        OPTIONS=$OPTIONS" "$FEATURE
Son Nguyen's avatar
Son Nguyen committed
167
168
169
170
171
172
    fi

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

173
    echo ./external/vendor/bin/behat --config $BEHATCONFIGFILE $OPTIONS
174
175
    ./external/vendor/bin/behat --config $BEHATCONFIGFILE $OPTIONS

Son Nguyen's avatar
Son Nguyen committed
176
177
178
179
    echo
    echo "=================================================="
    echo
    echo "Shutdown"
180
    cleanup 0
Son Nguyen's avatar
Son Nguyen committed
181
182
183
184
185
186
187
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 ""
188
189
190
    echo "# Run tests in file \"example.feature\""
    echo "mahara_behat run example.feature"
    echo ""
Son Nguyen's avatar
Son Nguyen committed
191
192
193
    echo "# Run tests with specific tag:"
    echo "mahara_behat run @tagname"
    echo ""
194
195
    echo "# Run tests with extra debug output:"
    echo "mahara_behat rundebug"
196
    echo "mahara_behat rundebug example.feature"
197
198
    echo "mahara_behat rundebug @tagname"
    echo ""
199
200
201
202
203
204
    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 ""
205
206
207
208
209
210
211
    echo "# To run html report option, add html as the third command line argument."
    echo "# Ex 1. To run selected tests:"
    echo "mahara_behat runheadless example.feature html"
    echo "# Ex 2. To run the whole suite:"
    echo "mahara_behat runheadless null html"
    echo "# If running linux, the report will open automatically, otherwise you'll find it in your behat dataroot"
    echo ""
Son Nguyen's avatar
Son Nguyen committed
212
213
214
215
216
217
218
219
220
221
    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