使用 ruby​​ (watir) 自动完成下拉测试

发布于 2024-07-14 13:07:16 字数 1063 浏览 4 评论 0原文

填充文本字段

我正在尝试创建 watir 测试,通过编写 feks “lon” 并等待下拉列表被触发,然后单击列表中的第一个元素来

。 写“lon”应该会触发许多选项,例如“伦敦,英格兰,Storbritannia”,伦敦,肯塔基州,美国等。 watir 有可能做到这一点吗? 提前谢谢。

到目前为止,这就是我的代码的样子, 但它不起作用,我想知道我在哪里错过了一些东西。

def test_scriptflight_autocomplete @site.navigate_to(:旅行,:航班) from_field = @site.ie.text_field(:id, "locOriginName") to_field = @site.ie.text_field(:id, 'locDestinationName') from_field.set('oslo')

# 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

结束

i'm trying to create watir test that fills inn a textfield by writing feks

"lon" and waiting till the dropdown is triggered and then clicking on the first element in the list.

Writing "lon" should trigger many options like "London, England, Storbritannia", London, Kentucky, USA and etc.
Is it somehow possible to this with watir??
thnx in advance.

This is what my code looks like until now,
it odes not work though and i'm wondering where i have missed something.

def test_scriptflight_autocomplete
@site.navigate_to(:travel, :flight)
from_field = @site.ie.text_field(:id, "locOriginName")
to_field = @site.ie.text_field(:id, 'locDestinationName')
from_field.set('oslo')

# 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

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2024-07-21 13:07:16

我和一位同事(Magnar)在工作中发现了这个博客,它帮助我们找到了我正在寻找的答案。

http:// /blog.saush.com/2008/05/using-rspec-and-watir-for-function-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 和键。 该方法调用Javascript创建一个事件对象(顺便说一句,这只适用于IE)并将键码设置为“13”,即回车键(回车键)。 它调用 Javascript 来获取 HTML 元素(使用元素 id)并在其上触发“onkeydown”事件,同时传递刚刚创建的事件对象。

Me and a colleague(Magnar) at work found this blog that helped us to find the answer i was looking for.

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

From the Blog:

We just added a new method ‘fire_keydown_on’ for the IE class, which takes in the element id, and the key. This method calls Javascript to create an event object (this only works in IE, by the way) and sets the key code to be ‘13′, which is the carriage-return key (the enter key). The it calls Javascript to get the HTML element (using the element id) and fire the ‘onkeydown’ event on it, while passing on the event object it just created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文