【问题标题】:How can I test that an item is removed from the page, with Capybara and Selenium?如何使用 Capybara 和 Selenium 测试从页面中删除的项目?
【发布时间】:2011-06-12 22:33:28
【问题描述】:

我正在尝试使用 Capybara (head) 和 Selenium 在 Rails 3.0.8 中编写集成测试。我想测试当用户单击我网页上某个项目的删除按钮时,该项目实际上是否被删除。所以在网页上,我使用 JQuery sn-p 隐藏项目

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

我使用follow 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

【问题讨论】:

    标签: ruby-on-rails selenium rspec capybara


    【解决方案1】:

    我最终将此行添加到我的 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
    

    连同这个函数应该_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
    

    【讨论】:

      【解决方案2】:

      Capybara 2.1 支持has_text? 方法,该方法采用类型参数:visible,它将检查页面中的所有可见文本。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多