Merge branch 'MDL-43439_master' of git://github.com/dmonllao/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2014-01-15 00:03:34 +01:00
2 changed files with 68 additions and 0 deletions
+4
View File
@@ -654,6 +654,10 @@ $CFG->admin = 'admin';
// Example:
// $CFG->behat_additionalfeatures = array('/home/developer/code/wipfeatures');
//
// You can make behat save a screenshot when a scenario fails.
// Example:
// $CFG->behat_screenshots_path = '/my/path/to/save/screenshots';
//
//=========================================================================
// 12. DEVELOPER DATA GENERATOR
//=========================================================================
+64
View File
@@ -77,6 +77,13 @@ class behat_hooks extends behat_base {
*/
protected static $currentstepexception = null;
/**
* If we are saving screenshots on failures we should use the same parent dir during a run.
*
* @var The parent dir name
*/
protected static $screenshotsdirname = false;
/**
* Gives access to moodle codebase, ensures all is ready and sets up the test lock.
*
@@ -134,6 +141,10 @@ class behat_hooks extends behat_base {
// Store the initial browser session opening.
self::$lastbrowsersessionstart = time();
}
if (!empty($CFG->behat_screenshots_path) && !is_writable($CFG->behat_screenshots_path)) {
throw new Exception('You set $CFG->behat_screenshots_path to a non-writable directory');
}
}
/**
@@ -258,6 +269,13 @@ class behat_hooks extends behat_base {
* @AfterStep @javascript
*/
public function after_step_javascript($event) {
global $CFG;
// Save a screenshot if the step failed.
if (!empty($CFG->behat_screenshots_path) &&
$event->getResult() === StepEvent::FAILED) {
$this->take_screenshot($event);
}
try {
$this->wait_for_pending_js();
@@ -278,6 +296,52 @@ class behat_hooks extends behat_base {
}
}
/**
* Getter for self::$screenshotsdirname
*
* @return string
*/
protected function get_run_screenshots_dir() {
return self::$screenshotsdirname;
}
/**
* Take screenshot when a step fails.
*
* @throws Exception
* @param StepEvent $event
*/
protected function take_screenshot(StepEvent $event) {
global $CFG;
// Goutte can't save screenshots.
if (!$this->running_javascript()) {
return false;
}
// All the run screenshots in the same parent dir.
if (!$screenshotsdirname = self::get_run_screenshots_dir()) {
$screenshotsdirname = self::$screenshotsdirname = date('Y-m-d_Hi');
$dir = $CFG->behat_screenshots_path . DIRECTORY_SEPARATOR . $screenshotsdirname;
if (!mkdir($dir, $CFG->directorypermissions, true)) {
// It shouldn't, we already checked that the directory is writable.
throw new Exception('No directories can be created inside $CFG->behat_screenshots_path, check the directory permissions.');
}
} else {
// We will always need to know the full path.
$dir = $CFG->behat_screenshots_path . DIRECTORY_SEPARATOR . $screenshotsdirname;
}
// The scenario title + the failed step text.
// We want a i-am-the-scenario-title_i-am-the-failed-step.png format.
$filename = $event->getStep()->getParent()->getTitle() . '_' . $event->getStep()->getText();
$filename = preg_replace('/([^a-zA-Z0-9\_]+)/', '-', $filename) . '.png';
$this->saveScreenshot($filename, $dir);
}
/**
* Waits for all the JS to be loaded.
*