Rails 4,Selenium使用事件执行javascript
在这个rspec测试中,如何执行javascript函数addRows()?该函数未在测试中运行 我的rspec测试Rails 4,Selenium使用事件执行javascript,javascript,selenium-webdriver,rspec,Javascript,Selenium Webdriver,Rspec,在这个rspec测试中,如何执行javascript函数addRows()?该函数未在测试中运行 我的rspec测试 context 'pressing alt+m, ' do it 'appends hello to the dom', :js => true do visit welcome_path page.execute_script("return addRows(keypressdown = 17, keypressdown = 77);")
context 'pressing alt+m, ' do
it 'appends hello to the dom', :js => true do
visit welcome_path
page.execute_script("return addRows(keypressdown = 17, keypressdown = 77);")
expect(page).to have_content("hello")
end
end
keypressListener.js文件
function addRows(event) {
event = event || window.event;
if(event.keyCode == 17 && event.keyCode == 77) {
document.body.append("hello");
}
}
运行rails服务器并在此页面上手动按alt+m可运行addRows()函数,但测试不运行该函数。您可能不需要执行任何Javascript。水豚有一个
元素。发送密钥方法。所以类似的方法可能会奏效:
page.find('body').send_keys [:alt, 'm']
有关详细信息,请参阅。根据您的建议,由于“元素表示页面上的单个元素”,我使用variable=page.find'body”,然后调用variable.send_键[:alt,'m']使测试通过。非常感谢。杰出的我在我的答案中加入了你的经验。