diff --git a/mod/choice/tests/behat/app_basic_usage.feature b/mod/choice/tests/behat/app_basic_usage.feature index a25265feb..6f0caea8e 100755 --- a/mod/choice/tests/behat/app_basic_usage.feature +++ b/mod/choice/tests/behat/app_basic_usage.feature @@ -58,7 +58,7 @@ Feature: Test basic usage of choice activity in app And I should find "Option 3: 0" in the app And I should find "Remove my choice" in the app - When I select "Option 1" in the app + When I unselect "Option 1" in the app And I select "Option 3" in the app And I press "Save my choice" in the app Then I should find "Option 1: 0" in the app @@ -114,9 +114,9 @@ Feature: Test basic usage of choice activity in app And I log in as "student1" And I press "Course 1" near "Course overview" in the app And I press "Test single choice name" in the app - And I press "Option 1" in the app + And I select "Option 1" in the app And I switch offline mode to "true" - And I press "Option 2" in the app + And I select "Option 2" in the app And I press "Save my choice" in the app Then I should find "Are you sure" in the app @@ -154,7 +154,7 @@ Feature: Test basic usage of choice activity in app When I press "OK" in the app And I press the back button in the app And I press "Test single choice name" in the app - And I press "Option 2" in the app + And I select "Option 2" in the app And I press "Save my choice" in the app Then I should find "Are you sure" in the app @@ -183,7 +183,7 @@ Feature: Test basic usage of choice activity in app And I log in as "student1" And I press "Course 1" near "Course overview" in the app And I press "Choice name" in the app - And I press "Option 2" in the app + And I select "Option 2" in the app And I press "Save my choice" in the app And I press "OK" in the app diff --git a/tests/behat/behat_app.php b/tests/behat/behat_app.php index 3d52a63c5..1d995a6ba 100644 --- a/tests/behat/behat_app.php +++ b/tests/behat/behat_app.php @@ -551,14 +551,49 @@ class behat_app extends behat_base { * with JavaScript, and clicks may not work until they are initialized properly which may cause flaky tests due * to race conditions. * - * @Then /^I select "(?P(?:[^"]|\\")*)"(?: near "(?P(?:[^"]|\\")*)")? in the app$/ + * @Then /^I (?Punselect|select) "(?P(?:[^"]|\\")*)"(?: near "(?P(?:[^"]|\\")*)")? in the app$/ + * @param string $selectedtext Select/unselect string * @param string $text Text identifying click target * @param string $near Text identifying a nearby unique piece of text * @throws DriverException If the press doesn't work */ - public function i_select_in_the_app($text, $near='') { - $this->getSession()->wait(100); - $this->press($text, $near); + public function i_select_in_the_app(string $selectedtext, string $text, string $near = '') { + $selected = $selectedtext === 'select' ? 'YES' : 'NO'; + $text = addslashes_js($text); + $near = addslashes_js($near); + + $this->spin(function() use ($selectedtext, $selected, $text, $near) { + // Don't do anything if the item is already in the expected state. + $result = $this->evaluate_script("return window.behat.isSelected(\"$text\", \"$near\");"); + + if ($result === $selected) { + return true; + } + + // Press item. + $result = $this->evaluate_script("return window.behat.press(\"$text\", \"$near\");"); + + if ($result !== 'OK') { + throw new DriverException('Error pressing item - ' . $result); + } + + // Check that it worked as expected. + $result = $this->evaluate_script("return window.behat.isSelected(\"$text\", \"$near\");"); + + switch ($result) { + case 'YES': + case 'NO': + if ($result !== $selected) { + throw new ExpectationException("Item wasn't $selectedtext after pressing it", $this->getSession()->getDriver()); + } + + return true; + default: + throw new DriverException('Error finding item - ' . $result); + } + }); + + $this->wait_for_pending_js(); } /** @@ -584,17 +619,16 @@ class behat_app extends behat_base { * @throws DriverException If the press doesn't work */ protected function press(string $text, string $near = '') { - $this->spin(function($context, $args) use ($text, $near) { - if ($near !== '') { - $nearbit = ', "' . addslashes_js($near) . '"'; - } else { - $nearbit = ''; - } - $result = $this->evaluate_script('return window.behat.press("' . - addslashes_js($text) . '"' . $nearbit .');'); + $text = addslashes_js($text); + $near = addslashes_js($near); + + $this->spin(function() use ($text, $near) { + $result = $this->evaluate_script("return window.behat.press(\"$text\", \"$near\");"); + if ($result !== 'OK') { throw new DriverException('Error pressing item - ' . $result); } + return true; }); $this->wait_for_pending_js();