Capybara ::用于AJAX的ElementNotFound填充<select>选项</select>

时间:2014-11-25 20:01:18

标签: jquery ajax rspec capybara

在此previous context中收到此错误后,我再一次在测试套件上遇到同样的错误。不同的是,现在我正在调用一个函数generate_steps_to_validate_school_pricing来执行以下操作。

def generate_steps_to_validate_school_pricing(school_pricing_hash, school,   homestay_single_room_per_week_cost, airport, airport_price)
  school_pricing_hash.each do |program_name, program |
    prog_name = school + " - " + program_name
    program.each do |price_point_number, price_per_week|
      visit "/application/new"
      fill_in "school_application_first_name", :with => "bob"
      fill_in "school_application_family_name", :with => "balaban"
      check("school_application_sevic") #200 dollars
      select(school, :from => 'school_application_fls_center')
      check('school_application_I_20')
      check('school_application_pay_application_fee_or_full')
      fill_in "school_application_start_date", :with => "05/11/2015"
      select(airport, :from => 'school_application_arrival_airport')
      select('Homestay Single Room', :from => 'school_application_housing_type')
      check("school_application_health_insurance")
      check("school_application_transfer_student")
      fill_in "school_application_comments", :with =>"Lorem"
      fill_in "school_application_gender", :with =>"trans"
      fill_in "school_application_address", :with=> "5/11/15"
      fill_in "school_application_city_state_province", :with =>"Dubai"
      fill_in "school_application_email", :with =>"bob@balaban.com"
      select(airport, :from => 'school_application_arrival_airport')

      fill_in "school_application_postal_code", :with =>"90226"
      fill_in "school_application_country", :with=> "Zimbabwe"
      fill_in "school_application_date_of_birth", :with =>"Dubai"
      fill_in "school_application_phone_number", :with => "2134932434"
      select(price_point_number, :from => "school_application_duration")
      check("school_application_read_everything")
      fill_in "school_application_country_of_birth", :with =>"Aim"
      fill_in "school_application_country_of_citizenship", :with =>"Durango"
      check("school_application_work_with_ad")

      fill_in "school_application_agency", :with =>"Aim"
      fill_in "school_application_fax_number", :with =>"Durango"
      save_and_open_page
      select(prog_name, :from => "school_application_program") # this is the line that fails
      total = price_point_number[0] *price_per_week
      total = total + price_point_number[0] * homestay_single_room_per_week_cost
      total = total + 500 #SEVIS fee (200) + Housing Application and Application Fees (150 each)
      total = total + airport_price
      total = total + 35 * price_per_week[0] #current Insurance for 2015 is 35/week

      click_button "Continue"
      expect(page).to have_text(total.to_s  + " USD")
    end
  end
end

虽然函数中定义的步骤序列没有应用:js=>true,但调用它的scenario会执行。我确信AJAX调用通常用作对应于通过AJAX传递填充的两个<select>的测试。

当我save_and_open_page时,应该有prog_name的下拉列表为空,这似乎告诉我由于某种原因特定的AJAX调用正在破坏。我不确定如何测试该单独的控制器方法以查看正在发生的事情,并且同样不确定这样做的重点是当我执行与我在localhost上测试的Happy Path完全相同的过程时所有AJAX调用都可以正常工作,包括填充当前未填充的<select>

有关如何进一步系统地调试这一点的任何说明将不胜感激。谢谢!

N.B。问题很可能是测试不会等待服务器响应数据,直到它返回回调。有没有办法让它等到收到回复?

更新:尝试将Capyabara.default_wait_time扩展到20秒仍然失败,我觉得它排除了异步问题?

1 个答案:

答案 0 :(得分:1)

在认为这是一个异步问题之后,我首先更改了Capybara请求的等待时间,然后最终实现了这个更加清洁和可靠的解决方案,以确保通过遵循本教程完成所有异步请求:http://robots.thoughtbot.com/automatically-wait-for-ajax-with-capybara

所以在实现了helper函数之后,我知道肯定知道它不是异步问题,这意味着AJAX调用本身一定存在一些问题。因为它在我的本地服务器上工作,所以我不明白在使用RSpec测试完全相同的功能时它会如何失败。

当我重新访问我的JS时,我看到我对服务器进行了以下调用,对于失败的方法:

    $.ajax({
    url: "http://0.0.0.0:3000/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));
      });
    }
});

通过提供本地服务器的完整路径,当我手动尝试相同的行为时,它成功地在本地发出请求但由于显而易见的原因在我运行bundle exec rspec(使用不同的服务器)时失败,请求失败。我写的其他类似的AJAX请求都称为相对路径,因此是成功的。