2018-11-12 12:11:06 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mobile/desktop app steps definitions.
|
|
|
|
*
|
|
|
|
* @package core
|
|
|
|
* @category test
|
|
|
|
* @copyright 2018 The Open University
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
|
|
|
|
|
2021-04-27 15:43:47 +00:00
|
|
|
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
|
2018-11-12 12:11:06 +00:00
|
|
|
|
2021-05-11 16:54:18 +00:00
|
|
|
use Behat\Gherkin\Node\TableNode;
|
2018-11-12 12:11:06 +00:00
|
|
|
use Behat\Mink\Exception\DriverException;
|
|
|
|
use Behat\Mink\Exception\ExpectationException;
|
|
|
|
|
2021-09-29 12:14:48 +00:00
|
|
|
/**
|
|
|
|
* Behat app listener.
|
|
|
|
*/
|
|
|
|
interface behat_app_listener {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the app is loaded.
|
|
|
|
*/
|
|
|
|
function on_app_load(): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called before the app is unloaded.
|
|
|
|
*/
|
|
|
|
function on_app_unload(): void;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Mobile/desktop app steps definitions.
|
|
|
|
*
|
|
|
|
* @package core
|
|
|
|
* @category test
|
|
|
|
* @copyright 2018 The Open University
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
class behat_app extends behat_base {
|
|
|
|
/** @var stdClass Object with data about launched Ionic instance (if any) */
|
|
|
|
protected static $ionicrunning = null;
|
|
|
|
|
2021-09-29 12:14:48 +00:00
|
|
|
/** @var array */
|
|
|
|
protected static $listeners = [];
|
|
|
|
|
2019-01-15 15:12:06 +00:00
|
|
|
/** @var string URL for running Ionic server */
|
|
|
|
protected $ionicurl = '';
|
|
|
|
|
2021-09-29 12:14:48 +00:00
|
|
|
/** @var bool Whether the app is running or not */
|
|
|
|
protected $apprunning = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register listener.
|
|
|
|
*
|
|
|
|
* @param behat_app_listener $listener Listener.
|
|
|
|
* @return Closure Unregister function.
|
|
|
|
*/
|
|
|
|
public static function listen(behat_app_listener $listener): Closure {
|
|
|
|
self::$listeners[] = $listener;
|
|
|
|
|
|
|
|
return function () use ($listener) {
|
|
|
|
$index = array_search($listener, self::$listeners);
|
|
|
|
|
|
|
|
if ($index !== false) {
|
|
|
|
array_splice(self::$listeners, $index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Checks if the current OS is Windows, from the point of view of task-executing-and-killing.
|
|
|
|
*
|
|
|
|
* @return bool True if Windows
|
|
|
|
*/
|
|
|
|
protected static function is_windows() : bool {
|
|
|
|
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
|
|
|
|
}
|
|
|
|
|
2019-01-15 15:12:06 +00:00
|
|
|
/**
|
|
|
|
* Called from behat_hooks when a new scenario starts, if it has the app tag.
|
|
|
|
*
|
|
|
|
* This updates Moodle configuration and starts Ionic running, if it isn't already.
|
|
|
|
*/
|
|
|
|
public function start_scenario() {
|
|
|
|
$this->check_behat_setup();
|
|
|
|
$this->fix_moodle_setup();
|
|
|
|
$this->ionicurl = $this->start_or_reuse_ionic();
|
2021-04-27 15:43:47 +00:00
|
|
|
}
|
2019-01-15 15:12:06 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
2021-05-26 15:09:09 +00:00
|
|
|
* Opens the Moodle app in the browser and introduces the enters the site.
|
2018-11-12 12:11:06 +00:00
|
|
|
*
|
|
|
|
* @Given /^I enter the app$/
|
|
|
|
* @throws DriverException Issue with configuration or feature file
|
|
|
|
* @throws dml_exception Problem with Moodle setup
|
|
|
|
* @throws ExpectationException Problem with resizing window
|
|
|
|
*/
|
|
|
|
public function i_enter_the_app() {
|
2021-05-26 15:09:09 +00:00
|
|
|
$this->i_launch_the_app();
|
|
|
|
$this->enter_site();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the Moodle app in the browser.
|
|
|
|
*
|
2021-09-23 10:26:49 +00:00
|
|
|
* @Given /^I launch the app( runtime)?$/
|
2021-05-26 15:09:09 +00:00
|
|
|
* @throws DriverException Issue with configuration or feature file
|
|
|
|
* @throws dml_exception Problem with Moodle setup
|
|
|
|
* @throws ExpectationException Problem with resizing window
|
|
|
|
*/
|
2021-09-23 10:26:49 +00:00
|
|
|
public function i_launch_the_app(string $runtime = '') {
|
2019-01-15 15:12:06 +00:00
|
|
|
// Check the app tag was set.
|
|
|
|
if (!$this->has_tag('app')) {
|
|
|
|
throw new DriverException('Requires @app tag on scenario or feature.');
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
// Go to page and prepare browser for app.
|
2021-09-23 10:26:49 +00:00
|
|
|
$this->prepare_browser(['skiponboarding' => empty($runtime)]);
|
2018-11-12 12:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
/**
|
|
|
|
* @Then /^I wait the app to restart$/
|
|
|
|
*/
|
|
|
|
public function i_wait_the_app_to_restart() {
|
|
|
|
// Wait window to reload.
|
|
|
|
$this->spin(function() {
|
|
|
|
return $this->evaluate_script("return !window.behat;");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Prepare testing runtime again.
|
2021-09-23 10:26:49 +00:00
|
|
|
$this->prepare_browser(['restart' => false]);
|
2021-06-10 13:15:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 15:43:47 +00:00
|
|
|
/**
|
|
|
|
* Finds elements in the app.
|
|
|
|
*
|
2021-12-01 13:24:43 +00:00
|
|
|
* @Then /^I should( not)? find (".+")( inside the split-view content)? in the app$/
|
2021-06-03 09:20:36 +00:00
|
|
|
* @param bool $not
|
2021-10-21 08:53:08 +00:00
|
|
|
* @param string $locator
|
2021-12-01 13:24:43 +00:00
|
|
|
* @param bool $insidesplitview
|
2021-04-27 15:43:47 +00:00
|
|
|
*/
|
2021-12-01 13:24:43 +00:00
|
|
|
public function i_find_in_the_app(bool $not, string $locator, bool $insidesplitview = false) {
|
2021-10-21 08:53:08 +00:00
|
|
|
$locator = $this->parse_element_locator($locator);
|
2021-06-03 09:20:36 +00:00
|
|
|
$locatorjson = json_encode($locator);
|
2021-12-01 13:24:43 +00:00
|
|
|
$insidesplitviewjson = json_encode($insidesplitview);
|
2021-04-27 15:43:47 +00:00
|
|
|
|
2021-12-01 13:24:43 +00:00
|
|
|
$this->spin(function() use ($not, $locatorjson, $insidesplitviewjson) {
|
|
|
|
$result = $this->evaluate_script("return window.behat.find($locatorjson, $insidesplitviewjson);");
|
2021-04-27 15:43:47 +00:00
|
|
|
|
|
|
|
if ($not && $result === 'OK') {
|
|
|
|
throw new DriverException('Error, found an item that should not be found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$not && $result !== 'OK') {
|
|
|
|
throw new DriverException('Error finding item - ' . $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2021-04-27 15:43:47 +00:00
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2021-11-25 12:04:25 +00:00
|
|
|
/**
|
|
|
|
* Scroll to an element in the app.
|
|
|
|
*
|
|
|
|
* @When /^I scroll to (".+") in the app$/
|
|
|
|
* @param string $locator
|
|
|
|
*/
|
|
|
|
public function i_scroll_to_in_the_app(string $locator) {
|
|
|
|
$locator = $this->parse_element_locator($locator);
|
|
|
|
$locatorjson = json_encode($locator);
|
|
|
|
|
|
|
|
$this->spin(function() use ($locatorjson) {
|
|
|
|
$result = $this->evaluate_script("return window.behat.scrollTo($locatorjson);");
|
|
|
|
|
|
|
|
if ($result !== 'OK') {
|
|
|
|
throw new DriverException('Error finding item - ' . $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:12:15 +00:00
|
|
|
/**
|
|
|
|
* Load more items in a list with an infinite loader.
|
|
|
|
*
|
|
|
|
* @When /^I (should not be able to )?load more items in the app$/
|
|
|
|
* @param bool $not
|
|
|
|
*/
|
|
|
|
public function i_load_more_items_in_the_app(bool $not = false) {
|
|
|
|
$this->spin(function() use ($not) {
|
|
|
|
$result = $this->evaluate_async_script('return window.behat.loadMoreItems();');
|
|
|
|
|
|
|
|
if ($not && $result !== 'ERROR: All items are already loaded') {
|
|
|
|
throw new DriverException('It should not have been possible to load more items');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$not && $result !== 'OK') {
|
|
|
|
throw new DriverException('Error loading more items - ' . $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2021-11-09 15:06:13 +00:00
|
|
|
/**
|
|
|
|
* Trigger swipe gesture.
|
|
|
|
*
|
|
|
|
* @When /^I swipe to the (left|right) in the app$/
|
|
|
|
* @param string $direction
|
|
|
|
*/
|
|
|
|
public function i_swipe_in_the_app(string $direction) {
|
|
|
|
$method = 'swipe' . ucwords($direction);
|
|
|
|
|
2022-01-10 13:18:10 +00:00
|
|
|
$this->evaluate_script("behat.getAngularInstance('ion-content', 'CoreSwipeNavigationDirective').$method()");
|
2021-11-09 15:06:13 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:40:42 +00:00
|
|
|
/**
|
|
|
|
* Check if elements are selected in the app.
|
|
|
|
*
|
2021-06-03 09:20:36 +00:00
|
|
|
* @Then /^(".+") should( not)? be selected in the app$/
|
2021-10-21 08:53:08 +00:00
|
|
|
* @param string $locator
|
2021-06-03 09:20:36 +00:00
|
|
|
* @param bool $not
|
2021-04-29 11:40:42 +00:00
|
|
|
*/
|
2021-10-21 08:53:08 +00:00
|
|
|
public function be_selected_in_the_app(string $locator, bool $not = false) {
|
|
|
|
$locator = $this->parse_element_locator($locator);
|
2021-06-03 09:20:36 +00:00
|
|
|
$locatorjson = json_encode($locator);
|
2021-04-29 11:40:42 +00:00
|
|
|
|
2021-06-03 09:20:36 +00:00
|
|
|
$this->spin(function() use ($locatorjson, $not) {
|
|
|
|
$result = $this->evaluate_script("return window.behat.isSelected($locatorjson);");
|
2021-04-29 11:40:42 +00:00
|
|
|
|
|
|
|
switch ($result) {
|
|
|
|
case 'YES':
|
|
|
|
if ($not) {
|
|
|
|
throw new ExpectationException("Item was selected and shouldn't have", $this->getSession()->getDriver());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'NO':
|
|
|
|
if (!$not) {
|
|
|
|
throw new ExpectationException("Item wasn't selected and should have", $this->getSession()->getDriver());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DriverException('Error finding item - ' . $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2021-04-29 11:40:42 +00:00
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Checks the Behat setup - tags and configuration.
|
|
|
|
*
|
|
|
|
* @throws DriverException
|
|
|
|
*/
|
|
|
|
protected function check_behat_setup() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
// Check JavaScript is enabled.
|
|
|
|
if (!$this->running_javascript()) {
|
|
|
|
throw new DriverException('The app requires JavaScript.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the config settings are defined.
|
2019-02-11 16:40:56 +00:00
|
|
|
if (empty($CFG->behat_ionic_wwwroot) && empty($CFG->behat_ionic_dirroot)) {
|
|
|
|
throw new DriverException('$CFG->behat_ionic_wwwroot or $CFG->behat_ionic_dirroot must be defined.');
|
2018-11-12 12:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixes the Moodle admin settings to allow mobile app use (if not already correct).
|
|
|
|
*
|
|
|
|
* @throws dml_exception If there is any problem changing Moodle settings
|
|
|
|
*/
|
|
|
|
protected function fix_moodle_setup() {
|
|
|
|
global $CFG, $DB;
|
|
|
|
|
|
|
|
// Configure Moodle settings to enable app web services.
|
|
|
|
if (!$CFG->enablewebservices) {
|
|
|
|
set_config('enablewebservices', 1);
|
|
|
|
}
|
|
|
|
if (!$CFG->enablemobilewebservice) {
|
|
|
|
set_config('enablemobilewebservice', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add 'Create token' and 'Use REST webservice' permissions to authenticated user role.
|
|
|
|
$userroleid = $DB->get_field('role', 'id', ['shortname' => 'user']);
|
|
|
|
$systemcontext = \context_system::instance();
|
|
|
|
role_change_permission($userroleid, $systemcontext, 'moodle/webservice:createtoken', CAP_ALLOW);
|
|
|
|
role_change_permission($userroleid, $systemcontext, 'webservice/rest:use', CAP_ALLOW);
|
|
|
|
|
|
|
|
// Check the value of the 'webserviceprotocols' config option. Due to weird behaviour
|
|
|
|
// in Behat with regard to config variables that aren't defined in a settings.php, the
|
|
|
|
// value in $CFG here may reflect a previous run, so get it direct from the database
|
|
|
|
// instead.
|
|
|
|
$field = $DB->get_field('config', 'value', ['name' => 'webserviceprotocols'], IGNORE_MISSING);
|
|
|
|
if (empty($field)) {
|
|
|
|
$protocols = [];
|
|
|
|
} else {
|
|
|
|
$protocols = explode(',', $field);
|
|
|
|
}
|
|
|
|
if (!in_array('rest', $protocols)) {
|
|
|
|
$protocols[] = 'rest';
|
|
|
|
set_config('webserviceprotocols', implode(',', $protocols));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable mobile service.
|
|
|
|
require_once($CFG->dirroot . '/webservice/lib.php');
|
|
|
|
$webservicemanager = new webservice();
|
|
|
|
$service = $webservicemanager->get_external_service_by_shortname(
|
|
|
|
MOODLE_OFFICIAL_MOBILE_SERVICE, MUST_EXIST);
|
|
|
|
if (!$service->enabled) {
|
|
|
|
$service->enabled = 1;
|
|
|
|
$webservicemanager->update_external_service($service);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If installed, also configure local_mobile plugin to enable additional features service.
|
|
|
|
$localplugins = core_component::get_plugin_list('local');
|
|
|
|
if (array_key_exists('mobile', $localplugins)) {
|
|
|
|
$service = $webservicemanager->get_external_service_by_shortname(
|
|
|
|
'local_mobile', MUST_EXIST);
|
|
|
|
if (!$service->enabled) {
|
|
|
|
$service->enabled = 1;
|
|
|
|
$webservicemanager->update_external_service($service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts an Ionic server if necessary, or uses an existing one.
|
|
|
|
*
|
|
|
|
* @return string URL to Ionic server
|
|
|
|
* @throws DriverException If there's a system error starting Ionic
|
|
|
|
*/
|
|
|
|
protected function start_or_reuse_ionic() {
|
|
|
|
global $CFG;
|
|
|
|
|
2019-02-13 05:55:34 +00:00
|
|
|
if (empty($CFG->behat_ionic_dirroot) && !empty($CFG->behat_ionic_wwwroot)) {
|
2018-11-12 12:11:06 +00:00
|
|
|
// Use supplied Ionic server which should already be running.
|
2019-02-11 16:40:56 +00:00
|
|
|
$url = $CFG->behat_ionic_wwwroot;
|
2018-11-12 12:11:06 +00:00
|
|
|
} else if (self::$ionicrunning) {
|
|
|
|
// Use existing Ionic instance launched previously.
|
|
|
|
$url = self::$ionicrunning->url;
|
|
|
|
} else {
|
|
|
|
// Open Ionic process in relevant path.
|
2019-02-11 16:40:56 +00:00
|
|
|
$path = realpath($CFG->behat_ionic_dirroot);
|
2018-11-12 12:11:06 +00:00
|
|
|
$stderrfile = $CFG->dataroot . '/behat/ionic-stderr.log';
|
|
|
|
$prefix = '';
|
|
|
|
// Except on Windows, use 'exec' so that we get the pid of the actual Node process
|
|
|
|
// and not the shell it uses to execute. You can't do exec on Windows; there is a
|
|
|
|
// bypass_shell option but it is not the same thing and isn't usable here.
|
|
|
|
if (!self::is_windows()) {
|
|
|
|
$prefix = 'exec ';
|
|
|
|
}
|
|
|
|
$process = proc_open($prefix . 'ionic serve --no-interactive --no-open',
|
|
|
|
[['pipe', 'r'], ['pipe', 'w'], ['file', $stderrfile, 'w']], $pipes, $path);
|
|
|
|
if ($process === false) {
|
|
|
|
throw new DriverException('Error starting Ionic process');
|
|
|
|
}
|
|
|
|
fclose($pipes[0]);
|
|
|
|
|
|
|
|
// Get pid - we will need this to kill the process.
|
|
|
|
$status = proc_get_status($process);
|
|
|
|
$pid = $status['pid'];
|
|
|
|
|
|
|
|
// Read data from stdout until the server comes online.
|
|
|
|
// Note: On Windows it is impossible to read simultaneously from stderr and stdout
|
|
|
|
// because stream_select and non-blocking I/O don't work on process pipes, so that is
|
|
|
|
// why stderr was redirected to a file instead. Also, this code is simpler.
|
|
|
|
$url = null;
|
|
|
|
$stdoutlog = '';
|
|
|
|
while (true) {
|
|
|
|
$line = fgets($pipes[1], 4096);
|
|
|
|
if ($line === false) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stdoutlog .= $line;
|
|
|
|
|
|
|
|
if (preg_match('~^\s*Local: (http\S*)~', $line, $matches)) {
|
|
|
|
$url = $matches[1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it failed, close the pipes and the process.
|
|
|
|
if (!$url) {
|
|
|
|
fclose($pipes[1]);
|
|
|
|
proc_close($process);
|
|
|
|
$logpath = $CFG->dataroot . '/behat/ionic-start.log';
|
|
|
|
$stderrlog = file_get_contents($stderrfile);
|
|
|
|
@unlink($stderrfile);
|
|
|
|
file_put_contents($logpath,
|
|
|
|
"Ionic startup log from " . date('c') .
|
|
|
|
"\n\n----STDOUT----\n$stdoutlog\n\n----STDERR----\n$stderrlog");
|
|
|
|
throw new DriverException('Unable to start Ionic. See ' . $logpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember the URL, so we can reuse it next time, and other details so we can kill
|
|
|
|
// the process.
|
|
|
|
self::$ionicrunning = (object)['url' => $url, 'process' => $process, 'pipes' => $pipes,
|
|
|
|
'pid' => $pid];
|
2020-01-17 14:30:23 +00:00
|
|
|
$url = self::$ionicrunning->url;
|
2018-11-12 12:11:06 +00:00
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes Ionic (if it was started) at end of test suite.
|
|
|
|
*
|
|
|
|
* @AfterSuite
|
|
|
|
*/
|
|
|
|
public static function close_ionic() {
|
|
|
|
if (self::$ionicrunning) {
|
|
|
|
fclose(self::$ionicrunning->pipes[1]);
|
|
|
|
|
|
|
|
if (self::is_windows()) {
|
|
|
|
// Using proc_terminate here does not work. It terminates the process but not any
|
|
|
|
// other processes it might have launched. Instead, we need to use an OS-specific
|
|
|
|
// mechanism to kill the process and children based on its pid.
|
|
|
|
exec('taskkill /F /T /PID ' . self::$ionicrunning->pid);
|
|
|
|
} else {
|
|
|
|
// On Unix this actually works, although only due to the 'exec' command inserted
|
|
|
|
// above.
|
|
|
|
proc_terminate(self::$ionicrunning->process);
|
|
|
|
}
|
|
|
|
self::$ionicrunning = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Goes to the app page and then sets up some initial JavaScript so we can use it.
|
|
|
|
*
|
|
|
|
* @param string $url App URL
|
|
|
|
* @throws DriverException If the app fails to load properly
|
|
|
|
*/
|
2021-09-23 10:26:49 +00:00
|
|
|
protected function prepare_browser(array $options = []) {
|
|
|
|
$restart = $options['restart'] ?? true;
|
|
|
|
$skiponboarding = $options['skiponboarding'] ?? true;
|
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
if ($restart) {
|
2021-09-29 12:14:48 +00:00
|
|
|
if ($this->apprunning) {
|
|
|
|
$this->notify_unload();
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
// Restart the browser and set its size.
|
|
|
|
$this->getSession()->restart();
|
|
|
|
$this->resize_window('360x720', true);
|
|
|
|
|
|
|
|
if (empty($this->ionicurl)) {
|
|
|
|
$this->ionicurl = $this->start_or_reuse_ionic();
|
|
|
|
}
|
2021-05-26 15:09:09 +00:00
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
// Visit the Ionic URL.
|
|
|
|
$this->getSession()->visit($this->ionicurl);
|
2021-09-29 12:14:48 +00:00
|
|
|
$this->notify_load();
|
|
|
|
|
|
|
|
$this->apprunning = true;
|
2021-06-10 13:15:38 +00:00
|
|
|
}
|
2021-04-27 15:43:47 +00:00
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
// Wait the application to load.
|
2021-05-26 15:09:09 +00:00
|
|
|
$this->spin(function($context) {
|
|
|
|
$title = $context->getSession()->getPage()->find('xpath', '//title');
|
|
|
|
|
|
|
|
if ($title) {
|
|
|
|
$text = $title->getHtml();
|
|
|
|
|
2021-11-24 09:47:10 +00:00
|
|
|
if ($text === 'Moodle App') {
|
2021-05-26 15:09:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new DriverException('Moodle app not found in browser');
|
|
|
|
}, false, 60);
|
2018-11-12 12:11:06 +00:00
|
|
|
|
|
|
|
// Run the scripts to install Moodle 'pending' checks.
|
2020-01-16 09:13:28 +00:00
|
|
|
$this->execute_script(file_get_contents(__DIR__ . '/app_behat_runtime.js'));
|
2018-11-12 12:11:06 +00:00
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
if ($restart) {
|
|
|
|
// Assert initial page.
|
2021-09-23 10:26:49 +00:00
|
|
|
$this->spin(function($context) use ($skiponboarding) {
|
2021-06-10 13:15:38 +00:00
|
|
|
$page = $context->getSession()->getPage();
|
|
|
|
$element = $page->find('xpath', '//page-core-login-site//input[@name="url"]');
|
|
|
|
|
|
|
|
if ($element) {
|
2021-09-23 10:26:49 +00:00
|
|
|
if (!$skiponboarding) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
// Wait for the onboarding modal to open, if any.
|
|
|
|
$this->wait_for_pending_js();
|
2020-05-20 11:29:26 +00:00
|
|
|
|
2021-11-24 09:47:10 +00:00
|
|
|
$element = $page->find('xpath', '//core-login-site-onboarding');
|
2021-05-26 15:09:09 +00:00
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
if ($element) {
|
2021-10-21 08:53:08 +00:00
|
|
|
$this->i_press_in_the_app('"Skip"');
|
2021-06-10 13:15:38 +00:00
|
|
|
}
|
2021-05-26 15:09:09 +00:00
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
// Login screen found.
|
|
|
|
return true;
|
2021-05-26 15:09:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
if ($page->find('xpath', '//page-core-mainmenu')) {
|
|
|
|
// Main menu found.
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-26 15:09:09 +00:00
|
|
|
|
2021-06-10 13:15:38 +00:00
|
|
|
throw new DriverException('Moodle app not launched properly');
|
|
|
|
}, false, 60);
|
|
|
|
}
|
2018-11-12 12:11:06 +00:00
|
|
|
|
|
|
|
// Continue only after JS finishes.
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:09:09 +00:00
|
|
|
protected function enter_site() {
|
|
|
|
if (!$this->is_in_login_page()) {
|
|
|
|
// Already in the site.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2021-11-24 09:47:10 +00:00
|
|
|
$this->i_set_the_field_in_the_app('Your site', $CFG->wwwroot);
|
|
|
|
$this->i_press_in_the_app('"Connect to your site"');
|
2021-05-26 15:09:09 +00:00
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
2019-01-15 16:51:38 +00:00
|
|
|
* Carries out the login steps for the app, assuming the user is on the app login page. Called
|
|
|
|
* from behat_auth.php.
|
2018-11-12 12:11:06 +00:00
|
|
|
*
|
|
|
|
* @param string $username Username (and password)
|
2019-01-15 16:51:38 +00:00
|
|
|
* @throws Exception Any error
|
2018-11-12 12:11:06 +00:00
|
|
|
*/
|
2019-01-15 16:51:38 +00:00
|
|
|
public function login(string $username) {
|
2018-11-12 12:11:06 +00:00
|
|
|
$this->i_set_the_field_in_the_app('Username', $username);
|
|
|
|
$this->i_set_the_field_in_the_app('Password', $username);
|
|
|
|
|
|
|
|
// Note there are two 'Log in' texts visible (the title and the button) so we have to use
|
2019-01-15 16:51:38 +00:00
|
|
|
// a 'near' value here.
|
2021-10-21 08:53:08 +00:00
|
|
|
$this->i_press_in_the_app('"Log in" near "Forgotten"');
|
2018-11-12 12:11:06 +00:00
|
|
|
|
|
|
|
// Wait until the main page appears.
|
|
|
|
$this->spin(
|
|
|
|
function($context, $args) {
|
|
|
|
$mainmenu = $context->getSession()->getPage()->find('xpath', '//page-core-mainmenu');
|
|
|
|
if ($mainmenu) {
|
|
|
|
return 'mainpage';
|
|
|
|
}
|
|
|
|
throw new DriverException('Moodle app main page not loaded after login');
|
|
|
|
}, false, 30);
|
|
|
|
|
|
|
|
// Wait for JS to finish as well.
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2021-11-29 09:37:04 +00:00
|
|
|
/**
|
|
|
|
* User enters a course in the app.
|
|
|
|
*
|
2021-12-01 16:33:15 +00:00
|
|
|
* @Given /^I enter the course "(.+?)"(?: as "(.+)")? in the app$/
|
2021-11-29 09:37:04 +00:00
|
|
|
* @param string $coursename Course name
|
|
|
|
* @throws DriverException If the button push doesn't work
|
|
|
|
*/
|
2021-12-01 16:33:15 +00:00
|
|
|
public function i_enter_the_course_in_the_app(string $coursename, ?string $username = null) {
|
|
|
|
if (!is_null($username)) {
|
|
|
|
$this->i_enter_the_app();
|
|
|
|
$this->login($username);
|
|
|
|
}
|
|
|
|
|
|
|
|
$mycoursesfound = $this->evaluate_script("return window.behat.find({ text: 'My courses', near: { text: 'Messages' } });");
|
|
|
|
|
|
|
|
if ($mycoursesfound !== 'OK') {
|
2021-11-29 09:37:04 +00:00
|
|
|
// My courses not present enter from Dashboard.
|
|
|
|
$this->i_press_in_the_app('"Home" near "Messages"');
|
|
|
|
$this->i_press_in_the_app('"Dashboard"');
|
|
|
|
$this->i_press_in_the_app('"'.$coursename.'" near "Course overview"');
|
|
|
|
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
|
2021-12-01 16:33:15 +00:00
|
|
|
return;
|
2021-11-29 09:37:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 16:33:15 +00:00
|
|
|
$this->i_press_in_the_app('"My courses" near "Messages"');
|
2021-11-29 09:37:04 +00:00
|
|
|
$this->i_press_in_the_app('"'.$coursename.'"');
|
|
|
|
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Presses standard buttons in the app.
|
|
|
|
*
|
2021-11-24 09:47:10 +00:00
|
|
|
* @Given /^I press the (back|more menu|page menu|user menu|main menu) button in the app$/
|
2018-11-12 12:11:06 +00:00
|
|
|
* @param string $button Button type
|
|
|
|
* @throws DriverException If the button push doesn't work
|
|
|
|
*/
|
|
|
|
public function i_press_the_standard_button_in_the_app(string $button) {
|
2021-06-03 09:20:36 +00:00
|
|
|
$this->spin(function() use ($button) {
|
|
|
|
$result = $this->evaluate_script("return window.behat.pressStandard('$button');");
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
if ($result !== 'OK') {
|
|
|
|
throw new DriverException('Error pressing standard button - ' . $result);
|
|
|
|
}
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
return true;
|
|
|
|
});
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2021-05-11 16:54:18 +00:00
|
|
|
/**
|
2021-06-10 13:14:51 +00:00
|
|
|
* Receives push notifications.
|
2021-05-11 16:54:18 +00:00
|
|
|
*
|
2021-06-10 13:14:51 +00:00
|
|
|
* @Given /^I receive a push notification in the app for:$/
|
2021-05-11 16:54:18 +00:00
|
|
|
* @param TableNode $data
|
|
|
|
*/
|
2021-06-10 13:14:51 +00:00
|
|
|
public function i_receive_a_push_notification(TableNode $data) {
|
2021-05-11 16:54:18 +00:00
|
|
|
global $DB, $CFG;
|
|
|
|
|
|
|
|
$data = (object) $data->getColumnsHash()[0];
|
|
|
|
$module = $DB->get_record('course_modules', ['idnumber' => $data->module]);
|
|
|
|
$discussion = $DB->get_record('forum_discussions', ['name' => $data->discussion]);
|
|
|
|
$notification = json_encode([
|
|
|
|
'site' => md5($CFG->wwwroot . $data->username),
|
|
|
|
'courseid' => $discussion->course,
|
|
|
|
'moodlecomponent' => 'mod_forum',
|
|
|
|
'name' => 'posts',
|
|
|
|
'contexturl' => '',
|
|
|
|
'notif' => 1,
|
|
|
|
'customdata' => [
|
|
|
|
'discussionid' => $discussion->id,
|
|
|
|
'cmid' => $module->id,
|
|
|
|
'instance' => $discussion->forum,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->evaluate_script("return window.pushNotifications.notificationClicked($notification)");
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:32:31 +00:00
|
|
|
/**
|
|
|
|
* Replace arguments from the content in the given activity field.
|
|
|
|
*
|
|
|
|
* @Given /^I replace the arguments in "([^"]+)" "([^"]+)"$/
|
|
|
|
*/
|
|
|
|
public function i_replace_arguments_in_the_activity(string $idnumber, string $field) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$coursemodule = $DB->get_record('course_modules', compact('idnumber'));
|
|
|
|
$module = $DB->get_record('modules', ['id' => $coursemodule->module]);
|
|
|
|
$activity = $DB->get_record($module->name, ['id' => $coursemodule->instance]);
|
|
|
|
|
|
|
|
$DB->update_record($module->name, [
|
|
|
|
'id' => $coursemodule->instance,
|
|
|
|
$field => $this->replace_arguments($activity->{$field}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:14:51 +00:00
|
|
|
/**
|
|
|
|
* Opens a custom link.
|
|
|
|
*
|
|
|
|
* @Given /^I open a custom link in the app for:$/
|
|
|
|
*/
|
|
|
|
public function i_open_a_custom_link(TableNode $data) {
|
|
|
|
global $DB, $CFG;
|
|
|
|
|
|
|
|
$data = (object) $data->getColumnsHash()[0];
|
|
|
|
$discussion = $DB->get_record('forum_discussions', ['name' => $data->discussion]);
|
|
|
|
$pageurl = "{$CFG->behat_wwwroot}/mod/forum/discuss.php?d={$discussion->id}";
|
|
|
|
$url = "moodlemobile://link=" . urlencode($pageurl);
|
|
|
|
|
|
|
|
$this->evaluate_script("return window.urlSchemes.handleCustomURL('$url')");
|
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Closes a popup by clicking on the 'backdrop' behind it.
|
|
|
|
*
|
|
|
|
* @Given /^I close the popup in the app$/
|
|
|
|
* @throws DriverException If there isn't a popup to close
|
|
|
|
*/
|
|
|
|
public function i_close_the_popup_in_the_app() {
|
2021-06-03 09:20:36 +00:00
|
|
|
$this->spin(function() {
|
2020-01-16 09:13:28 +00:00
|
|
|
$result = $this->evaluate_script("return window.behat.closePopup();");
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
if ($result !== 'OK') {
|
|
|
|
throw new DriverException('Error closing popup - ' . $result);
|
|
|
|
}
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
return true;
|
|
|
|
});
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clicks on / touches something that is visible in the app.
|
|
|
|
*
|
|
|
|
* Note it is difficult to use the standard 'click on' or 'press' steps because those do not
|
|
|
|
* distinguish visible items and the app always has many non-visible items in the DOM.
|
|
|
|
*
|
2021-06-03 09:20:36 +00:00
|
|
|
* @Then /^I press (".+") in the app$/
|
2021-10-21 08:53:08 +00:00
|
|
|
* @param string $locator Element locator
|
2018-11-12 12:11:06 +00:00
|
|
|
* @throws DriverException If the press doesn't work
|
|
|
|
*/
|
2021-10-21 08:53:08 +00:00
|
|
|
public function i_press_in_the_app(string $locator) {
|
|
|
|
$locator = $this->parse_element_locator($locator);
|
2021-06-03 09:20:36 +00:00
|
|
|
$locatorjson = json_encode($locator);
|
|
|
|
|
|
|
|
$this->spin(function() use ($locatorjson) {
|
|
|
|
$result = $this->evaluate_script("return window.behat.press($locatorjson);");
|
|
|
|
|
|
|
|
if ($result !== 'OK') {
|
|
|
|
throw new DriverException('Error pressing item - ' . $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->wait_for_pending_js();
|
2018-11-12 12:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-18 13:53:31 +00:00
|
|
|
* Select an item from a list of options, such as a radio button.
|
2018-11-12 12:11:06 +00:00
|
|
|
*
|
2021-05-18 13:53:31 +00:00
|
|
|
* It may be necessary to use this step instead of "I press..." because radio buttons in Ionic are initialized
|
|
|
|
* with JavaScript, and clicks may not work until they are initialized properly which may cause flaky tests due
|
|
|
|
* to race conditions.
|
2018-11-12 12:11:06 +00:00
|
|
|
*
|
2021-06-03 09:20:36 +00:00
|
|
|
* @Then /^I (unselect|select) (".+") in the app$/
|
|
|
|
* @param string $selectedtext
|
2021-10-21 08:53:08 +00:00
|
|
|
* @param string $locator
|
2018-11-12 12:11:06 +00:00
|
|
|
* @throws DriverException If the press doesn't work
|
|
|
|
*/
|
2021-10-21 08:53:08 +00:00
|
|
|
public function i_select_in_the_app(string $selectedtext, string $locator) {
|
2021-05-31 14:39:45 +00:00
|
|
|
$selected = $selectedtext === 'select' ? 'YES' : 'NO';
|
2021-10-21 08:53:08 +00:00
|
|
|
$locator = $this->parse_element_locator($locator);
|
2021-06-03 09:20:36 +00:00
|
|
|
$locatorjson = json_encode($locator);
|
2021-05-31 14:39:45 +00:00
|
|
|
|
2021-06-03 09:20:36 +00:00
|
|
|
$this->spin(function() use ($selectedtext, $selected, $locatorjson) {
|
2021-05-31 14:39:45 +00:00
|
|
|
// Don't do anything if the item is already in the expected state.
|
2021-06-03 09:20:36 +00:00
|
|
|
$result = $this->evaluate_script("return window.behat.isSelected($locatorjson);");
|
2021-05-31 14:39:45 +00:00
|
|
|
|
|
|
|
if ($result === $selected) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Press item.
|
2021-06-03 09:20:36 +00:00
|
|
|
$result = $this->evaluate_script("return window.behat.press($locatorjson);");
|
2021-05-31 14:39:45 +00:00
|
|
|
|
|
|
|
if ($result !== 'OK') {
|
|
|
|
throw new DriverException('Error pressing item - ' . $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that it worked as expected.
|
2021-06-03 09:20:36 +00:00
|
|
|
$result = $this->evaluate_script("return window.behat.isSelected($locatorjson);");
|
2021-05-31 14:39:45 +00:00
|
|
|
|
|
|
|
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();
|
2019-01-15 16:51:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 15:09:09 +00:00
|
|
|
/**
|
|
|
|
* Check whether the current page is the login form.
|
|
|
|
*/
|
|
|
|
protected function is_in_login_page(): bool {
|
|
|
|
$page = $this->getSession()->getPage();
|
|
|
|
$logininput = $page->find('xpath', '//page-core-login-site//input[@name="url"]');
|
|
|
|
|
|
|
|
return !is_null($logininput);
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Sets a field to the given text value in the app.
|
|
|
|
*
|
|
|
|
* Currently this only works for input fields which must be identified using a partial or
|
|
|
|
* exact match on the placeholder text.
|
|
|
|
*
|
2021-11-29 09:04:21 +00:00
|
|
|
* @Given /^I set the field "((?:[^"]|\\")+)" to "((?:[^"]|\\")*)" in the app$/
|
2018-11-12 12:11:06 +00:00
|
|
|
* @param string $field Text identifying field
|
|
|
|
* @param string $value Value for field
|
|
|
|
* @throws DriverException If the field set doesn't work
|
|
|
|
*/
|
|
|
|
public function i_set_the_field_in_the_app(string $field, string $value) {
|
2021-06-03 09:20:36 +00:00
|
|
|
$field = addslashes_js($field);
|
|
|
|
$value = addslashes_js($value);
|
|
|
|
|
|
|
|
$this->spin(function() use ($field, $value) {
|
|
|
|
$result = $this->evaluate_script("return window.behat.setField(\"$field\", \"$value\");");
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
if ($result !== 'OK') {
|
|
|
|
throw new DriverException('Error setting field - ' . $result);
|
|
|
|
}
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
return true;
|
|
|
|
});
|
2021-06-03 09:20:36 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
$this->wait_for_pending_js();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that the current header stripe in the app contains the expected text.
|
|
|
|
*
|
|
|
|
* This can be used to see if the app went to the expected page.
|
|
|
|
*
|
2021-06-03 09:20:36 +00:00
|
|
|
* @Then /^the header should be "((?:[^"]|\\")+)" in the app$/
|
2018-11-12 12:11:06 +00:00
|
|
|
* @param string $text Expected header text
|
|
|
|
* @throws DriverException If the header can't be retrieved
|
|
|
|
* @throws ExpectationException If the header text is different to the expected value
|
|
|
|
*/
|
|
|
|
public function the_header_should_be_in_the_app(string $text) {
|
2021-04-27 15:43:47 +00:00
|
|
|
$this->spin(function() use ($text) {
|
2020-01-16 09:13:28 +00:00
|
|
|
$result = $this->evaluate_script('return window.behat.getHeader();');
|
2021-04-27 15:43:47 +00:00
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
if (substr($result, 0, 3) !== 'OK:') {
|
|
|
|
throw new DriverException('Error getting header - ' . $result);
|
|
|
|
}
|
2021-04-27 15:43:47 +00:00
|
|
|
|
|
|
|
$header = substr($result, 3);
|
|
|
|
if (trim($header) !== trim($text)) {
|
|
|
|
throw new ExpectationException(
|
|
|
|
"The header text was not as expected: '$header'",
|
|
|
|
$this->getSession()->getDriver()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-11-12 12:11:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:09:47 +00:00
|
|
|
/**
|
|
|
|
* Check that the app opened a new browser tab.
|
|
|
|
*
|
2021-10-08 13:11:02 +00:00
|
|
|
* @Given /^the app should( not)? have opened a browser tab(?: with url "(?P<pattern>[^"]+)")?$/
|
2021-06-03 09:20:36 +00:00
|
|
|
* @param bool $not
|
2021-10-08 13:11:02 +00:00
|
|
|
* @param string $urlpattern
|
2021-05-26 15:09:47 +00:00
|
|
|
*/
|
2021-10-08 13:11:02 +00:00
|
|
|
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;
|
2021-05-26 15:09:47 +00:00
|
|
|
|
2021-10-08 13:11:02 +00:00
|
|
|
if ((!$not && !$openedbrowsertab) || ($not && $openedbrowsertab && is_null($urlpattern))) {
|
2021-05-26 15:09:47 +00:00
|
|
|
throw new ExpectationException(
|
|
|
|
$not
|
|
|
|
? 'Did not expect the app to have opened a browser tab'
|
|
|
|
: 'Expected the app to have opened a browser tab',
|
|
|
|
$this->getSession()->getDriver()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:11:02 +00:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:09:47 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Switches to a newly-opened browser tab.
|
|
|
|
*
|
|
|
|
* This assumes the app opened a new tab.
|
|
|
|
*
|
|
|
|
* @Given /^I switch to the browser tab opened by the app$/
|
|
|
|
* @throws DriverException If there aren't exactly 2 tabs open
|
|
|
|
*/
|
|
|
|
public function i_switch_to_the_browser_tab_opened_by_the_app() {
|
|
|
|
$names = $this->getSession()->getWindowNames();
|
|
|
|
if (count($names) !== 2) {
|
|
|
|
throw new DriverException('Expected to see 2 tabs open, not ' . count($names));
|
|
|
|
}
|
|
|
|
$this->getSession()->switchToWindow($names[1]);
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:39:47 +00:00
|
|
|
/**
|
|
|
|
* Force cron tasks instead of waiting for the next scheduled execution.
|
|
|
|
*
|
|
|
|
* @When /^I run cron tasks in the app$/
|
|
|
|
*/
|
|
|
|
public function i_run_cron_tasks_in_the_app() {
|
|
|
|
$session = $this->getSession();
|
|
|
|
|
|
|
|
// Force cron tasks execution and wait until they are completed.
|
|
|
|
$operationid = random_string();
|
|
|
|
|
|
|
|
$session->executeScript(
|
|
|
|
"cronProvider.forceSyncExecution().then(() => { window['behat_{$operationid}_completed'] = true; });"
|
|
|
|
);
|
|
|
|
$this->spin(
|
|
|
|
function() use ($session, $operationid) {
|
|
|
|
return $session->evaluateScript("window['behat_{$operationid}_completed'] || false");
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
60,
|
|
|
|
new ExpectationException('Forced cron tasks in the app took too long to complete', $session)
|
|
|
|
);
|
|
|
|
|
2021-06-21 08:36:26 +00:00
|
|
|
// Trigger Angular change detection
|
2021-11-24 09:47:10 +00:00
|
|
|
$session->executeScript('ngZone.run(() => {});');
|
2021-06-07 15:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait until loading has finished.
|
|
|
|
*
|
|
|
|
* @When /^I wait loading to finish in the app$/
|
|
|
|
*/
|
|
|
|
public function i_wait_loading_to_finish_in_the_app() {
|
|
|
|
$session = $this->getSession();
|
|
|
|
|
|
|
|
$this->spin(
|
|
|
|
function() use ($session) {
|
2021-11-24 09:47:10 +00:00
|
|
|
$session->executeScript('ngZone.run(() => {});');
|
2021-06-07 15:39:47 +00:00
|
|
|
|
|
|
|
$nodes = $this->find_all('css', 'core-loading ion-spinner');
|
|
|
|
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
if (!$node->isVisible()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
60,
|
|
|
|
new ExpectationException('"Loading took too long to complete', $session)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
/**
|
|
|
|
* Closes the current browser tab.
|
|
|
|
*
|
|
|
|
* This assumes it was opened by the app and you will now get back to the app.
|
|
|
|
*
|
|
|
|
* @Given /^I close the browser tab opened by the app$/
|
|
|
|
* @throws DriverException If there aren't exactly 2 tabs open
|
|
|
|
*/
|
|
|
|
public function i_close_the_browser_tab_opened_by_the_app() {
|
|
|
|
$names = $this->getSession()->getWindowNames();
|
|
|
|
if (count($names) !== 2) {
|
|
|
|
throw new DriverException('Expected to see 2 tabs open, not ' . count($names));
|
|
|
|
}
|
2021-10-08 13:11:02 +00:00
|
|
|
// Make sure the browser tab is selected.
|
|
|
|
if ($this->getSession()->getWindowName() !== $names[1]) {
|
|
|
|
$this->getSession()->switchToWindow($names[1]);
|
|
|
|
}
|
|
|
|
|
2020-01-16 09:13:28 +00:00
|
|
|
$this->execute_script('window.close()');
|
2018-11-12 12:11:06 +00:00
|
|
|
$this->getSession()->switchToWindow($names[0]);
|
|
|
|
}
|
2019-05-31 11:51:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch navigator online mode.
|
|
|
|
*
|
2021-06-03 09:20:36 +00:00
|
|
|
* @Given /^I switch offline mode to "(true|false)"$/
|
2019-05-31 11:51:17 +00:00
|
|
|
* @param string $offline New value for navigator online mode
|
|
|
|
* @throws DriverException If the navigator.online mode is not available
|
|
|
|
*/
|
|
|
|
public function i_switch_offline_mode(string $offline) {
|
2021-06-03 09:20:36 +00:00
|
|
|
$this->execute_script("appProvider.setForceOffline($offline);");
|
2019-05-31 11:51:17 +00:00
|
|
|
}
|
2021-06-03 09:20:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse an element locator string.
|
|
|
|
*
|
|
|
|
* @param string $text Element locator string.
|
|
|
|
* @return object
|
|
|
|
*/
|
2021-10-21 08:53:08 +00:00
|
|
|
public function parse_element_locator(string $text): object {
|
2021-06-09 08:21:02 +00:00
|
|
|
preg_match('/^"((?:[^"]|\\")*?)"(?: "([^"]*?)")?(?: near "((?:[^"]|\\")*?)"(?: "([^"]*?)")?)?$/', $text, $matches);
|
2021-06-03 09:20:36 +00:00
|
|
|
|
|
|
|
$locator = [
|
|
|
|
'text' => str_replace('\\"', '"', $matches[1]),
|
|
|
|
'selector' => $matches[2] ?? null,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty($matches[3])) {
|
|
|
|
$locator['near'] = (object) [
|
|
|
|
'text' => str_replace('\\"', '"', $matches[3]),
|
|
|
|
'selector' => $matches[4] ?? null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return (object) $locator;
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:39:47 +00:00
|
|
|
/**
|
|
|
|
* Replaces $WWWROOT for the url of the Moodle site.
|
|
|
|
*
|
|
|
|
* @Transform /^(.*\$WWWROOT.*)$/
|
|
|
|
* @param string $text Text.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function replace_wwwroot($text) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return str_replace('$WWWROOT', $CFG->behat_wwwroot, $text);
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:32:31 +00:00
|
|
|
/**
|
|
|
|
* Replace arguments with the format "${activity:field}" from a string, where "activity" is
|
|
|
|
* the idnumber of an activity and "field" is the activity's field to get replacement from.
|
|
|
|
*
|
|
|
|
* At the moment, the only field supported is "cmid", the id of the course module for this activity.
|
|
|
|
*
|
|
|
|
* @param string $text Original text.
|
|
|
|
* @return string Text with arguments replaced.
|
|
|
|
*/
|
|
|
|
protected function replace_arguments(string $text): string {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
preg_match_all("/\\$\\{([^:}]+):([^}]+)\\}/", $text, $matches);
|
|
|
|
|
|
|
|
foreach ($matches[0] as $index => $match) {
|
|
|
|
switch ($matches[2][$index]) {
|
|
|
|
case 'cmid':
|
|
|
|
$coursemodule = $DB->get_record('course_modules', ['idnumber' => $matches[1][$index]]);
|
|
|
|
$text = str_replace($match, $coursemodule->id, $text);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2021-09-29 12:14:48 +00:00
|
|
|
/**
|
|
|
|
* Notify to listeners that the app was just loaded.
|
|
|
|
*/
|
|
|
|
private function notify_load(): void {
|
|
|
|
foreach (self::$listeners as $listener) {
|
|
|
|
$listener->on_app_load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify to listeners that the app is about to be unloaded.
|
|
|
|
*/
|
|
|
|
private function notify_unload(): void {
|
|
|
|
foreach (self::$listeners as $listener) {
|
|
|
|
$listener->on_app_unload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:12:15 +00:00
|
|
|
/**
|
|
|
|
* Evaludate a script that returns a Promise.
|
|
|
|
*
|
|
|
|
* @param string $script
|
|
|
|
* @return mixed Resolved promise result.
|
|
|
|
*/
|
|
|
|
private function evaluate_async_script(string $script) {
|
|
|
|
$script = preg_replace('/^return\s+/', '', $script);
|
|
|
|
$script = preg_replace('/;$/', '', $script);
|
|
|
|
$start = microtime(true);
|
|
|
|
$promisevariable = 'PROMISE_RESULT_' . time();
|
|
|
|
$timeout = self::get_timeout();
|
|
|
|
|
|
|
|
$this->evaluate_script("Promise.resolve($script)
|
|
|
|
.then(result => window.$promisevariable = result)
|
|
|
|
.catch(error => window.$promisevariable = 'Async code rejected: ' + error?.message);");
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (microtime(true) - $start > $timeout) {
|
|
|
|
throw new DriverException("Async script not resolved after $timeout seconds");
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(100000);
|
|
|
|
} while (!$this->evaluate_script("return '$promisevariable' in window;"));
|
|
|
|
|
|
|
|
$result = $this->evaluate_script("return window.$promisevariable;");
|
|
|
|
|
|
|
|
$this->evaluate_script("delete window.$promisevariable;");
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-11-12 12:11:06 +00:00
|
|
|
}
|