capybara assert attributes of an element
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 existfind('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:
Here is one workaround if you are having trouble with assertions on elements that are updated asynchronously:
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 visiblefind("a.my_link").should be_visiblefind(:xpath, "//a[@class='disabled']").should be_visible
The actual xpath selectors may be incorrect. I don't use xpath often!
It was a bit messy to find out the correct xpath, here is the correct one,
using capybara 0.4.1.1
# <a href="/clowns?ordered_by=clumsyness" class="weep">View Clowns</a> page.should have_xpath("//a[@class='weep'][@href='/clowns?ordered_by=clumsyness']", :text => "View Clowns")
If you only have a link without a class, use
page.should have_link('View Clowns', :href => '/clowns?ordered_by=clumsyness')
Something like this will sadly not work:
page.should have_link('This will not work!', :href => '/clowns?ordered_by=clumsyness', :class => "weep")
The class option will be ignored.