From 97862f40f577fefec0c63ec8969b422e2ed3bb92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Mon, 9 May 2022 10:40:57 +0200 Subject: [PATCH] MOBILE-4061 behat: Native handle of skip on boarding --- .../tests/behat/behat_app.php | 30 +++++-------------- .../login/tests/behat/basic_usage.feature | 8 +++++ src/testing/services/behat-runtime.ts | 29 +++++++++++++++++- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/local-moodleappbehat/tests/behat/behat_app.php b/local-moodleappbehat/tests/behat/behat_app.php index 2d59da7ec..098e1a06f 100644 --- a/local-moodleappbehat/tests/behat/behat_app.php +++ b/local-moodleappbehat/tests/behat/behat_app.php @@ -470,7 +470,6 @@ class behat_app extends behat_base { */ protected function prepare_browser(array $options = []) { $restart = $options['restart'] ?? true; - $skiponboarding = $options['skiponboarding'] ?? true; if ($restart) { if ($this->apprunning) { @@ -509,32 +508,23 @@ class behat_app extends behat_base { try { // Init Behat JavaScript runtime. - $this->execute_script('window.behatInit();'); + + $initOptions = new StdClass(); + $initOptions->skipOnBoarding = $options['skiponboarding'] ?? true; + $initOptions->configOverrides = $this->appconfig; + + $this->execute_script('window.behatInit(' . json_encode($initOptions) . ');'); } catch (Exception $error) { throw new DriverException('Moodle app not running or not running on Automated mode.'); } - if ($restart) { // Assert initial page. - $this->spin(function($context) use ($skiponboarding) { + $this->spin(function($context) { $page = $context->getSession()->getPage(); $element = $page->find('xpath', '//page-core-login-site//input[@name="url"]'); if ($element) { - if (!$skiponboarding) { - return true; - } - - // Wait for the onboarding modal to open, if any. - $this->wait_for_pending_js(); - - $element = $page->find('xpath', '//core-login-site-onboarding'); - - if ($element) { - $this->i_press_in_the_app('"Skip"'); - } - // Login screen found. return true; } @@ -548,12 +538,6 @@ class behat_app extends behat_base { }, false, 60); } - // Prepare testing config. - $configoverrides = json_encode($this->appconfig); - - $this->evaluate_script("document.cookie='MoodleAppConfig=$configoverrides'"); - $this->evaluate_script("configProvider.patchEnvironment($configoverrides)"); - // Continue only after JS finishes. $this->wait_for_pending_js(); } diff --git a/src/core/features/login/tests/behat/basic_usage.feature b/src/core/features/login/tests/behat/basic_usage.feature index b94698e0b..4230ddfa9 100755 --- a/src/core/features/login/tests/behat/basic_usage.feature +++ b/src/core/features/login/tests/behat/basic_usage.feature @@ -17,6 +17,14 @@ Feature: Test basic usage of login in app | student2 | C1 | student | | teacher1 | C1 | editingteacher | + Scenario: Skip on boarding + When I launch the app runtime + Then I should find "Welcome to the Moodle App!" in the app + + When I press "Skip" in the app + Then I should not find "Skip" in the app + And I should find "Connect to Moodle" in the app + Scenario: Add a new account in the app & Site name in displayed when adding a new account When I enter the app And I press the back button in the app diff --git a/src/testing/services/behat-runtime.ts b/src/testing/services/behat-runtime.ts index 10d573b74..25fb88f35 100644 --- a/src/testing/services/behat-runtime.ts +++ b/src/testing/services/behat-runtime.ts @@ -15,13 +15,21 @@ import { TestsBehatDomUtils } from './behat-dom'; import { TestsBehatBlocking } from './behat-blocking'; import { CoreCustomURLSchemes } from '@services/urlschemes'; +import { CoreLoginHelperProvider } from '@features/login/services/login-helper'; +import { CoreConfig } from '@services/config'; +import { EnvironmentConfig } from '@/types/config'; /** * Behat runtime servive with public API. */ export class TestsBehatRuntime { - static init(): void { + /** + * Init behat functions and set options like skipping onboarding. + * + * @param options Options to set on the app. + */ + static init(options?: TestsBehatInitOptions): void { TestsBehatBlocking.init(); (window as BehatTestsWindow).behat = { @@ -38,6 +46,20 @@ export class TestsBehatRuntime { setField: TestsBehatRuntime.setField, handleCustomURL: TestsBehatRuntime.handleCustomURL, }; + + if (!options) { + return; + } + + if (options.skipOnBoarding === true) { + CoreConfig.set(CoreLoginHelperProvider.ONBOARDING_DONE, 1); + } + + if (options.configOverrides) { + // Set the cookie so it's maintained between reloads. + document.cookie = 'MoodleAppConfig=' + JSON.stringify(options.configOverrides); + CoreConfig.patchEnvironment(options.configOverrides); + } } /** @@ -407,3 +429,8 @@ export type TestBehatElementLocator = { near?: TestBehatElementLocator; selector?: string; }; + +export type TestsBehatInitOptions = { + skipOnBoarding?: boolean; + configOverrides?: Partial; +};