如果打开的浏览器具有焦点,则使用Selenium2和Behat监听点击事件不起作用

时间:2014-02-26 09:38:08

标签: javascript jquery selenium

我正在使用Behat和Selenium2测试我的UI。这是我的观点:

Scenario: Invite a Facebook friend
  Given database is empty
  Then I go to "/somewhere"
  Given I follow "Invite a friend"
  And I fill in "friends" with "Acme"
  Then I wait for the suggestion box to appear
  Then I select first suggestion
  Then show last response

我有自动完成(jquery-ui)框。所有句子都按照我的预期工作,并且是绿色的,但用户不是重定向

/**
 * @Then /^I wait for the suggestion box to appear$/
 */
public function iWaitForTheSuggestionBoxToAppear()
{
    $this->getSession()->wait(
        1000,
        "$('.ui-autocomplete').children().length > 0"
    );
}

但我已经添加了第二句话

/**
 * @Then /^I select first suggestion$/
 */
public function iSelectFirstSuggestion()
{
    $this->getSession()->executeScript(
        "$('.ui-autocomplete').children().children().click()"
    );
}

Selenium控制台告诉我:

10:06:01.426 INFO - ... $('.ui-autocomplete').children().length > 0;, ...
10:06:01.439 INFO - ...
10:06:01.545 INFO - ... $('.ui-autocomplete').children().length > 0;...
10:06:01.559 INFO - ...
10:06:01.669 INFO - ... $('.ui-autocomplete').children().children().click(); ...

点击后,用户应重定向到/ invite / url。

    $("#friends").autocomplete({
        minLength: 0,
        source: projects,
        select: function (event, ui) {
            document.location.href = '/invite/' + ui.item.uid;
            return false;
        }
    })

相反,此脚本并不总是有效。当selenium打开浏览器时,如果我打开另一个窗口,test会将所有步骤恢复为绿色。如果不是:如果我继续观看打开的窗口并保持焦点,...单击并重定向,它们就不起作用。

为什么?

1 个答案:

答案 0 :(得分:0)

经过一天的尝试后,我得出的结论是,使用firefox的selenium(以及在我的情况下使用WebDriver的phantomjs)不能从jquery自动完成中捕获所有必需的事件以正常工作,因为数据输入不能完全模拟使用手册。

在一天结束时,底线就是通过javascript $(' element')进行调用。自动填充功能('搜索')我确信ui-autocomplete项目已显示

今天我已经阅读并尝试了一切:)

类似的事情: