【问题标题】:autocomplete dropdown test with ruby (watir)使用 ruby​​ (watir) 进行自动完成下拉测试
【发布时间】:2010-10-10 18:22:40
【问题描述】:

我正在尝试通过编写 feks 创建填充 inn 文本字段的 watir 测试

“lon”并等待下拉菜单被触发,然后单击列表中的第一个元素。

写“lon”应该触发许多选项,例如“London, England, Storbritannia”, London, Kentucky, USA 等。 用watir有可能做到这一点吗? 提前谢谢。

这是我的代码到现在的样子, 它虽然不起作用,但我想知道我在哪里错过了一些东西。

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

结束

【问题讨论】:

    标签: ruby watir


    【解决方案1】:

    我和一个同事(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 类添加了一个新方法“fire_keydown_on”,它接收元素 id 和键。该方法调用 Javascript 来创建一个事件对象(顺便说一下,这只适用于 IE)并将键码设置为“13”,即回车键(回车键)。它调用 Javascript 来获取 HTML 元素(使用元素 id)并在其上触发“onkeydown”事件,同时传递它刚刚创建的事件对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      相关资源
      最近更新 更多