Commit e35e3528 authored by Rebecca Blundell's avatar Rebecca Blundell

Bug #1696627: Behat HTML report

- Updated details for behat.yml config to BehatConfigManager and a call
to update the file
- Added BehatHooks to get screenshots on fail. Screenshots are saved in the
Behat maharadata directory at /behat/results/html/screenshots
- Added auto-open of report
- Added some explanatory notes on how to run report

If you set the "html" flag as third parameter of the Behat command, you can
generate the HTML report. The report will be saved in the Behat
dataroot under behat/html_results

If you accidentially set the "html" flag as 2nd parameter it will
instead show help info

behatnotneeded

Change-Id: I76c67a1148278efa049317b526fef760c1a43ded
parent 186c07f8
......@@ -8,6 +8,7 @@
"behat/mink-goutte-driver": "v1.2.1",
"behat/mink-selenium2-driver": "v1.3.1",
"emuse/behat-html-formatter": ">=0.1",
"fabpot/goutte": "v3.1.2",
"guzzlehttp/guzzle": "6.2.1",
......@@ -19,4 +20,4 @@
"MaharaExtension": "../htdocs/testing/frameworks/behat/extension/"
}
}
}
\ No newline at end of file
}
......@@ -150,7 +150,10 @@ class BehatConfigManager {
'default' => array(
'autoload' => array($basedir . DIRECTORY_SEPARATOR . 'classes'),
'formatters' => array(
'progress' => true
'progress' => true,
'html' => array(
'output_path' => '%paths.base%/html_results/'
),
),
'extensions' => array(
'Behat\MinkExtension' => array(
......@@ -163,6 +166,14 @@ class BehatConfigManager {
),
'goutte' => null,
),
'emuse\BehatHTMLFormatter\BehatHTMLFormatterExtension' => array(
'name' => 'html',
'renderer' => 'Twig,Behat2',
'file_name' => 'index',
'print_args' => 'true',
'print_outp' => 'true',
'loop_break' => 'true'
)
),
'suites' => $suites
)
......
......@@ -232,6 +232,16 @@ class BehatHooks extends BehatBase {
// }
// }
/**
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
*/
public function setUpTestEnvironment($scope) {
$this->currentScenario = $scope->getScenario();
}
/**
* Wait for JS to complete after finishing the step.
*
......@@ -280,6 +290,24 @@ class BehatHooks extends BehatBase {
$scope->getTestResult()->getResultCode() === 99) {
$this->take_contentdump($scope);
}
// if test has failed, and is not an api test, get screenshot
if (!$scope->getTestResult()->isPassed()) {
// create filename string
$featureFolder = preg_replace('/\W/', '', $scope->getFeature()->getTitle());
$scenarioName = $this->currentScenario->getTitle();
$fileName = preg_replace('/\W/', '', $scenarioName) . '.png';
// create screenshots directory if it doesn't exist
if (!file_exists($CFG->behat_dataroot . '/behat/html_results/screenshots/' . $featureFolder)) {
mkdir($CFG->behat_dataroot . '/behat/html_results/screenshots/' . $featureFolder, $CFG->directorypermissions, true);
}
// For Selenium2 Driver you can use:
file_put_contents($CFG->behat_dataroot . '/behat/html_results/screenshots/' . $featureFolder . '/' . $fileName, $this->getSession()->getDriver()->getScreenshot());
}
}
/**
......@@ -490,4 +518,3 @@ class BehatHooks extends BehatBase {
}
}
......@@ -139,7 +139,7 @@ try {
// Update behat and dependencies using composer
testing_update_dependencies();
}
BehatConfigManager::update_config_file();
// Other possible actions we may need to take.
switch ($statuscode) {
case BEHAT_MAHARA_EXITCODE_NOTINSTALLED:
......
......@@ -2,8 +2,10 @@
# Get action and Mahara dir
ACTION=$1
REPORT=$3
SCRIPTPATH=`readlink -f "${BASH_SOURCE[0]}"`
MAHARAROOT=`dirname $( dirname $( dirname "$SCRIPTPATH" ))`
BEHATROOT=`grep -v '^//\|^#' $MAHARAROOT/htdocs/config.php | grep behat_dataroot | grep -o "['\"].*['\"];" | sed "s/['\";]//g"`
SERVER=0
test -z $SELENIUM_PORT && export SELENIUM_PORT=4444
test -z $PHP_PORT && export PHP_PORT=8000
......@@ -28,6 +30,11 @@ function cleanup {
echo "Shutdown Selenium"
curl -o /dev/null --silent http://localhost:${SELENIUM_PORT}/selenium-server/driver/?cmd=shutDownSeleniumServer
if [[ $REPORT ]]
then
xdg-open file://${BEHATROOT}/behat/html_results/index.html
fi
if [[ $SERVER ]]
then
echo "Shutdown PHP server"
......@@ -66,7 +73,7 @@ then
PERFORM=$2
php htdocs/testing/frameworks/behat/cli/util.php --$PERFORM
elif [ "$ACTION" = "run" -o "$ACTION" = "runheadless" -o "$ACTION" = "rundebug" -o "$ACTION" = "runfresh" -o $ACTION = 'rundebugheadless' ]
elif [ "$ACTION" = "run" -o "$ACTION" = "runheadless" -o "$ACTION" = "rundebug" -o "$ACTION" = "runfresh" -o "$ACTION" = "rundebugheadless" ] && [ "$2" != "html" ]
then
if [[ $2 == @* ]]; then
......@@ -138,11 +145,19 @@ then
BEHATCONFIGFILE=`php htdocs/testing/frameworks/behat/cli/util.php --config`
echo "Run Behat..."
#added html format for html report
OPTIONS=''
if [ $ACTION = 'rundebug' -o $ACTION = 'rundebugheadless' ]
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" ]
then
OPTIONS=$OPTIONS" --format=pretty"
OPTIONS=$OPTIONS" --format=pretty"
fi
if [ "$TAGS" ]; then
......@@ -187,6 +202,13 @@ else
echo "# Run in headless mode with extra debug output:"
echo "mahara_behat rundebugheadless"
echo ""
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 ""
echo "# Enable test site:"
echo "mahara_behat action enable"
echo ""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment