diff --git a/tests/features/bootstrap/FeatureContext.php b/tests/features/bootstrap/FeatureContext.php index 3d4e78fdbf..cf62da1133 100644 --- a/tests/features/bootstrap/FeatureContext.php +++ b/tests/features/bootstrap/FeatureContext.php @@ -10,6 +10,7 @@ use Behat\Behat\Context\SnippetAcceptingContext; use Behat\Behat\Hook\Scope\AfterStepScope; use Behat\Gherkin\Node\TableNode; +use Behat\Mink\Driver\Selenium2Driver; use Behat\Mink\Exception\ExpectationException; use Behat\Mink\Exception\ResponseTextException; use Drupal\Component\Serialization\Yaml; @@ -22,6 +23,8 @@ use Drupal\joinup\Traits\UserTrait; use Drupal\joinup\Traits\UtilityTrait; use LoversOfBehat\TableExtension\Hook\Scope\AfterTableFetchScope; +use WebDriver\Exception; +use WebDriver\Key; /** * Defines generic step definitions. @@ -1205,4 +1208,46 @@ public static function stripScreenReaderElements(AfterTableFetchScope $scope) { $scope->setHtml($html_manipulator->removeElements('.visually-hidden')->html()); } + /** + * Fills in the autocomplete field with the given text. + * + * This differs from MinkContext::fillField() in that this will no remove the + * focus on the field after entering the text, so that the autocomplete + * results will not disappear. The final action taken on the field will be the + * "keyup" event for the last character. + * + * @param string $field + * The ID, name, label or value of the autocomplete field to fill in. + * @param string $value + * The text to type in the autocomplete field. + * + * @When I type :value in the :field autocomplete field + */ + public function fillAutoCompleteField(string $field, string $value): void + { + $this->assertJavaScriptEnabledBrowser(); + + $driver = $this->getSession()->getDriver(); + if (!$driver instanceof Selenium2Driver) { + throw new \RuntimeException("Only Selenium is currently supported for typing in autocomplete fields."); + } + + $xpath = $this->getSession()->getSelectorsHandler()->selectorToXpath('named', ['field', $field]); + try { + $element = $driver->getWebDriverSession()->element('xpath', $xpath); + } + catch (Exception $e) { + throw new \RuntimeException("Field with locator '$field' was not found in the page."); + } + + // Clear any existing data in the field before typing the new data. + $value = str_repeat(Key::BACKSPACE . Key::DELETE, strlen($element->attribute('value'))) . $value; + + // Fill in the field by directly using the postValue() method of the + // webdriver. This executes the keystrokes that make up the text but will + // not remove focus from the field so the autocomplete results remain + // visible and can be inspected. + $element->postValue(['value' => [$value]]); + } + } diff --git a/tests/features/joinup_discussion/discussion.invite.feature b/tests/features/joinup_discussion/discussion.invite.feature index 125a941868..2a5fe41719 100644 --- a/tests/features/joinup_discussion/discussion.invite.feature +++ b/tests/features/joinup_discussion/discussion.invite.feature @@ -102,35 +102,35 @@ Feature: Invite members to subscribe to discussions Then I should see the error message "Please add at least one user." # Try to filter by first name. - When I fill in "E-mail" with "sha" + When I type "sha" in the "E-mail" autocomplete field Then I wait until the page contains the text "Shaquila Paternoster (paternoster)" And I should see the text "Shannon Hargrave (theacuteone)" But I should not see the text "Lynwood Crawford (Lynwood Crawford)" And I should not see the text "Glory Ruskin (Glory Ruskin)" ## Try to filter by last name. - When I fill in "E-mail" with "raw" + When I type "raw" in the "E-mail" autocomplete field Then I wait until the page contains the text "Lynwood Crawford (Lynwood Crawford)" But I should not see the text "Shaquila Paternoster (paternoster)" And I should not see the text "Shannon Hargrave (theacuteone)" And I should not see the text "Glory Ruskin (Glory Ruskin)" ## Try to filter by e-mail address. - When I fill in "E-mail" with "hr" + When I type "hr" in the "E-mail" autocomplete field Then I wait until the page contains the text "Glory Ruskin (Glory Ruskin)" And I should see the text "Shannon Hargrave (theacuteone)" But I should not see the text "Lynwood Crawford (Lynwood Crawford)" And I should not see the text "Shaquila Paternoster (paternoster)" ## Try fo filter by username. - When I fill in "E-mail" with "acute" + When I type "acute" in the "E-mail" autocomplete field Then I wait until the page contains the text "Shannon Hargrave (theacuteone)" But I should see the text "Glory Ruskin (Glory Ruskin)" And I should not see the text "Lynwood Crawford (Lynwood Crawford)" And I should not see the text "Shaquila Paternoster (paternoster)" ## Try to filter on a combination of first name and last name. - When I fill in "E-mail" with "or" + When I type "or" in the "E-mail" autocomplete field Then I wait until the page contains the text "Lynwood Crawford (Lynwood Crawford)" And I should see the text "Glory Ruskin (Glory Ruskin)" But I should not see the text "Shannon Hargrave (theacuteone)"