/** * Renders the list of available steps according to the submitted filters. * * @param mixed $stepsdefinitions Available steps array. * @param moodleform $form * @return string HTML code */ public function render_stepsdefinitions($stepsdefinitions, $form) { $title = get_string('pluginname', 'tool_behat'); // Header. $html = $this->output->header(); $html .= $this->output->heading($title); // Info. $installurl = behat_command::DOCS_URL . '#Installation'; $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank')); $writetestsurl = behat_command::DOCS_URL . '#Writting_features'; $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank')); $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions'; $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank')); $infos = array(get_string('installinfo', 'tool_behat', $installlink), get_string('newtestsinfo', 'tool_behat', $writetestslink), get_string('newstepsinfo', 'tool_behat', $writestepslink)); // List of steps. $html .= $this->output->box_start(); $html .= html_writer::tag('h1', get_string('infoheading', 'tool_behat')); $html .= html_writer::tag('div', get_string('aim', 'tool_behat')); $html .= html_writer::start_tag('div'); $html .= html_writer::start_tag('ul'); $html .= html_writer::start_tag('li'); $html .= implode(html_writer::end_tag('li') . html_writer::start_tag('li'), $infos); $html .= html_writer::end_tag('li'); $html .= html_writer::end_tag('ul'); $html .= html_writer::end_tag('div'); $html .= $this->output->box_end(); // Form. ob_start(); $form->display(); $html .= ob_get_contents(); ob_end_clean(); if (empty($stepsdefinitions)) { $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat'); } else { $stepsdefinitions = implode('', $stepsdefinitions); // Replace text selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\\d?_STRING)/', function ($matches) { return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid()); }, $stepsdefinitions); // Replace selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(SELECTOR\\d?_STRING)/', function ($matches) { return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid()); }, $stepsdefinitions); // Replace simple OR options. $regex = '#\\(\\?P<[^>]+>([^\\)|]+\\|[^\\)]+)\\)#'; $stepsdefinitions = preg_replace_callback($regex, function ($matches) { return html_writer::select(explode('|', $matches[1]), uniqid()); }, $stepsdefinitions); } // Steps definitions. $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions')); $html .= $this->output->footer(); return $html; }
/** * Renders the list of available steps according to the submitted filters. * * @param mixed $stepsdefinitions Available steps array. * @param moodleform $form * @return string HTML code */ public function render_stepsdefinitions($stepsdefinitions, $form) { $html = $this->generic_info(); // Form. ob_start(); $form->display(); $html .= ob_get_contents(); ob_end_clean(); if (empty($stepsdefinitions)) { $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat'); } else { $stepsdefinitions = implode('', $stepsdefinitions); // Replace text selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/', function ($matches) { return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid()); }, $stepsdefinitions ); // Replace selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/', function ($matches) { return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid()); }, $stepsdefinitions ); // Replace simple OR options. $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#'; $stepsdefinitions = preg_replace_callback($regex, function($matches){ return html_writer::select(explode('|', $matches[1]), uniqid()); }, $stepsdefinitions ); } // Steps definitions. $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions')); $html .= $this->output->footer(); return $html; }
/** * Renders the list of available steps according to the submitted filters. * * @param mixed $stepsdefinitions Available steps array. * @param moodleform $form * @return string HTML code */ public function render_stepsdefinitions($stepsdefinitions, $form) { $html = $this->generic_info(); // Form. ob_start(); $form->display(); $html .= ob_get_contents(); ob_end_clean(); if (empty($stepsdefinitions)) { $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat'); } else { $stepsdefinitions = implode('', $stepsdefinitions); // Replace text selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\\d?_STRING)/', function ($matches) { return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid()); }, $stepsdefinitions); // Replace selector type arguments with a user-friendly select. $stepsdefinitions = preg_replace_callback('/(SELECTOR\\d?_STRING)/', function ($matches) { return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid()); }, $stepsdefinitions); // Replace simple OR options. $regex = '#\\(\\?P<[^>]+>([^\\)|]+\\|[^\\)]+)\\)#'; $stepsdefinitions = preg_replace_callback($regex, function ($matches) { return html_writer::select(explode('|', $matches[1]), uniqid()); }, $stepsdefinitions); $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/', function ($matches) { global $CFG; // Creating a link to a popup with the help. $url = new moodle_url('/help.php', array('component' => 'tool_behat', 'identifier' => 'fieldvalueargument', 'lang' => current_language())); // Note: this title is displayed only if JS is disabled, // otherwise the link will have the new ajax tooltip. $title = get_string('fieldvalueargument', 'tool_behat'); $title = get_string('helpprefix2', '', trim($title, ". \t")); $attributes = array('href' => $url, 'title' => $title, 'aria-haspopup' => 'true', 'target' => '_blank'); $output = html_writer::tag('a', 'FIELD_VALUE_STRING', $attributes); return html_writer::tag('span', $output, array('class' => 'helptooltip')); }, $stepsdefinitions); } // Steps definitions. $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions')); $html .= $this->output->footer(); return $html; }
/** * Transforms from step definition's argument style to Mink format. * * Delegates all the process to behat_base::transform_selector() checking * the provided $selectortype. * * @throws ExpectationException * @param string $selectortype It can be css, xpath or any of the named selectors. * @param string $element The locator (or string) we are looking for. * @return array Contains the selector and the locator expected by Mink. */ protected function transform_text_selector($selectortype, $element) { $selectors = behat_selectors::get_allowed_text_selectors(); if (empty($selectors[$selectortype])) { throw new ExpectationException('The "' . $selectortype . '" selector can not be used to select text nodes', $this->getSession()); } return $this->transform_selector($selectortype, $element); }
/** * Resets the test environment. * * @param OutlineExampleEvent|ScenarioEvent $event event fired before scenario. * @throws coding_exception If here we are not using the test database it should be because of a coding error * @BeforeScenario */ public function before_scenario($event) { global $DB, $CFG; // TODO: check config value to ensure site is set for performance data. $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::is_server_running() we already checked that the server is running. throw new Exception($driverexceptionmsg); } catch (DriverException $e) { throw new Exception($driverexceptionmsg); } catch (UnknownError $e) { // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions. $this->throw_unknown_exception($e); } // We need the Mink session to do it and we do it only before the first scenario. if (self::is_first_scenario()) { behat_selectors::register_moodle_selectors($session); behat_context_helper::set_session($session); } // Reset mink session between the scenarios. $session->reset(); // 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); // 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) { $this->throw_unknown_exception($e); } // Checking that the root path is a Moodle test site. /*if (self::is_first_scenario()) { $notestsiteexception = new 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; }*/ }
/** * Resets the test environment. * * @throws coding_exception If here we are not using the test database it should be because of a coding error * @BeforeScenario */ public function before_scenario($event) { global $DB, $SESSION, $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 coding_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::is_server_running() we already checked that the server is running. throw new Exception($driverexceptionmsg); } catch (DriverException $e) { throw new Exception($driverexceptionmsg); } catch (UnknownError $e) { // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions. $this->throw_unknown_exception($e); } // We need the Mink session to do it and we do it only before the first scenario. if (self::is_first_scenario()) { behat_selectors::register_moodle_selectors($session); behat_context_helper::set_session($session); } // Reset $SESSION. \core\session\manager::init_empty_session(); behat_util::reset_database(); behat_util::reset_dataroot(); accesslib_clear_all_caches(true); // Reset the nasty strings list used during the last test. nasty_strings::reset_used_strings(); // 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; } } // 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) { $this->throw_unknown_exception($e); } // Checking that the root path is a Moodle test site. if (self::is_first_scenario()) { $notestsiteexception = new 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'); }
/** * Resets the test environment. * * @param OutlineExampleEvent|ScenarioEvent $event event fired before scenario. * @throws coding_exception If here we are not using the test database it should be because of a coding error * @BeforeScenario */ public function before_scenario(BeforeScenario $event) { global $DB, $CFG; // TODO: check config value to ensure site is set for performance data. $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::is_server_running() we already checked that the server is running. throw new Exception($driverexceptionmsg); } catch (DriverException $e) { throw new Exception($driverexceptionmsg); } catch (UnknownError $e) { // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions. $this->throw_unknown_exception($e); } // We need the Mink session to do it and we do it only before the first scenario. if (self::is_first_scenario()) { behat_selectors::register_moodle_selectors($session); behat_context_helper::set_session($session); // Ensure performance site is enable. if (!get_config('core', 'performancesitedata')) { $notperformancesite = "This is not a performance site" . PHP_EOL . " - Run vendor/bin/moodle_performance_site --enable=s" . PHP_EOL; throw new Exception($notperformancesite); } } // Start a new thread group. testplan_writer::start_thread_group(self::$featurefile, self::$threadgroupname); }
/** * Resets the test environment. * * @param OutlineExampleEvent|ScenarioEvent $event event fired before scenario. * @throws coding_exception If here we are not using the test database it should be because of a coding error * @BeforeScenario */ public function before_scenario($event) { global $DB, $CFG; // TODO: check config value to ensure site is set for performance data. $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::is_server_running() we already checked that the server is running. throw new Exception($driverexceptionmsg); } catch (DriverException $e) { throw new Exception($driverexceptionmsg); } catch (UnknownError $e) { // Generic 'I have no idea' Selenium error. Custom exception to provide more feedback about possible solutions. $this->throw_unknown_exception($e); } // We need the Mink session to do it and we do it only before the first scenario. if (self::is_first_scenario()) { behat_selectors::register_moodle_selectors($session); behat_context_helper::set_session($session); } // Reset mink session between the scenarios. $session->reset(); // 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); // 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) { $this->throw_unknown_exception($e); } }