Capybara无法在按下按钮时更改的模态上找到链接

时间:2016-04-20 12:52:27

标签: ruby-on-rails ruby rspec capybara capybara-webkit

我有一个使用rspec 3.4.0 capybara 2.6.2capybara-webkit 1.8.0的rails项目。

我正在为我的网站上的流程编写功能测试,如下所示:

scenario "Buyer creates a seller profile", :js => true do
   click_link("SELL ON MYSITE",match: :first)
   expect(page).to have_text("Reach thousands of customers in your area")
   click_link("Create an Activity",match: :first)
   expect(current_path).to eql (new_seller_profile_path)

    fill_in "seller_profile[business_name]", :with => "Test company"
    fill_in "seller_profile[business_email]", :with => "test@email.com"
    fill_in "seller_profile[business_phone_number]", :with => "07771330510"
    fill_in "seller_profile[business_description]", :with => "This is a test company"

    find('label[for="social"]').click


    find("#facebook-placeholder").click

    fill_in "seller_profile[business_facebook_url]", :with => "https://www.facebook.com/test"

    click_button("CREATE AN ACTIVITY")

------这个按钮在页面改变后打开一个模态--------

    fill_in "seller_profile[requested_postcode]", :with => "EH21 8PB"

    click_button("Submit")

    save_and_open_screenshot

------这就是出错的地方-------

    click_link("Continue")

    expect(page).to have_text("Choose the type of activity that you want to create")

 end

click_link继续失败,错误:

Failure/Error: click_link("Continue")

     Capybara::ElementNotFound:
       Unable to find link "Continue"

链接确实存在 - 当您单击提交按钮时,会执行一些javascript来更改模式的内容以显示一些新文本和一个新按钮。但是由于某些原因,click_link不会等待或查看模态,它会立即失败。

添加了save_and_open_screenshot调用,我们可以看到这种情况,因为模态javascript尚未执行,因为我们仍然可以看到提交按钮:

enter image description here

有趣的是,鼠标似乎不在“提交”按钮上,因为它应该只是点击它?

enter image description here

如何让click_link等到“继续”按钮出现?!

这是在按下“提交”时执行的javascript。添加到改变它的模态:

$('#gate .modal-body .intro').text('Congratulations, we are available in your location, please continue to create your activity.');

$('#gate .modal-footer').append('<a class="btn btn-lg btn-primary" href="/events/new/">Continue</a>');

1 个答案:

答案 0 :(得分:0)

似乎增加了几个睡眠已经解决了。我不知道这是否是“正确”的解决方案。如果有人有更好的方式或更好的方式我会爱你帮助我找到它:

fill_in "seller_profile[requested_postcode]", :with => "EH21 8PB"

    sleep 2

    click_button("Submit")

    sleep 2

    click_link("Continue")