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

3
4
5
# Quit on error
set -e

Son Nguyen's avatar
Son Nguyen committed
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
function is_selenium_running {
    res=$(curl -o /dev/null --silent --write-out '%{http_code}\n' http://localhost:4444/wd/hub/status)
    if [[ $res == "200" ]]; then
        return 0;
    else
        return 1;
    fi
}

# 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

# Get action and Mahara dir
ACTION=$1
SCRIPTPATH=`readlink -f "${BASH_SOURCE[0]}"`
MAHARAROOT=`dirname $( dirname $( dirname "$SCRIPTPATH" ))`

cd $MAHARAROOT

if [ "$ACTION" = "action" ]
then

    # Wrap the util.php script

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

37
elif [ "$ACTION" = "run" -o "$ACTION" = "runheadless" ]
Son Nguyen's avatar
Son Nguyen committed
38
39
40
41
42
43
44
45
46
47
48
49
50
then

    # 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..."

51
        SELENIUM_VERSION_MAJOR=2.45
52
        SELENIUM_VERSION_MINOR=0
Son Nguyen's avatar
Son Nguyen committed
53
54
55
56
57
58
59
60
61
62
63

        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

64
65
66
67
68
69
70
71
72
73
74
        if [[ $ACTION == 'runheadless' ]]
        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
        sleep 15  # wait for selenium to initialise properly
Son Nguyen's avatar
Son Nguyen committed
75
76
77
78
79
80
81
82
83
84

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

    echo "Start PHP server"
85
    php --server localhost:8000 --docroot ./htdocs &>/dev/null &
Son Nguyen's avatar
Son Nguyen committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    SERVER=$!

    echo "Enable test site"
    php htdocs/testing/frameworks/behat/cli/util.php --enable

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

    if [ "$TAGS" ]
    then
        echo "Only run tests with the tag: $TAGS"
    else
        echo "Run all tests"
    fi

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

    if [ "$TAGS" ]
    then
107
        ./external/vendor/bin/behat --config $BEHATCONFIGFILE --tags $TAGS
Son Nguyen's avatar
Son Nguyen committed
108
    else
109
        ./external/vendor/bin/behat --config $BEHATCONFIGFILE
Son Nguyen's avatar
Son Nguyen committed
110
111
112
113
114
115
    fi

    echo
    echo "=================================================="
    echo
    echo "Shutdown"
116
117
118
119

    # Kill Selenium
    curl -o /dev/null --silent http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer
    # Kill PHP server
Son Nguyen's avatar
Son Nguyen committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    kill $SERVER
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 ""
    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