Reputation: 313
Example 1:
click_on 'Add Cart'
assert_no_selector "#empty-cart" #note #empty-cart should be dynamically created since we added an item
accept_confirm do
click_on 'Empty Cart'
end
assert_selector #"empty-cart" #note: empty-cart should no longer be present since it was dynamically removed
but somehow I run the test and everything is fine.
Example 2
click_on 'Add Cart'
visit my_url
assert_no_selector "#empty-cart"
accept_confirm do
click_on 'Empty Cart'
end
visit my_url
assert_selector #"empty-cart"
By forcing a reload, the test script somehow is able to detect that the assertion is wrong.
Upvotes: 0
Views: 87
Reputation: 49890
Note: I'm assuming you meant the #s to be inside the quotes in the two examples you give - otherwise they're just creating comments
The concept you're missing is that all actions in the browser occur asynchronously. This means that when accept_confirm
returns, the confirm modal has been clicked but any actions that triggers are not guaranteed to have occurred yet. Therefore when assert_selector "#empty-cart"
is run it is highly likely that element is still on the page and the assertion will immediately pass.
For your second example you have a similar issue. Clicking on 'Add Cart' and then immediately calling visit
is likely to either abort the visit
call or have the visit
call processed in parallel and not actually see the results of 'Add Cart'. You need to add an assertion to confirm that the 'Add Cart' actions have completed before moving on. Something like
click_on 'Add Cart'
assert_no_selector '#empty-cart' # or maybe assert_text 'Item added to cart!' etc.
visit my_url
Upvotes: 1