Merge pull request #21 from NoelDeMartin/MOBILE-3320

MOBILE-3320 behat: Improve flaky tests
main
Dani Palou 2021-06-01 09:05:34 +02:00 committed by GitHub
commit 10209fef33
2 changed files with 51 additions and 17 deletions

View File

@ -58,7 +58,7 @@ Feature: Test basic usage of choice activity in app
And I should find "Option 3: 0" in the app And I should find "Option 3: 0" in the app
And I should find "Remove my choice" in the app And I should find "Remove my choice" in the app
When I select "Option 1" in the app When I unselect "Option 1" in the app
And I select "Option 3" in the app And I select "Option 3" in the app
And I press "Save my choice" in the app And I press "Save my choice" in the app
Then I should find "Option 1: 0" in the app Then I should find "Option 1: 0" in the app
@ -114,9 +114,9 @@ Feature: Test basic usage of choice activity in app
And I log in as "student1" And I log in as "student1"
And I press "Course 1" near "Course overview" in the app And I press "Course 1" near "Course overview" in the app
And I press "Test single choice name" in the app And I press "Test single choice name" in the app
And I press "Option 1" in the app And I select "Option 1" in the app
And I switch offline mode to "true" And I switch offline mode to "true"
And I press "Option 2" in the app And I select "Option 2" in the app
And I press "Save my choice" in the app And I press "Save my choice" in the app
Then I should find "Are you sure" in the app Then I should find "Are you sure" in the app
@ -154,7 +154,7 @@ Feature: Test basic usage of choice activity in app
When I press "OK" in the app When I press "OK" in the app
And I press the back button in the app And I press the back button in the app
And I press "Test single choice name" in the app And I press "Test single choice name" in the app
And I press "Option 2" in the app And I select "Option 2" in the app
And I press "Save my choice" in the app And I press "Save my choice" in the app
Then I should find "Are you sure" in the app Then I should find "Are you sure" in the app
@ -183,7 +183,7 @@ Feature: Test basic usage of choice activity in app
And I log in as "student1" And I log in as "student1"
And I press "Course 1" near "Course overview" in the app And I press "Course 1" near "Course overview" in the app
And I press "Choice name" in the app And I press "Choice name" in the app
And I press "Option 2" in the app And I select "Option 2" in the app
And I press "Save my choice" in the app And I press "Save my choice" in the app
And I press "OK" in the app And I press "OK" in the app

View File

@ -551,14 +551,49 @@ class behat_app extends behat_base {
* with JavaScript, and clicks may not work until they are initialized properly which may cause flaky tests due * with JavaScript, and clicks may not work until they are initialized properly which may cause flaky tests due
* to race conditions. * to race conditions.
* *
* @Then /^I select "(?P<text_string>(?:[^"]|\\")*)"(?: near "(?P<near_string>(?:[^"]|\\")*)")? in the app$/ * @Then /^I (?P<select_string>unselect|select) "(?P<text_string>(?:[^"]|\\")*)"(?: near "(?P<near_string>(?:[^"]|\\")*)")? in the app$/
* @param string $selectedtext Select/unselect string
* @param string $text Text identifying click target * @param string $text Text identifying click target
* @param string $near Text identifying a nearby unique piece of text * @param string $near Text identifying a nearby unique piece of text
* @throws DriverException If the press doesn't work * @throws DriverException If the press doesn't work
*/ */
public function i_select_in_the_app($text, $near='') { public function i_select_in_the_app(string $selectedtext, string $text, string $near = '') {
$this->getSession()->wait(100); $selected = $selectedtext === 'select' ? 'YES' : 'NO';
$this->press($text, $near); $text = addslashes_js($text);
$near = addslashes_js($near);
$this->spin(function() use ($selectedtext, $selected, $text, $near) {
// Don't do anything if the item is already in the expected state.
$result = $this->evaluate_script("return window.behat.isSelected(\"$text\", \"$near\");");
if ($result === $selected) {
return true;
}
// Press item.
$result = $this->evaluate_script("return window.behat.press(\"$text\", \"$near\");");
if ($result !== 'OK') {
throw new DriverException('Error pressing item - ' . $result);
}
// Check that it worked as expected.
$result = $this->evaluate_script("return window.behat.isSelected(\"$text\", \"$near\");");
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();
} }
/** /**
@ -584,17 +619,16 @@ class behat_app extends behat_base {
* @throws DriverException If the press doesn't work * @throws DriverException If the press doesn't work
*/ */
protected function press(string $text, string $near = '') { protected function press(string $text, string $near = '') {
$this->spin(function($context, $args) use ($text, $near) { $text = addslashes_js($text);
if ($near !== '') { $near = addslashes_js($near);
$nearbit = ', "' . addslashes_js($near) . '"';
} else { $this->spin(function() use ($text, $near) {
$nearbit = ''; $result = $this->evaluate_script("return window.behat.press(\"$text\", \"$near\");");
}
$result = $this->evaluate_script('return window.behat.press("' .
addslashes_js($text) . '"' . $nearbit .');');
if ($result !== 'OK') { if ($result !== 'OK') {
throw new DriverException('Error pressing item - ' . $result); throw new DriverException('Error pressing item - ' . $result);
} }
return true; return true;
}); });
$this->wait_for_pending_js(); $this->wait_for_pending_js();