示例#1
0
 /**
  * Resets the test environment.
  *
  * @param BeforeScenarioScope $scope scope passed by event fired before scenario.
  * @throws behat_stop_exception If here we are not using the test database it should be because of a coding error
  */
 public function before_scenario(BeforeScenarioScope $scope)
 {
     global $DB, $CFG;
     // As many checks as we can.
     if (!defined('BEHAT_TEST') || !defined('BEHAT_SITE_RUNNING') || php_sapi_name() != 'cli' || !behat_util::is_test_mode_enabled() || !behat_util::is_test_site()) {
         throw new behat_stop_exception('Behat only can modify the test database and the test dataroot!');
     }
     $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests';
     $driverexceptionmsg = 'Selenium server is not running, you need to start it to run tests that involve Javascript. ' . $moreinfo;
     try {
         $session = $this->getSession();
     } catch (CurlExec $e) {
         // Exception thrown by WebDriver, so only @javascript tests will be caugth; in
         // behat_util::check_server_status() we already checked that the server is running.
         throw new behat_stop_exception($driverexceptionmsg);
     } catch (DriverException $e) {
         throw new behat_stop_exception($driverexceptionmsg);
     } catch (UnknownError $e) {
         // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions.
         throw new behat_stop_exception($e->getMessage());
     }
     $suitename = $scope->getSuite()->getName();
     // Register behat selectors for theme, if suite is changed. We do it for every suite change.
     if ($suitename !== self::$runningsuite) {
         behat_context_helper::set_environment($scope->getEnvironment());
         // We need the Mink session to do it and we do it only before the first scenario.
         $namedpartialclass = 'behat_partial_named_selector';
         $namedexactclass = 'behat_exact_named_selector';
         if ($suitename !== 'default') {
             // If override selector exist, then set it as default behat selectors class.
             $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_partial', true);
             if (class_exists($overrideclass)) {
                 $namedpartialclass = $overrideclass;
             }
             // If override selector exist, then set it as default behat selectors class.
             $overrideclass = behat_config_util::get_behat_theme_selector_override_classname($suitename, 'named_exact', true);
             if (class_exists($overrideclass)) {
                 $namedexactclass = $overrideclass;
             }
         }
         $this->getSession()->getSelectorsHandler()->registerSelector('named_partial', new $namedpartialclass());
         $this->getSession()->getSelectorsHandler()->registerSelector('named_exact', new $namedexactclass());
     }
     // Reset mink session between the scenarios.
     $session->reset();
     // Reset $SESSION.
     \core\session\manager::init_empty_session();
     behat_util::reset_all_data();
     // Assign valid data to admin user (some generator-related code needs a valid user).
     $user = $DB->get_record('user', array('username' => 'admin'));
     \core\session\manager::set_user($user);
     // Reset the browser if specified in config.php.
     if (!empty($CFG->behat_restart_browser_after) && $this->running_javascript()) {
         $now = time();
         if (self::$lastbrowsersessionstart + $CFG->behat_restart_browser_after < $now) {
             $session->restart();
             self::$lastbrowsersessionstart = $now;
         }
     }
     // Set the theme if not default.
     if ($suitename !== "default") {
         set_config('theme', $suitename);
         self::$runningsuite = $suitename;
     }
     // Start always in the the homepage.
     try {
         // Let's be conservative as we never know when new upstream issues will affect us.
         $session->visit($this->locate_path('/'));
     } catch (UnknownError $e) {
         throw new behat_stop_exception($e->getMessage());
     }
     // Checking that the root path is a Moodle test site.
     if (self::is_first_scenario()) {
         $notestsiteexception = new behat_stop_exception('The base URL (' . $CFG->wwwroot . ') is not a behat test site, ' . 'ensure you started the built-in web server in the correct directory or your web server is correctly started and set up');
         $this->find("xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']", $notestsiteexception);
         self::$initprocessesfinished = true;
     }
     // Run all test with medium (1024x768) screen size, to avoid responsive problems.
     $this->resize_window('medium');
 }
示例#2
0
 /**
  * Test if clean features key and path is returned.
  * @dataProvider clean_features_path_list
  */
 public function test_get_clean_feature_key_and_path($featurepath, $key, $cleanfeaturepath)
 {
     global $CFG;
     // This is a hack so directory name is correctly detected in tests.
     //FIXME: MDL-55722 work out why this is necessary..
     $oldroot = $CFG->dirroot;
     $CFG->dirroot = 'C:';
     $behatconfigutil = new behat_config_util();
     // Fix expected directory path for OS.
     $cleanfeaturepath = testing_cli_fix_directory_separator($cleanfeaturepath);
     list($retkey, $retcleanfeaturepath) = $behatconfigutil->get_clean_feature_key_and_path($featurepath);
     $this->assertEquals($key, $retkey);
     $this->assertEquals($cleanfeaturepath, $retcleanfeaturepath);
     //FIXME: MDL-55722 work out why this is necessary..
     $CFG->dirroot = $oldroot;
 }
示例#3
0
文件: index.php 项目: evltuma/moodle
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require __DIR__ . '/../../../config.php';
require_once $CFG->libdir . '/adminlib.php';
require_once $CFG->dirroot . '/' . $CFG->admin . '/tool/behat/locallib.php';
require_once $CFG->libdir . '/behat/classes/behat_config_manager.php';
// This page usually takes an exceedingly long time to load, so we need to
// increase the time limit. At present it takes about a minute on some
// systems, but let's allow room for expansion.
core_php_time_limit::raise(300);
$filter = optional_param('filter', '', PARAM_ALPHANUMEXT);
$type = optional_param('type', false, PARAM_ALPHAEXT);
$component = optional_param('component', '', PARAM_ALPHAEXT);
admin_externalpage_setup('toolbehat');
// Getting available steps definitions from behat.
$steps = tool_behat::stepsdefinitions($type, $component, $filter);
// Form.
$componentswithsteps = array('' => get_string('allavailablesteps', 'tool_behat'));
// Complete the components list with the moodle steps definitions.
$behatconfig = new behat_config_util();
$components = $behatconfig->get_components_contexts();
if ($components) {
    foreach ($components as $component => $filepath) {
        // TODO Use a class static attribute instead of the class name.
        $componentswithsteps[$component] = 'Moodle ' . substr($component, 6);
    }
}
$form = new steps_definitions_form(null, array('components' => $componentswithsteps));
// Output contents.
$renderer = $PAGE->get_renderer('tool_behat');
echo $renderer->render_stepsdefinitions($steps, $form);