黄瓜测试不适用于I18n的下拉列表

时间:2016-04-23 22:05:16

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

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

<%= 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

这会抛出此错误: 无法找到选择框“城市”(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>

测试通过成功。

所以,我的第一个线索是,可能是黄瓜无法检索翻译。我搜索了很多,尝试了一些建议,如使用:来自ENV等的语言环境。 但没有任何作用。

NB:搜索内容无论是否有翻译(I18n)都可以使用。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

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

示例:

# 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中阅读更多内容,它解释了您可以用黄瓜书写的不同类型的步骤。