【问题标题】:Testing rails' autocomplete with cucumber用黄瓜测试 rails 的自动完成功能
【发布时间】:2010-08-07 21:28:40
【问题描述】:

我需要用黄瓜测试一个自动完成字段。我怎样才能做到这一点?我试过了

  Scenario: Using Autocomplete   
  Given I am on the home page
  And there are the following users:
    |id |name   |
    |1  |foo    |
  When I fill in "name" with "f" 
  Then I should see "foo" 

但是它失败了,因为预期以下元素的内容包含“foo”。 有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails autocomplete cucumber


    【解决方案1】:

    您需要使用支持 JS 的驱动程序进行测试。如果您使用的是 webrat o capybara,您可以尝试使用 selenium。

    Then I should see "foo" 
    

    这在使用默认 web 步骤时不起作用,因为 DOM 在查找文本时尚未更新。有两种选择:

    选项 1:等待

    And I wait for 1 second
    Then I should see "foo"
    

    调用sleep(n)需要实现这一步

    选项 2:自定义定位

    Capybara 的 locate 方法将查看目标是否存在于 DOM 中。如果不存在,它将等待几秒钟,然后重试,如果仍然不存在则抛出错误。

    Then I should see the following autocomplete options:
    | foo |
    

    这对我来说是这些步骤的实现方式:

    Then /^I should see the following autocomplete options:$/ do |table|
      table.raw.each do |row|
        locate(:xpath, "//a[text()='#{row[0]}']")
      end
    end
    

    如果您需要有关此主题的更多详细信息,我已经写了一篇包含示例和一些 sn-ps 的博文,还涉及单击显示的自动完成选项:

    http://www.aentos.com/blog/testing-ajax-autocomplete-fields-cucumber-and-capybara

    【讨论】:

      【解决方案2】:

      自动完成使用 Javascript 设置文本字段的值(在您的情况下为“名称”)。由于 Cucumber 使用 Webrat 检查从服务器返回的响应,并且 Webrat 不支持 Javascript,因此您的方案失败。

      Cucumber 可以使用 Selenium 自动执行 Javascript 测试并验证是否插入了预期值。更多信息 here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多