LoginSignup
2

More than 5 years have passed since last update.

Behat + Mink + Selenium2 で JavaScript動かす時に suggest的なのが取れない!という時の対処法

Posted at

少しハマったのでメモ。

Behat + Mink + Selenium2 で JavaScript動かす時に suggest的なのが取れない!という時の対処法

Behat + Mink + Selenium2 な環境の時、下記の Mink チュートリアル後半でハマりました。

Wiki に検索をした時、サジェストに規定の文字が出てくるのを期待するチュートリアル。

結論

できるだけ、I fill in "hoge" with "fuga" は使わずに、Click や KeyPress を使うようにするべき。

本当は、規定の input を Click してから、KeyPress で必要な文字を入力するべきだと思うのですが、とりあえず、以下のようにするとサジェストは出てくる。

/behat.yml
# behat.yml
default:
  extensions:
    Behat\MinkExtension\Extension:
      base_url: http://en.wikipedia.org
      goutte: ~
      selenium2: ~

/features/search.feature
  @javascript
  Scenario: Searching for a page with auto completion
    Given I am on "/wiki/Main_Page"
    When I fill in "search" with "Behavior Driv"
    And I press " " key into "searchInput"
    And I wait for the suggestion box to appear
    Then I should see "Behavior-driven development"
/features/FeatureContext.php
<?php
class FeatureContext extends MinkContext // MinkContext // BehatContext
{
    /**
     * @Given /^I press "([^"]*)" key into "([^"]*)"$/
     */
    public function iPressKeyInto($key, $id)
    {
        $this->getSession()->getPage()->findById($id)->keyPress($key);
        // throw new PendingException();
    }

    /**
     * @Given /^I wait for the suggestion box to appear$/
     */
    public function iWaitForTheSuggestionBoxToAppear()
    {
        $this->getSession()->wait(5000, "$('.suggestions-results').children().length > 0");
        // throw new PendingException();
    }
}

チュートリアルと違う点

Featureに以下を追加した。

    And I press " " key into "searchInput"

これによって、FeatureContext にもメソッドの追加が必要となった。のが、以下。

    /**
     * @Given /^I press "([^"]*)" key into "([^"]*)"$/
     */
    public function iPressKeyInto($key, $id)
    {
        $this->getSession()->getPage()->findById($id)->keyPress($key);
        // throw new PendingException();
    }

$id の要素に対して、keyPress で " "スペースを入力してみた。


本当は「When I fill in "search" with "Behavior Driv"」のところからして書きなおしたほうが良いのかも。

あと、自戒として。

https://github.com/Behat/MinkSelenium2Driver に目を通しておいて損はないかも。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2