如何使用Capybara和Selenium测试是否从页面中删除了某个项目?

时间:2011-06-12 22:33:28

标签: ruby-on-rails selenium rspec capybara

我正在尝试使用Capybara(head)和Selenium在Rails 3.0.8中编写集成测试。我想测试一下,当用户点击我网页上某个项目的删除按钮时,该项目实际上会被删除。所以在网页上,我使用JQuery代码段隐藏了该项目

$('#interaction').fadeOut();

我使用了跟随rspec测试,并将Capybara.default_wait_time设置为5秒,但似乎没有什么能给我一个通过测试。我该如何正确测试?

describe "A User adding an Interaction" do
  describe "when looking at an Item detail page" do
    it "should be able to delete and existing interaction", :js => true do
      sign_in_and_view_item
      page.should have_content "ITEM DETAILS - #{@item.name}"
      fill_in "interaction-input", :with  => "My Interaction"
      click_button "item-interaction-submit"
      page.should have_content "My Interaction"
      click_link "Delete Interaction"
      wait_until do
        page.evaluate_script('$.active') == 0
      end
      #page.should_not have_content "My Interaction"
      page.should have_no_content "My Interaction"
    end
  end
end

2 个答案:

答案 0 :(得分:3)

我最后将此行添加到我的spec / spec_helper.rb

Capybara.ignore_hidden_elements = false

使用此规范

要求'spec_helper'

describe "A User adding an Interaction" do
  describe "when looking at an Item detail page" do
    it "should be able to delete and existing interaction", :js => true do
      sign_in_and_view_item
      page.should have_content "ITEM DETAILS - #{@item.name}"
      fill_in "interaction-input", :with  => "My Interaction"
      click_button "Save"
      page.should have_content "My Interaction"
      click_link "Delete Interaction"
      should_be_hidden "My Interaction"
    end
  end
end

以及此函数should_be_hidden

def should_be_hidden text, selector=''
  sleep Capybara.default_wait_time
  its_hidden = page.evaluate_script("$('#{selector}:contains(#{text})').is(':hidden');")
  its_not_in_dom = page.evaluate_script("$('#{selector}:contains(#{text})').length == 0;")
  (its_hidden || its_not_in_dom).should be_true
end

答案 1 :(得分:0)

Capybara 2.1支持采用类型参数has_text?的{​​{1}}方法,该方法将检查所有可见文本的页面。

http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_text%3F-instance_method