Skip to content

Commit 2643e81

Browse files
authored
Merge pull request #21 from NoelDeMartin/MOBILE-3320
MOBILE-3320 behat: Improve flaky tests
2 parents b8986f7 + 7f0ab93 commit 2643e81

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

mod/choice/tests/behat/app_basic_usage.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Feature: Test basic usage of choice activity in app
5858
And I should find "Option 3: 0" in the app
5959
And I should find "Remove my choice" in the app
6060

61-
When I select "Option 1" in the app
61+
When I unselect "Option 1" in the app
6262
And I select "Option 3" in the app
6363
And I press "Save my choice" in the app
6464
Then I should find "Option 1: 0" in the app
@@ -114,9 +114,9 @@ Feature: Test basic usage of choice activity in app
114114
And I log in as "student1"
115115
And I press "Course 1" near "Course overview" in the app
116116
And I press "Test single choice name" in the app
117-
And I press "Option 1" in the app
117+
And I select "Option 1" in the app
118118
And I switch offline mode to "true"
119-
And I press "Option 2" in the app
119+
And I select "Option 2" in the app
120120
And I press "Save my choice" in the app
121121
Then I should find "Are you sure" in the app
122122

@@ -154,7 +154,7 @@ Feature: Test basic usage of choice activity in app
154154
When I press "OK" in the app
155155
And I press the back button in the app
156156
And I press "Test single choice name" in the app
157-
And I press "Option 2" in the app
157+
And I select "Option 2" in the app
158158
And I press "Save my choice" in the app
159159
Then I should find "Are you sure" in the app
160160

@@ -183,7 +183,7 @@ Feature: Test basic usage of choice activity in app
183183
And I log in as "student1"
184184
And I press "Course 1" near "Course overview" in the app
185185
And I press "Choice name" in the app
186-
And I press "Option 2" in the app
186+
And I select "Option 2" in the app
187187
And I press "Save my choice" in the app
188188
And I press "OK" in the app
189189

tests/behat/behat_app.php

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,49 @@ public function i_press_in_the_app($text, $near='') {
551551
* with JavaScript, and clicks may not work until they are initialized properly which may cause flaky tests due
552552
* to race conditions.
553553
*
554-
* @Then /^I select "(?P<text_string>(?:[^"]|\\")*)"(?: near "(?P<near_string>(?:[^"]|\\")*)")? in the app$/
554+
* @Then /^I (?P<select_string>unselect|select) "(?P<text_string>(?:[^"]|\\")*)"(?: near "(?P<near_string>(?:[^"]|\\")*)")? in the app$/
555+
* @param string $selectedtext Select/unselect string
555556
* @param string $text Text identifying click target
556557
* @param string $near Text identifying a nearby unique piece of text
557558
* @throws DriverException If the press doesn't work
558559
*/
559-
public function i_select_in_the_app($text, $near='') {
560-
$this->getSession()->wait(100);
561-
$this->press($text, $near);
560+
public function i_select_in_the_app(string $selectedtext, string $text, string $near = '') {
561+
$selected = $selectedtext === 'select' ? 'YES' : 'NO';
562+
$text = addslashes_js($text);
563+
$near = addslashes_js($near);
564+
565+
$this->spin(function() use ($selectedtext, $selected, $text, $near) {
566+
// Don't do anything if the item is already in the expected state.
567+
$result = $this->evaluate_script("return window.behat.isSelected(\"$text\", \"$near\");");
568+
569+
if ($result === $selected) {
570+
return true;
571+
}
572+
573+
// Press item.
574+
$result = $this->evaluate_script("return window.behat.press(\"$text\", \"$near\");");
575+
576+
if ($result !== 'OK') {
577+
throw new DriverException('Error pressing item - ' . $result);
578+
}
579+
580+
// Check that it worked as expected.
581+
$result = $this->evaluate_script("return window.behat.isSelected(\"$text\", \"$near\");");
582+
583+
switch ($result) {
584+
case 'YES':
585+
case 'NO':
586+
if ($result !== $selected) {
587+
throw new ExpectationException("Item wasn't $selectedtext after pressing it", $this->getSession()->getDriver());
588+
}
589+
590+
return true;
591+
default:
592+
throw new DriverException('Error finding item - ' . $result);
593+
}
594+
});
595+
596+
$this->wait_for_pending_js();
562597
}
563598

564599
/**
@@ -584,17 +619,16 @@ protected function is_in_login_page(): bool {
584619
* @throws DriverException If the press doesn't work
585620
*/
586621
protected function press(string $text, string $near = '') {
587-
$this->spin(function($context, $args) use ($text, $near) {
588-
if ($near !== '') {
589-
$nearbit = ', "' . addslashes_js($near) . '"';
590-
} else {
591-
$nearbit = '';
592-
}
593-
$result = $this->evaluate_script('return window.behat.press("' .
594-
addslashes_js($text) . '"' . $nearbit .');');
622+
$text = addslashes_js($text);
623+
$near = addslashes_js($near);
624+
625+
$this->spin(function() use ($text, $near) {
626+
$result = $this->evaluate_script("return window.behat.press(\"$text\", \"$near\");");
627+
595628
if ($result !== 'OK') {
596629
throw new DriverException('Error pressing item - ' . $result);
597630
}
631+
598632
return true;
599633
});
600634
$this->wait_for_pending_js();

0 commit comments

Comments
 (0)