Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capybara assert attributes of an element

I'm using RSpec2 and Capybara for acceptance testing.

I would like to assert that link is disabled or not in Capybara. How can I do this?

like image 908
kriysna Avatar asked Mar 01 '11 11:03

kriysna


2 Answers

Another simple solution is to access the HTML attribute you are looking for with []:

find('#my_element')['class']
# => "highlighted clearfix some_other_css_class"

find('a#my_element')['href']
# => "http://example.com

# or in general, find any attribute, even if it does not exist
find('a#my_element')['no_such_attribute']
# => ""

Note that Capybara will automatically try to wait for asynchronous requests to finish, but it may not work in some cases:

  • http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara

Here is one workaround if you are having trouble with assertions on elements that are updated asynchronously:

  • http://robots.thoughtbot.com/automatically-wait-for-ajax-with-capybara
like image 79
bowsersenior Avatar answered Nov 19 '22 08:11

bowsersenior


How are you disabling the link? Is it a class you're adding? An attribute?

# Check for a link that has a "disabled" class:
page.should have_css("a.my_link.disabled")
page.should have_xpath("//a[@class='disabled']")

# Check for a link that has a "disabled" attribute:
page.should have_css("a.my_link[disabled]")
page.should have_xpath("//a[@class='disabled' and @disabled='disabled']")

# Check that the element is visible
find("a.my_link").should be_visible
find(:xpath, "//a[@class='disabled']").should be_visible

The actual xpath selectors may be incorrect. I don't use xpath often!

like image 93
idlefingers Avatar answered Nov 19 '22 07:11

idlefingers