Capybara::Element Not Found for <select> pop

2019-08-30 08:21发布

I get the following error when I run bundle exec rspec for my feature test for a form:

 Failure/Error: select('Boston Commons - Vacation English', :from => 'school_application_program')
 Capybara::ElementNotFound:
   Unable to find option "Boston Commons - Vacation English"

Here is the snippet of my scenario from my feature spec:

select('Boston Commons', :from => 'school_application_fls_center')
select('Boston Commons - Vacation English', :from => 'school_application_program')

The second line fails.

In my development environment I visit the same route that the scenario visits, perform the previous selection, and then confirm that school_application_program is the id of the <select> tag and that the option name is the exact same as the one I am checking.

My only guess, is that for some reason, my javascript onchange function is not properly populating the <select> tag, but that doesn't make a whole lot of sense given that the feature spec should mirror the development environment exactly.

The onchange event is being bound to the <select> tag immediately preceding school_application_program and resets the options for school_application_program

Here is the javascript that populated my school_application_program <select>

 $("#school_application_fls_center").change(function(){
var center_id = document.getElementById("school_application_fls_center").value;
var formdata = {center: center_id}; 
$.ajax({
    url: "/application/get_programs_for_center",
    type: "POST",
    datatype: 'json',
    data: formdata,
    success: function(response){
      var options = $("#school_application_program"); 
      removeOptions(document.getElementById("school_application_program"));
      $.each(response.programs, function(i,item) {
        options.append($("<option />").val(response.programs[i].id).text(response.programs[i].name)); 
      });
    }
});
});

This is a shot from my development environment, showing the form, after I have done the behavior that the spec does manually, at the point where the test fails.

enter image description here

1条回答
干净又极端
2楼-- · 2019-08-30 08:59

All that was required was to enable Javascript via adding the :js => true flag when the scenario was defined, like so:

scenario 'happy path that uses AJAX', :js=>true do
   #etc.
end
查看更多
登录 后发表回答