【问题标题】:Cucumber testing is not working for dropdown list with I18n黄瓜测试不适用于 I18n 的下拉列表
【发布时间】:2016-04-23 22:05:15
【问题描述】:

这是我在视图文件中的代码:

<%= f.label t('city'), class: "form-control-label" %><i>(<%= t('select-a-city', :default => 'Select a city') %>)</i>
<%= f.select :city, options_for_select(['city1', 'city2', 'city3']), {}, class: "form-control" %>

这是黄瓜步骤定义:

Then(/^I select "([^"]*)" from the "([^"]*)" dropdown list$/) do |param1, city| 
  select(param1, :from => I18n.t(city))
end

这会引发此错误: 找不到选择框“City”(Capybara::ElementNotFound) ./features/support/step_definitions/home_page_steps.rb:27:in `/^我从“城市”下拉列表中选择“city1”$/'

但是如果我像这样修改视图文件:

<%= f.label :city, class: "form-control-label" %><i>(<%= t('select-a-city', :default => 'Select a city') %>)</i>

测试成功通过。

所以,我的第一个线索是,可能是 cucumber 无法检索翻译。我搜索了很多,尝试了一些建议,例如使用 ENV 中的 :locale 等。 但没有任何效果。

注意:搜索内容无论有无翻译 (I18n) 均可。

任何帮助将不胜感激。

【问题讨论】:

  • 尝试将binding.pry 或byebug 放入您的ERB 模板中。 &lt;%- binding.pry %&gt;,例如。然后重新加载页面,您可以在控制台(运行服务器的控制台)中使用该上下文中的变量。
  • Cucumber 是测试框架。它不与下拉菜单交互。您可能正在为此使用其他一些库。也许水豚或硒。
  • 是的,我正在使用带有 selenium 的水豚作为 Web 驱动程序。我通过包装 id 属性中的选择选项使测试通过。但这就像幸福的道路。我没有尝试 poltergeist 网络驱动程序。

标签: ruby-on-rails-4 cucumber integration-testing


【解决方案1】:

您只能使用元素选择器和选项值作为选定的下拉选项。将根据值选择该选项。

例子:

# When I select option "My Test Inc Account" from ".accounts-dropdown"
When(/^I select option "(.*?)" from element "(.*?)"$/) do |option, 
selector|
  all(selector).last.find(:option, option).select_option
end

您可以在this article 阅读更多内容,它解释了您可以在黄瓜中编写的不同类型的步骤。

【讨论】:

    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多