capybara断言元素的属性

kri*_*sna 85 rspec ruby-on-rails capybara

我正在使用RSpec2和Capybara进行验收测试.

我想断言链接在Capybara中是否被禁用.我怎样才能做到这一点?

bow*_*ior 143

另一个简单的解决方案是访问您正在寻找的HTML属性[]:

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']
# => ""
Run Code Online (Sandbox Code Playgroud)

请注意,Capybara将自动尝试等待异步请求完成,但在某些情况下可能无效:

如果您在异步更新的元素上遇到问题,则可以使用以下解决方法:

  • Vrey有用的解决方案.谢谢! (4认同)

idl*_*ers 90

你是如何禁用链接的?这是你要加的课吗?一个属性?

# 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
Run Code Online (Sandbox Code Playgroud)

实际的xpath选择器可能不正确.我经常不使用xpath!