简体   繁体   English

Rails RSpec水豚选择不起作用

[英]rails rspec capybara select not working

In my rails app, I have a select on the navigation bar as follows: 在我的rails应用程序中,我在导航栏上有一个选择,如下所示:

<body>
  <div id="wrapper">

  <!-- Navigation -->
  <nav role="navigation" style="margin-bottom: 0">
    <div class="navbar-default sidebar hidden-sm hidden-xs" role="navigation">
      <div class="sidebar-nav">
        <ul class="nav" id="side-menu">
          <li>
            <h4 class="sidebar-title">SGPLAN</h4>
          </li>
          <li class="logo">
            <form action="#">
              <select name="" id="change_plan" class="form-control plan">
                <option value="1" id="plan_id" selected="">first </option>
                <option value="2" id="plan_id">other </option>
              </select>
            </form>
          </li>

and javascript in application.js to load the home page when the user selects a different option. 当用户选择其他选项时,通过application.js中的javascript加载首页。

$(document).ready(function() {
    //...
    $('#change_plan').on('change', function(){
        var str = ''
        str += $( this ).val() + " ";
        setCookie("plan", str,1);
        window.location.href = "/";
    })
});

I have written the following test for this feature using rspec, capybara and capybara-webkit: 我使用rspec,capybara和capybara-webkit为此功能编写了以下测试:

require 'rails_helper'

feature "Change plan", :js do
  background do
    login_as create(:admin_user), scope: :user
    Agency.current = Agency.find_by(initials: 'SECTI').id
    FactoryGirl.create(:other_plan)
    Plan.current = Plan.find_by(name: 'first').id
  end

  scenario "User changes the current plan" do
    visit "/milestones"
    save_and_open_page
    select('other', from: 'change_plan')
    # within '#change_plan' do
    #   find("option[value='2']").click
    # end
    # find('#change_plan').find('option', text: 'other').select_option
    expect(current_path).to eq("/")
  end
end

save_and_open_page results in the html snippet as shown above. save_and_open_page会产生html片段,如上所示。

The result of running the test is as follows: 运行测试的结果如下:

Failures:

  1) Change plan User changes the current plan
     Failure/Error: expect(current_path).to eq("/")

       expected: "/"
            got: "/milestones"

       (compared using ==)
     # ./spec/features/plans/change_plan_spec.rb:19:in `block (2 levels) in <top (required)>'

Finished in 1 minute 1.71 seconds (files took 2.04 seconds to load)
1 example, 1 failure

If I use find('#change_plan')... or find("option...") (as per the commented out lines) in the test instead of the select, the result is the same. 如果我在测试中使用find('#change_plan')...find("option...") (根据注释行)而不是select,则结果是相同的。

My versions are follows (from Gemfile.lock ): 我的版本如下(来自Gemfile.lock ):

capybara (2.7.1)
capybara-webkit (1.11.1)
database_cleaner (1.5.3)
factory_girl_rails (4.7.0)
rails (4.2.5)
rspec-core (3.5.4)
rspec-expectations (3.5.0)
rspec-mocks (3.5.0)
rspec-rails (3.5.2)

and ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux] ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]

What do I need to do get this test to work? 我需要做些什么才能使此测试起作用? Should I be using a different test platform? 我应该使用其他测试平台吗? We are relatively committed to rspec but less so to capybara. 我们相对致力于rspec,但对水豚则相对较少。

Update 更新

I finally got this working with the help of Thomas and employing multiple suggestions that he provided. 最终,我在Thomas的帮助下并使用了他提供的多个建议来完成这项工作。

  1. There was a javascript error with the capybara webkit driver. capybara webkit驱动程序出现JavaScript错误。
  2. I tried the selenium driver but got a 503 error at the visit /milestones step. 我尝试了硒驱动程序,但在访问/里程碑步骤中遇到了503错误。
  3. I then switched to the poltergeist driver and found that the wait behaviour was also an issue - so I had to use have_current_path. 然后,我切换到poltergeist驱动程序,发现等待行为也是一个问题-因此我不得不使用have_current_path。

You shouldn't be using the eq matcher with current_path since it has no waiting/retrying behavior. 您不应该将eq匹配器与current_path一起使用,因为它没有等待/重试的行为。 This means you're checking the path before the page has time to change. 这意味着您要在页面有时间更改之前检查路径。 Instead use the have_current_path matcher provided by Capybara 而是使用Capybara提供的have_current_path匹配器

expect(page).to have_current_path('/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM