Making ‘Wikipedia’ example for Behat to work — autocomplete ugly workaround

Kirill Strelkov
2 min readOct 12, 2015

--

In documentation you can see there is a scenario with autocomplete
http://docs.behat.org/en/v2.5/cookbook/behat_and_mink.html

But this scenario won’t work because of this line:

When I fill in “search” with “Behavior Driv”

The line only sets value of the input to “Behavior Driv” but autocomplete feature won’t be triggered.
To fix this we introduce new keyword:

When I type “Behavior Driv” into search box

Adding implementation to FeatureContext:

/**
* @Then I type :text into search box
*/
public function iTypeTextIntoSearchBox($text)
{
$element = $this->getSession()->getPage()->findById(‘searchInput’);
$script = “$(‘#searchInput’).keypress();”;
$element->setValue($text);
$this->getSession()->evaluateScript($script);
}

Here after setting value to our text we execute jQuery’s ‘keypress’ event as a result suggestion box appears.

Here is full content of the files:

test@test-VirtualBox:~/my-behat-tests$ cat features/bootstrap/FeatureContext.php
<?php

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

use Behat\MinkExtension\Context\MinkContext;


/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}

/**
* @When I wait for the suggestion box to appear
*/
public function iWaitForTheSuggestionBoxToAppear()
{
$this->getSession()->wait(5000, “$(‘.suggestions-results’).children().length > 0”);
PHPUnit_Framework_Assert::assertTrue($this->getSession()->getPage()->has(‘css’, ‘.suggestions-results’), ‘ERROR: Suggestions are not visible’);
}

/**
* @Then I type :text into search box
*/
public function iTypeTextIntoSearchBox($text)
{
$element = $this->getSession()->getPage()->findById(‘searchInput’);
$script = “$(‘#searchInput’).keypress();”;
$element->setValue($text);
$this->getSession()->evaluateScript($script);
}
}

test@test-VirtualBox:~/my-behat-tests$ cat features/CustomerRegistration.feature
Feature: Customer registration

Scenario: Searching for a page that does exist
Given I am on “/wiki/Main_Page”
When I fill in “search” with “Behavior Driven Development”
And I press “searchButton”
Then I should see “agile software development”

Scenario: Searching for a page that does NOT exist
Given I am on “/wiki/Main_Page”
When I fill in “search” with “Glory Driven Development”
And I press “searchButton”
Then I should see “Search results”

@mink:selenium2
Scenario: Searching for a page with autocompletion
Given I am on “/wiki/Main_Page”
When I type “behavior driv” into search box
And I wait for the suggestion box to appear
Then I should see “Behavior-driven development”

--

--

No responses yet