Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
ISAICP-4633: Use a dedicated step for typing in an autocomplete field.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrenssen committed Jul 22, 2018
1 parent c4106e8 commit a24eeca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
41 changes: 41 additions & 0 deletions tests/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@
use Drupal\joinup\Traits\UserTrait;
use Drupal\joinup\Traits\UtilityTrait;
use LoversOfBehat\TableExtension\Hook\Scope\AfterTableFetchScope;
use WebDriver\Exception;

/**
* Defines generic step definitions.
Expand Down Expand Up @@ -1205,4 +1207,43 @@ 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.");
}

// 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]]);
}

}
10 changes: 5 additions & 5 deletions tests/features/joinup_discussion/discussion.invite.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit a24eeca

Please sign in to comment.