Ruby-on-rails 水豚断言元素的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5153550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:33:22  来源:igfitidea点击:

capybara assert attributes of an element

ruby-on-railsrspeccapybara

提问by kriysna

I'm using RSpec2 and Capybara for acceptance testing.

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

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

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

回答by bowsersenior

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

另一个简单的解决方案是访问您正在寻找的 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']
# => ""

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

请注意,它Capybara会自动尝试等待异步请求完成,但在某些情况下可能不起作用:

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

如果对异步更新的元素进行断言时遇到问题,这是一种解决方法:

回答by idlefingers

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!

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

回答by Clint

It was a bit messy to find out the correct xpath, here is the correct one,
using capybara 0.4.1.1

找出正确的 xpath 有点麻烦,这里是正确的,
使用水豚 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.

类选项将被忽略。

回答by Nick McCurdy

I recommend using have_linkand find_link(name)[:disabled]in two separate assertions. While performing the second assertion alone is simpler, this makes error messages about missing links look nicer, making your test results easier to read.

我建议在两个单独的断言中使用have_linkfind_link(name)[:disabled]。虽然单独执行第二个断言更简单,但这会使有关缺失链接的错误消息看起来更好,使您的测试结果更易于阅读。

expect(page).to have_link "Example"
expect(find_link("Example")[:disabled]).to be false

Note that "Example"can be changed to the name or id of the link.

请注意,"Example"可以更改为链接的名称或 id。

回答by kaikuchn

page.should have_link('It will work this way!', {:href => '/clowns?ordered_by=clumsyness', :class => "smile"})

have_link expects a hash of options which is empty if you do not provide any. You can specify any attributes the link should have - just make sure you pass all the options in ONE hash.

have_link 需要一个散列选项,如果您不提供任何选项,则该散列值为空。您可以指定链接应具有的任何属性 - 只需确保在 ONE 哈希中传递所有选项。

Hope this helps

希望这可以帮助

PS: For attributes like data-method you have to pass the attribute name as a string since the hyphen breaks the symbol.

PS:对于像 data-method 这样的属性,您必须将属性名称作为字符串传递,因为连字符会破坏符号。

回答by 18augst

bowsersenior, thanks for a hint

bowsersenior,谢谢你的提示

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

另一个简单的解决方案是使用 [] 访问您要查找的 HTML 属性

Here is an example:

下面是一个例子:

let(:action_items) { page.find('div.action_items') }

it "action items displayed as buttons" do
  action_items.all(:css, 'a').each do |ai|
    expect(ai[:class]).to match(/btn/)
  end
end

回答by Camilo Sad

Using Rspec3's syntax i did it this way:

使用 Rspec3 的语法我是这样做的:

expect(page).not_to have_selector(:link_or_button, 'Click here')

回答by Aravin

Simply you can use page.has_css?method

只需您可以使用page.has_css?方法

page.has_css?('.class_name') 

this will return trueif element exists.

true如果元素存在,这将返回。

Do some action based on validation.

根据验证执行一些操作。

page.has_css?('.class_name') do
  #some code
end

回答by pixelearth

According to the docsyou can use the [attribute] accessor syntax:

根据文档,您可以使用 [attribute] 访问器语法:

find('#selector')['class'] => "form-control text optional disabled"

For disabled, you could also do this:

对于残疾人,你也可以这样做:

expect(find('#selector').disabled?).to be(true)