自动完成下拉测试
问题描述:
我想要创建的Watir测试通过写feks自动完成下拉测试
填充客栈一个文本框“LON”,并等到下拉被触发,然后单击在第一个元素上名单。
书写“lon”应该引发很多选项,如“伦敦,英格兰,Storbritannia”,伦敦,肯塔基州,美国等 这是不是可能与watir? thnx提前。
这是我的代码看起来像直到现在, 它ö不工作,但我想知道我错过了什么。
高清test_scriptflight_autocomplete @ site.navigate_to(:旅行:飞行) from_field = @ site.ie.text_field(:身份证, “locOriginName”) to_field = @ site.ie.text_field(:身份证,“locDestinationName “) from_field.set(”奥斯陆“)
# need to fire a key press event after setting the text since the js is handling
# trigger the autocomplete (requires a 'keydown')
from_field.fire_event('onkeydown')
# wait until the autocomplete gets populated with the AJAX call
@site.ie.wait_until{@site.ie.div(:id, 'onlinesearch').lis.length > 0}
puts @site.ie.div(:id, 'locOriginName ').lis.length
puts @site.ie.div(:id, 'locOriginName').li(:index, 5).text
# find the li you want to select in the autocomplete list and click it
@site.ie.div(:id, 'from-field').li(:text, 'Oslo, Oslo, Norge').click
末
答
我和工作同事(Magnar)发现了这个博客,帮助我们找到了我一直在寻找的答案。
http://blog.saush.com/2008/05/using-rspec-and-watir-for-functional-testing-in-web-applications/
class Watir::IE
def fire_keydown_on(element_id, key)
ie.Document.parentWindow.execScript("key = document.createEventObject(); key.keyCode = #{key}")
ie.Document.parentWindow.execScript("document.getElementById('#{element_id}').fireEvent('onkeydown', key)")
end
end
从博客:
我们只是添加了IE类,这需要在元素ID的新方法“fire_keydown_on”和关键。这个方法调用Javascript来创建一个事件对象(顺便说一下,它只能在IE中使用),并将关键代码设置为'13',这是回车键(回车键)。它调用Javascript来获取HTML元素(使用元素ID)并在其上激发“onkeydown”事件,同时传递刚刚创建的事件对象。