我正在使用 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 打开浏览器时,如果我打开另一个窗口,测试将所有步骤返回为绿色。如果不是:如果我继续观看打开的窗口并保持焦点,...单击和重定向,它们就不会完成其工作。
为什么?
最佳答案
经过一天的尝试,我得出的结论是,selenium 与 firefox(以及在我的情况下带有 WebDriver 的 phantomjs)无法从 jquery 自动完成捕获正常工作所需的所有事件,因为数据输入未完全模拟使用手册。
归根结底,最重要的是,只需通过 javascript $('element') 进行调用。 autocomplete('search') 我确定显示了 ui-autocomplete 项目
今天我已经阅读并尝试了所有相关内容:)
类似的事情:
关于javascript - 如果打开的浏览器具有焦点,则使用 Selenium2 和 Behat 监听单击事件将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22037281/