From 421c5b282a74de2365cdaa45662fd9d62bb95902 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 8 Oct 2021 15:11:02 +0200 Subject: [PATCH] MOBILE-3726 behat: Add tests for external links --- tests/behat/app_behat_runtime.js | 3 ++ tests/behat/behat_app.php | 33 ++++++++++++-- tests/behat/navigation_externallinks.feature | 48 ++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 tests/behat/navigation_externallinks.feature diff --git a/tests/behat/app_behat_runtime.js b/tests/behat/app_behat_runtime.js index 40a71cdf7..a21ac38e8 100644 --- a/tests/behat/app_behat_runtime.js +++ b/tests/behat/app_behat_runtime.js @@ -518,6 +518,9 @@ near: { text: 'Notifications' }, })[0]; break; + case 'page menu': + foundButton = findElementsBasedOnText({ text: 'Display options' })[0]; + break; default: return 'ERROR: Unsupported standard button type'; } diff --git a/tests/behat/behat_app.php b/tests/behat/behat_app.php index 54ebb3d91..31583690a 100644 --- a/tests/behat/behat_app.php +++ b/tests/behat/behat_app.php @@ -789,14 +789,16 @@ class behat_app extends behat_base { /** * Check that the app opened a new browser tab. * - * @Given /^the app should( not)? have opened a browser tab$/ + * @Given /^the app should( not)? have opened a browser tab(?: with url "(?P[^"]+)")?$/ * @param bool $not + * @param string $urlpattern */ - public function the_app_should_have_opened_a_browser_tab(bool $not = false) { - $this->spin(function() use ($not) { - $openedbrowsertab = count($this->getSession()->getWindowNames()) === 2; + public function the_app_should_have_opened_a_browser_tab(bool $not = false, ?string $urlpattern = null) { + $this->spin(function() use ($not, $urlpattern) { + $windownames = $this->getSession()->getWindowNames(); + $openedbrowsertab = count($windownames) === 2; - if ($not === $openedbrowsertab) { + if ((!$not && !$openedbrowsertab) || ($not && $openedbrowsertab && is_null($urlpattern))) { throw new ExpectationException( $not ? 'Did not expect the app to have opened a browser tab' @@ -805,6 +807,22 @@ class behat_app extends behat_base { ); } + if (!is_null($urlpattern)) { + $this->getSession()->switchToWindow($windownames[1]); + $windowurl = $this->getSession()->getCurrentUrl(); + $windowhaspattern = preg_match("/$urlpattern/", $windowurl); + $this->getSession()->switchToWindow($windownames[0]); + + if ($not === $windowhaspattern) { + throw new ExpectationException( + $not + ? "Did not expect the app to have opened a browser tab with pattern '$urlpattern'" + : "Browser tab url does not match pattern '$urlpattern', it is '$windowurl'", + $this->getSession()->getDriver() + ); + } + } + return true; }); } @@ -895,6 +913,11 @@ class behat_app extends behat_base { if (count($names) !== 2) { throw new DriverException('Expected to see 2 tabs open, not ' . count($names)); } + // Make sure the browser tab is selected. + if ($this->getSession()->getWindowName() !== $names[1]) { + $this->getSession()->switchToWindow($names[1]); + } + $this->execute_script('window.close()'); $this->getSession()->switchToWindow($names[0]); } diff --git a/tests/behat/navigation_externallinks.feature b/tests/behat/navigation_externallinks.feature new file mode 100644 index 000000000..b3da3c3be --- /dev/null +++ b/tests/behat/navigation_externallinks.feature @@ -0,0 +1,48 @@ +@app @javascript +Feature: It opens external links properly. + + Background: + Given the following "users" exist: + | username | + | student1 | + And the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + And the following "activities" exist: + | activity | name | intro | course | idnumber | + | forum | Test forum | Test forum | C1 | forum | + And the following forum discussions exist in course "Course 1": + | forum | user | name | message | + | Test forum | student1 | Forum topic | See moodle.org | + + Scenario: Click an external link + When I enter the app + And I log in as "student1" + And I press "Course 1" near "Course overview" in the app + And I press "Test forum" in the app + And I press "Forum topic" in the app + And I press "moodle.org" in the app + Then I should find "You are about to leave the app" in the app + + When I press "Cancel" in the app + And I press "moodle.org" in the app + And I press "OK" in the app + Then the app should have opened a browser tab with url "moodle.org" + + When I close the browser tab opened by the app + And I press the back button in the app + And I press the page menu button in the app + And I press "Open in browser" in the app + Then the app should have opened a browser tab + + When I close the browser tab opened by the app + And I press "Forum topic" in the app + And I press "moodle.org" in the app + And I select "Don't show again." in the app + And I press "OK" in the app + And I close the browser tab opened by the app + And I press "moodle.org" in the app + Then the app should have opened a browser tab with url "moodle.org"