Skip to content

Commit 83eab88

Browse files
committed
MOBILE-3320 behat: Implement step to wait restart
1 parent f075c64 commit 83eab88

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

tests/behat/behat_app.php

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ public function i_launch_the_app() {
100100
$this->prepare_browser();
101101
}
102102

103+
/**
104+
* @Then /^I wait the app to restart$/
105+
*/
106+
public function i_wait_the_app_to_restart() {
107+
// Wait window to reload.
108+
$this->spin(function() {
109+
return $this->evaluate_script("return !window.behat;");
110+
});
111+
112+
// Prepare testing runtime again.
113+
$this->prepare_browser(false);
114+
}
115+
103116
/**
104117
* Finds elements in the app.
105118
*
@@ -347,24 +360,28 @@ public static function close_ionic() {
347360
* @param string $url App URL
348361
* @throws DriverException If the app fails to load properly
349362
*/
350-
protected function prepare_browser() {
351-
// Restart the browser and set its size.
352-
$this->getSession()->restart();
353-
$this->resize_window('360x720', true);
363+
protected function prepare_browser(bool $restart = true) {
364+
if ($restart) {
365+
// Restart the browser and set its size.
366+
$this->getSession()->restart();
367+
$this->resize_window('360x720', true);
368+
369+
if (empty($this->ionicurl)) {
370+
$this->ionicurl = $this->start_or_reuse_ionic();
371+
}
354372

355-
if (empty($this->ionicurl)) {
356-
$this->ionicurl = $this->start_or_reuse_ionic();
357-
}
373+
// Check whether the app is running a legacy version.
374+
$json = @file_get_contents("{$this->ionicurl}/assets/env.json") ?: @file_get_contents("{$this->ionicurl}/config.json");
375+
$data = json_decode($json);
376+
$appversion = $data->build->version ?? str_replace('-dev', '', $data->versionname);
358377

359-
// Check whether the app is running a legacy version.
360-
$json = @file_get_contents("{$this->ionicurl}/assets/env.json") ?: @file_get_contents("{$this->ionicurl}/config.json");
361-
$data = json_decode($json);
362-
$appversion = $data->build->version ?? str_replace('-dev', '', $data->versionname);
378+
$this->islegacy = version_compare($appversion, '3.9.5', '<');
363379

364-
$this->islegacy = version_compare($appversion, '3.9.5', '<');
380+
// Visit the Ionic URL.
381+
$this->getSession()->visit($this->ionicurl);
382+
}
365383

366-
// Visit the Ionic URL and wait for it to load.
367-
$this->getSession()->visit($this->ionicurl);
384+
// Wait the application to load.
368385
$this->spin(function($context) {
369386
$title = $context->getSession()->getPage()->find('xpath', '//title');
370387

@@ -387,34 +404,36 @@ protected function prepare_browser() {
387404
$this->execute_script("window.BehatMoodleAppLegacy = $islegacyboolean;");
388405
$this->execute_script(file_get_contents(__DIR__ . '/app_behat_runtime.js'));
389406

390-
// Assert initial page.
391-
$this->spin(function($context) {
392-
$page = $context->getSession()->getPage();
393-
$element = $page->find('xpath', '//page-core-login-site//input[@name="url"]');
407+
if ($restart) {
408+
// Assert initial page.
409+
$this->spin(function($context) {
410+
$page = $context->getSession()->getPage();
411+
$element = $page->find('xpath', '//page-core-login-site//input[@name="url"]');
394412

395-
if ($element) {
396-
// Wait for the onboarding modal to open, if any.
397-
$this->wait_for_pending_js();
413+
if ($element) {
414+
// Wait for the onboarding modal to open, if any.
415+
$this->wait_for_pending_js();
398416

399-
$element = $this->islegacy
400-
? $page->find('xpath', '//page-core-login-site-onboarding')
401-
: $page->find('xpath', '//core-login-site-onboarding');
417+
$element = $this->islegacy
418+
? $page->find('xpath', '//page-core-login-site-onboarding')
419+
: $page->find('xpath', '//core-login-site-onboarding');
402420

403-
if ($element) {
404-
$this->i_press_in_the_app($this->parse_element_locator('"Skip"'));
405-
}
421+
if ($element) {
422+
$this->i_press_in_the_app($this->parse_element_locator('"Skip"'));
423+
}
406424

407-
// Login screen found.
408-
return true;
409-
}
425+
// Login screen found.
426+
return true;
427+
}
410428

411-
if ($page->find('xpath', '//page-core-mainmenu')) {
412-
// Main menu found.
413-
return true;
414-
}
429+
if ($page->find('xpath', '//page-core-mainmenu')) {
430+
// Main menu found.
431+
return true;
432+
}
415433

416-
throw new DriverException('Moodle app not launched properly');
417-
}, false, 60);
434+
throw new DriverException('Moodle app not launched properly');
435+
}, false, 60);
436+
}
418437

419438
// Continue only after JS finishes.
420439
$this->wait_for_pending_js();

0 commit comments

Comments
 (0)