水豚断言元素的属性

发布于 2024-10-19 14:38:43 字数 67 浏览 2 评论 0原文

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

我想断言水豚中的链接是否被禁用。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

小耗子 2024-10-26 14:38:43

另一个简单的解决方案是使用 [] 访问您要查找的 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']
# => ""

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

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

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:

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

梦晓ヶ微光ヅ倾城 2024-10-26 14:38:43

你如何禁用该链接?这是您要添加的课程吗?属性?

# 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

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

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!

两相知 2024-10-26 14:38:43

找出正确的 xpath 有点混乱,这是正确的,
使用 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")

如果您只有一个没有类的链接,请使用

page.should have_link('View Clowns', :href => '/clowns?ordered_by=clumsyness')

类似这样的东西,遗憾的是不起作用:

page.should have_link('This will not work!', :href => '/clowns?ordered_by=clumsyness', :class => "weep")

类选项将被忽略。

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.

胡大本事 2024-10-26 14:38:43

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

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

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

I recommend using have_link and 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.

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.

淡墨 2024-10-26 14:38:43
page.should have_link('It will work this way!', {:href => '/clowns?ordered_by=clumsyness', :class => "smile"})

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

希望这会有所帮助

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

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.

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.

柳若烟 2024-10-26 14:38:43

只要有可能,您应该尝试使用 Capybara 提供的包装器,这将在各个驱动程序之间更加一致地工作。

对于disabled的特殊情况,在2.1中引入了一个包装器:https://github.com/jnicklas/capybara/blob/fc56557a5463b9d944207f2efa401faa5b49d9ef/History.md#version-210

如果您使用它,您将在 RackTest 和 Poltergeist 上获得合理的结果:

HTML :

<input type="text" id="disabled-false"            ></div>
<input type="text" id="disabled-true"     disabled></div>
<input type="text" id="disabled-js-true"          ></div>
<input type="text" id="disabled-js-false" disabled></div>
<script>
  document.getElementById('disabled-js-true').disabled = true
  document.getElementById('disabled-js-false').disabled = false
</script>

测试:

!all(:field, 'disabled-false',    disabled: false).empty? or raise
 all(:field, 'disabled-false',    disabled: true ).empty? or raise
 all(:field, 'disabled-true',     disabled: false).empty? or raise
!all(:field, 'disabled-true',     disabled: true ).empty? or raise
 all(:field, 'disabled-js-true',  disabled: true ).empty? or raise
 all(:field, 'disabled-js-false', disabled: false).empty? or raise

Capybara.current_driver = :poltergeist
!all(:field, 'disabled-false',    disabled: false).empty? or raise
 all(:field, 'disabled-false',    disabled: true ).empty? or raise
 all(:field, 'disabled-true',     disabled: false).empty? or raise
!all(:field, 'disabled-true',     disabled: true ).empty? or raise
!all(:field, 'disabled-js-true',  disabled: true ).empty? or raise
!all(:field, 'disabled-js-false', disabled: false).empty? or raise

请注意,如果您开始使用支持 Js 的驱动程序,请注意如何使用它而不是 CSS 选择器,Javascript 测试将无需任何更改即可工作。

可运行的测试文件此处

Whenever possible, you should try to use the Capybara provided wrappers which will work more consistently across drivers.

For the particular case of disabled, a wrapper was introduced in 2.1: https://github.com/jnicklas/capybara/blob/fc56557a5463b9d944207f2efa401faa5b49d9ef/History.md#version-210

If you use it, you will get sensible results on both RackTest and Poltergeist:

HTML:

<input type="text" id="disabled-false"            ></div>
<input type="text" id="disabled-true"     disabled></div>
<input type="text" id="disabled-js-true"          ></div>
<input type="text" id="disabled-js-false" disabled></div>
<script>
  document.getElementById('disabled-js-true').disabled = true
  document.getElementById('disabled-js-false').disabled = false
</script>

Tests:

!all(:field, 'disabled-false',    disabled: false).empty? or raise
 all(:field, 'disabled-false',    disabled: true ).empty? or raise
 all(:field, 'disabled-true',     disabled: false).empty? or raise
!all(:field, 'disabled-true',     disabled: true ).empty? or raise
 all(:field, 'disabled-js-true',  disabled: true ).empty? or raise
 all(:field, 'disabled-js-false', disabled: false).empty? or raise

Capybara.current_driver = :poltergeist
!all(:field, 'disabled-false',    disabled: false).empty? or raise
 all(:field, 'disabled-false',    disabled: true ).empty? or raise
 all(:field, 'disabled-true',     disabled: false).empty? or raise
!all(:field, 'disabled-true',     disabled: true ).empty? or raise
!all(:field, 'disabled-js-true',  disabled: true ).empty? or raise
!all(:field, 'disabled-js-false', disabled: false).empty? or raise

Note how by using this instead of CSS selectors, the Javascript tests will work without any changes if you start using a Js capable driver.

Runnable test file here.

携君以终年 2024-10-26 14:38:43

只需使用 page.has_css? 方法,

page.has_css?('.class_name') 

如果元素存在,该方法将返回 true

根据验证执行一些操作。

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

Simply you can use page.has_css? method

page.has_css?('.class_name') 

this will return true if element exists.

Do some action based on validation.

page.has_css?('.class_name') do
  #some code
end
蓝眸 2024-10-26 14:38:43

bowsersenior,感谢您的提示

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

这是一个示例:

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

bowsersenior, thanks for a hint

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

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
陌上青苔 2024-10-26 14:38:43

使用 Rspec3 的语法,我这样做了:

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

Using Rspec3's syntax i did it this way:

expect(page).not_to have_selector(:link_or_button, 'Click here')
素染倾城色 2024-10-26 14:38:43

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

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

对于禁用的情况,您还可以执行以下操作:

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

According to the docs you can use the [attribute] accessor syntax:

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

For disabled, you could also do this:

expect(find('#selector').disabled?).to be(true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文