ai didi

jquery - 检查 Capybara::Node::Element 是否已被 jQuery.remove() 删除

转载 作者:行者123 更新时间:2023-12-01 00:30:30 24 4
gpt4 key购买 nike

我正在尝试进行以下测试:

it 'remove item from cart' do
visit cart_path
button = page.find("a[href='/carts/#{item.id}/remove']")
card = find_ancestor_with_class(button, '.card')
button.click
# check if card has been removed from page
end

此测试应该有效,因为以下 JS 会从页面中删除卡片:

$.ajax({
url: link,
method: "GET",
success: function() {
$('#alert-modal').modal('show');
$(button).closest(".card").remove();
}
});

如何验证 .card html 是否已从页面中删除?

最佳答案

要检查页面上是否有某些内容不可见,您会执行以下操作

expect(page).to have_no_css(".card")

expect(page).not_to have_css(".card")

所以总的来说,它变成了

visit cart_path
click_link(href: "/cars/#{item.id}/remove")
expect(page).not_to have_css(".card")

只有删除所有 .card 元素时,这才有效。如果页面上仍有其他 .card 元素,则根据您希望在卡片中出现的文本或卡片元素(id 等)上的其他属性/属性进行检查,例如项目描述或其他显示内容,如

expect(page).not_to have_css(".card", text: item.description)

如果您经常处理 .card 元素,您还可以编写自定义 Capybara 选择器以使内容更易于阅读,本地化“卡片”等的 CSS 定义

Capybara.add_selector :card do
xpath do |content|
xp = XPath.css('.card')
xp = xp[XPath.string.n.is(content)] unless content.nil?
xp
end
end

然后应该允许

expect(page).to have_no_selector(:card)
expect(page).to have_no_selector(:card, item.description)

等等

关于jquery - 检查 Capybara::Node::Element 是否已被 jQuery.remove() 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44973789/

24 4 0
文章推荐: php - mysql全选,按orderid分组
文章推荐: PHP,将数据转换成Unix时间戳格式
文章推荐: php - 如何在 Php 中将日期从一种格式转换为另一种格式
文章推荐: javascript - Dropzone js错误 "Uncaught TypeError: $(...).dropzone is not a function"
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com