gpt4 book ai didi

ruby-on-rails - 如何使用 Capybara 和 Selenium 测试某个项目是否已从页面中删除?

转载 作者:行者123 更新时间:2023-12-04 05:34:24 25 4
gpt4 key购买 nike

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

$('#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

最佳答案

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

关于ruby-on-rails - 如何使用 Capybara 和 Selenium 测试某个项目是否已从页面中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6325147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com