From faec9c4a8e44f2e74a36252170293514f5758b8e Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 15 Mar 2022 10:36:51 +0100 Subject: [PATCH 1/2] MOBILE-3833 config: Fix patching in tests --- src/core/constants.ts | 1 + src/core/services/config.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/constants.ts b/src/core/constants.ts index 0fcd0ddd3..cabb5585f 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -153,6 +153,7 @@ export class CoreConstants { static enableDevTools(): boolean { // @todo [4.0] This is not the proper way to check for development tools, we should rely only on the BUILD variable. return this.BUILD.isDevelopment + || this.BUILD.isTesting || this.CONFIG.versionname.includes('-dev'); } diff --git a/src/core/services/config.ts b/src/core/services/config.ts index 012e7c7bd..eb2221487 100644 --- a/src/core/services/config.ts +++ b/src/core/services/config.ts @@ -164,15 +164,21 @@ export class CoreConfigProvider { * Update config with the given values. * * @param config Config updates. - * @param reset Whether to reset environment before applying the patch. + * @param options Patching options. + * - reset: Whether to reset environment before applying the patch. + * - patchDefault: Whether to patch default values as well. */ - patchEnvironment(config: Partial, reset: boolean = false): void { + patchEnvironment(config: Partial, options: Partial<{ reset: boolean; patchDefault: boolean }> = {}): void { this.defaultEnvironment = this.defaultEnvironment ?? { ...CoreConstants.CONFIG }; - if (reset) { + if (options.reset) { this.resetEnvironmentSilently(); } + if (options.patchDefault) { + Object.assign(this.defaultEnvironment, config); + } + Object.assign(CoreConstants.CONFIG, config); CoreEvents.trigger(CoreConfigProvider.ENVIRONMENT_UPDATED, CoreConstants.CONFIG); } @@ -199,7 +205,7 @@ export class CoreConfigProvider { return; } - this.patchEnvironment(JSON.parse(CoreBrowser.getCookie('MoodleAppConfig') ?? '{}')); + this.patchEnvironment(JSON.parse(CoreBrowser.getCookie('MoodleAppConfig') ?? '{}'), { patchDefault: true }); } /** From 994d1bb1493bed779ed73911dd49339aab7e7683 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 15 Mar 2022 10:38:10 +0100 Subject: [PATCH 2/2] MOBILE-3833 behat: Document performance config --- tests/behat/classes/performance_measure.php | 39 ++++++++++++++++++++- tests/behat/performance.feature | 19 ++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/behat/classes/performance_measure.php b/tests/behat/classes/performance_measure.php index 3348fba36..4e3c8ee7d 100644 --- a/tests/behat/classes/performance_measure.php +++ b/tests/behat/classes/performance_measure.php @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +use Behat\Mink\Exception\DriverException; +use Facebook\WebDriver\Exception\InvalidArgumentException; use Moodle\BehatExtension\Driver\WebDriver; /** @@ -268,7 +270,7 @@ class performance_measure implements behat_app_listener { $scripting = 0; $styling = 0; $networking = 0; - $logs = $this->driver->getWebDriver()->manage()->getLog('performance'); + $logs = $this->getPerformanceLogs(); foreach ($logs as $log) { // TODO this should filter by end time as well, but it seems like the timestamps are not @@ -304,4 +306,39 @@ class performance_measure implements behat_app_listener { $this->networking = $networking; } + /** + * Get performance logs. + * + * @return array Performance logs. + */ + private function getPerformanceLogs(): array { + try { + return $this->driver->getWebDriver()->manage()->getLog('performance'); + } catch (InvalidArgumentException $e) { + throw new DriverException( + implode("\n", [ + "It wasn't possible to get performance logs, make sure that you have configured the following capabilities:", + "", + "\$CFG->behat_profiles = [", + " 'default' => [", + " 'browser' => 'chrome',", + " 'wd_host' => 'http://selenium:4444/wd/hub',", + " 'capabilities' => [", + " 'extra_capabilities' => [", + " 'goog:loggingPrefs' => ['performance' => 'ALL'],", + " 'chromeOptions' => [", + " 'perfLoggingPrefs' => [", + " 'traceCategories' => 'devtools.timeline',", + " ],", + " ],", + " ],", + " ],", + " ],", + ");", + "", + ]) + ); + } + } + } diff --git a/tests/behat/performance.feature b/tests/behat/performance.feature index f90a81ce9..39a41c5d1 100644 --- a/tests/behat/performance.feature +++ b/tests/behat/performance.feature @@ -1,6 +1,25 @@ @app @javascript @performance Feature: Measure performance. + # In order to run performance tests, you need to add the following capabilities to your Behat configuration: + # + # $CFG->behat_profiles = [ + # 'default' => [ + # 'browser' => 'chrome', + # 'wd_host' => 'http://selenium:4444/wd/hub', + # 'capabilities' => [ + # 'extra_capabilities' => [ + # 'goog:loggingPrefs' => ['performance' => 'ALL'], + # 'chromeOptions' => [ + # 'perfLoggingPrefs' => [ + # 'traceCategories' => 'devtools.timeline', + # ], + # ], + # ], + # ], + # ], + # ]; + Background: Given the following "users" exist: | username |