【问题标题】:Capybara select option, from: dropdownCapybara 选择选项,来自:下拉菜单
【发布时间】:2015-04-07 17:31:59
【问题描述】:

我一直在尝试针对一个简单的页面创建一个 capybara 测试用例,该页面包括一个由 country_select gem 填充的选择框。在我的测试用例中,我尝试从选择框中选择国家和生日月份,如下所示:

feature "registering for a new account" do

  Capybara.javascript_driver = :poltergeist

  scenario "a new user registers with a complete profile", :js => true do
    page.visit '/users/sign_up'
    select "United States", from: 'user[country]'
    select 'January', from: 'birthMonth'

    click 'sign up'

    expect(response.code).to eq '200'
  end

end

Capybara 或 Poltergeist 除了我默认选择的选项外,看不到任何选项。有一次我使用断言来查看我能找到哪些选项

assert_selector(:css, 'option', minimum: 20) # countries + months should be way more than 20

我得到了以下回复:

Failure/Error: assert_selector(:css, 'option', minimum: 20)
     Capybara::ExpectationNotMet:
       expected to find css "option" at least 20 times, found 2 matches: "country", "month"

我不知道我做错了什么。

【问题讨论】:

    标签: rspec capybara rspec-rails poltergeist


    【解决方案1】:

    试试这个

    find(:xpath, '//option[contains(text(), "United States")]').select_option
    find(:xpath, '//option[contains(text(), "January")]').select_option
    

    这应该可行,因为没有不明确的下拉值。

    【讨论】:

      【解决方案2】:

      我的问题不是 Capybara、RSpec 或 Poltergeist 的错。在控制器类中,我从数据库中动态加载一年中的月份。该表是在我的开发数据库中播种的月份,但不是我的测试数据库......对不起

      【讨论】:

        猜你喜欢
        • 2013-12-06
        • 1970-01-01
        • 2016-01-10
        • 2014-11-28
        • 2023-03-31
        • 2018-04-01
        • 1970-01-01
        • 2018-02-17
        相关资源
        最近更新 更多