diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index 2ead0ffe4ea..d0e505d1b58 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -221,8 +221,9 @@ class behat_course extends behat_base { * @param int $section */ public function i_add_to_section($activity, $section) { + $this->require_javascript('Please use the \'the following "activity" exists:\' data generator instead.'); - if ($this->getSession()->getPage()->find('css', 'body#page-site-index') && (int)$section <= 1) { + if ($this->getSession()->getPage()->find('css', 'body#page-site-index') && (int) $section <= 1) { // We are on the frontpage. if ($section) { // Section 1 represents the contents on the frontpage. @@ -237,41 +238,24 @@ class behat_course extends behat_base { $sectionxpath = "//li[@id='section-" . $section . "']"; } + // Clicks add activity or resource section link. + $sectionnode = $this->find('xpath', $sectionxpath); + $this->execute('behat_general::i_click_on_in_the', [ + get_string('addresourceoractivity', 'moodle'), + 'button', + $sectionnode, + 'NodeElement', + ]); + + // Clicks the selected activity if it exists. $activityliteral = behat_context_helper::escape(ucfirst($activity)); + $activityxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' modchooser ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optioninfo ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optionname ')]" . + "[normalize-space(.)=$activityliteral]" . + "/parent::a"; - if ($this->running_javascript()) { - - // Clicks add activity or resource section link. - $sectionxpath = $sectionxpath . "/descendant::div" . - "[contains(concat(' ', normalize-space(@class) , ' '), ' section-modchooser ')]/button"; - - $this->execute('behat_general::i_click_on', [$sectionxpath, 'xpath']); - - // Clicks the selected activity if it exists. - $activityxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' modchooser ')]" . - "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optioninfo ')]" . - "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optionname ')]" . - "[normalize-space(.)=$activityliteral]" . - "/parent::a"; - - $this->execute('behat_general::i_click_on', [$activityxpath, 'xpath']); - - } else { - // Without Javascript. - - // Selecting the option from the select box which contains the option. - $selectxpath = $sectionxpath . "/descendant::div" . - "[contains(concat(' ', normalize-space(@class), ' '), ' section_add_menus ')]" . - "/descendant::select[option[normalize-space(.)=$activityliteral]]"; - $selectnode = $this->find('xpath', $selectxpath); - $selectnode->selectOption($activity); - - // Go button. - $gobuttonxpath = $selectxpath . "/ancestor::form/descendant::input[@type='submit']"; - $gobutton = $this->find('xpath', $gobuttonxpath); - $gobutton->click(); - } - + $this->execute('behat_general::i_click_on', [$activityxpath, 'xpath']); } /** diff --git a/lib/behat/classes/behat_session_trait.php b/lib/behat/classes/behat_session_trait.php index 8d2b1ad4d50..67d7b8a2c20 100644 --- a/lib/behat/classes/behat_session_trait.php +++ b/lib/behat/classes/behat_session_trait.php @@ -498,10 +498,11 @@ trait behat_session_trait { /** * Require that javascript be available in the current Session. * + * @param null|string $message An additional information message to show when JS is not available * @throws DriverException */ - protected function require_javascript() { - return self::require_javascript_in_session($this->getSession()); + protected function require_javascript(?string $message = null) { + return self::require_javascript_in_session($this->getSession(), $message); } /** @@ -518,14 +519,19 @@ trait behat_session_trait { * Require that javascript be available for the specified Session. * * @param Session $session + * @param null|string $message An additional information message to show when JS is not available * @throws DriverException */ - protected static function require_javascript_in_session(Session $session): void { + protected static function require_javascript_in_session(Session $session, ?string $message = null): void { if (self::running_javascript_in_session($session)) { return; } - throw new DriverException('Javascript is required'); + $error = "Javascript is required for this step."; + if ($message) { + $error = "{$error} {$message}"; + } + throw new DriverException($error); } /**