1

This may seem unusually basic but how do I confirm the presence of a pop up confirmation?

<a data-confirm="delete this video?" rel="nofollow" data-method="delete" href="/videos/21">Delete</a>

<a is the "tag"/"element" and data-confirm is an attribute. I want to test for the existence of the "data-confirm" attribute within the <a> element/tag

I have tried

expect(page).to have_css("a.data-confirm.delete this video?")

from

capybara assert attributes of an element but no joy.

Edit:

I've tried the expectation from Arup's comment below

expect(page).to have_content "Content"
click_link "Delete"
expect(page).to have_css('a[data-confirm="delete this video?"]')

But it raises the following (same) error

Failures:

1) Visiting the video index page should search and save movies
 Failure/Error: expect(page).to have_css('a[data-confirm="delete this video?"]')
   expected #has_css?("a[data-confirm=\"delete this video?\"]") to return true, got false

but the page source shows it there and it is clearly working for the user

Any assistance would be very appreciated

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • Don't edit the question with the answer like that. Of you did, add it as an *update* without removing the actual post. Now it seems to everyone I wrote an answer which is what you tried on the first place and it didn't work. That said, the expectation syntax is correct, but it is failing due to other reason which can be answered by looking at your actual environment. – Arup Rakshit Jan 20 '18 at 05:03
  • Oh, yeah okay. I've made a bit of a mess now haven't I? Feel free to edit the original post. with a reputation like 87k I guess you have the privileges. – Just Matt for now Jan 20 '18 at 05:10
  • @Arup What element(s) of the environment would I need to be looking for? – Just Matt for now Jan 20 '18 at 05:40

2 Answers2

4

You can write this expectation as:

expect(page).to have_css('a[data-confirm="delete this video?"]')
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • Thanks @Arup Rakshit I've updated my question to reflect your comment. But I still can't make it work? – Just Matt for now Jan 20 '18 at 05:00
  • I don't understand why it's failing? it is only this one expectation that I can not run. And your solution is the only reference to it I can find anywhere on the WWW. How did you know the syntax? – Just Matt for now Jan 20 '18 at 05:14
  • Okay. Placing Arups line prior to the delete link being clicked works!?! I would have expected confirmation of the 'pop-up' notification to occur after it was raised? Oh well... 4 hours of frustration later I at least know how to test for element attributes with Rspec and Capybara. Thanks Arup – Just Matt for now Jan 20 '18 at 07:04
  • @JustMattfornow It is a normal, valid CSS rule I wrote with Capybara. :) – Arup Rakshit Jan 20 '18 at 11:27
1

The answer by Arup is correct for the title of the question (and as he stated in the comments it's just valid CSS - https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors), however it's not actually testing the more detailed part of the question "how do I confirm the presence of a pop up confirmation". All it is doing is confirming the correct data attribute is on the link element to trigger the rails provided JS that should show a confirm.

If you wanted to actually test the confirm box is shown you would need to swap to using a JS capable driver - https://github.com/teamcapybara/capybara/tree/2.17_stable#drivers - and then use something like the following in your test

expect(page).to have_content "Content"
accept_confirm "delete this video?" do
  click_link "Delete" # The action that will make the system modal confirm box appear
end

See - http://www.rubydoc.info/gems/capybara/Capybara/Session#accept_confirm-instance_method

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Ahh... Yeah, absolutely right. The question doesn't reflect the actual problem. Damn! You solved the problem but Arup _technically_ answered the question. (which was still a problem, just not, as you have now illuminated, _the_ problem). Note the reputation number next to my avatar and beware "Newb at large!" Thanks a heap! testing popups is fairly fundamental. – Just Matt for now Jan 23 '18 at 10:42