我正在向页面添加sweetalert,这样如果客户忘记上传文件,当他们点击继续按钮然后继续下一页之前,就会用sweetalert加热它们。
按钮html:
<a href="/checkout/address" class="checkout-now continue-to-checkout">Checkout Now ></a>
点击处理程序javascript:
$(HtmlIds.cart.checkoutBtn).click(function(e) {
if ($(HtmlIds.cart.missingUploadsTag).length !== 0) {
e.preventDefault();
var linkURL = $(this).attr("href");
SweetAlert({
title: "Missing Uploads",
text: "Your print job(s) are missing file uploads, which will cause a delay in the production of your order. " +
"To upload a file to a print job, please click on the 'Update Files' links in your shopping cart.",
type: 'warning',
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "I understand, proceed anyway"
}, function(isConfirm) {
if (isConfirm)
window.location.href = linkURL;
});
}
});
代码工作正常。但是,我遇到了通过rspec和capybara进行测试的问题。当我尝试进入下一页时,测试失败并且当我检查下一页是否即时通讯时,capybara / poltergeist错误。
rspec / capybara测试代码:
click_link "Checkout Now"
find(".sweet-alert").should have_content("Missing Uploads") #wait until sweet alert pops up
click_button "I understand, proceed anyway"
#page.execute_script('$(".confirm").click()')
page.should have_content("Checkout: Delivery")
错误消息:
2) checkout should be able to go through checkout via shipping
Failure/Error: page.should have_content("Checkout: Delivery")
expected #has_content?("Checkout: Delivery") to return true, got false
Capybara似乎在我使用click_button时注册了点击,因为它并没有错误地说按钮不存在,但它从未继续到下一页。注释掉的强制jquery代码也不起作用。如果我从我的代码中删除sweetalert(以及我的测试中的点击),那么无论测试通过什么,用户都会继续。
答案 0 :(得分:1)
如果使用了poltergeist,请使用.trigger(“click”)而不是click或click_button。在poltergeist中有一个错误,其中click和click_button不会给出一致的结果(似乎是元素正在进行动画时)。 Poltergeist issue on github