水豚断言元素的属性
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
另一个简单的解决方案是使用
[]
访问您要查找的 HTML 属性:请注意,
Capybara
会自动尝试等待异步请求完成,但它可能不起作用在某些情况下:如果您在对异步更新的元素进行断言时遇到问题,这里有一种解决方法:
Another simple solution is to access the HTML attribute you are looking for with
[]
: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:
你如何禁用该链接?这是您要添加的课程吗?属性?
实际的 xpath 选择器可能不正确。我不经常使用 xpath!
How are you disabling the link? Is it a class you're adding? An attribute?
The actual xpath selectors may be incorrect. I don't use xpath often!
找出正确的 xpath 有点混乱,这是正确的,
使用 capybara 0.4.1.1
如果您只有一个没有类的链接,请使用
类似这样的东西,遗憾的是不起作用:
类选项将被忽略。
It was a bit messy to find out the correct xpath, here is the correct one,
using capybara 0.4.1.1
If you only have a link without a class, use
Something like this will sadly not work:
The class option will be ignored.
我建议在两个单独的断言中使用
have_link
和find_link(name)[:disabled]
。虽然单独执行第二个断言更简单,但这使得有关丢失链接的错误消息看起来更好,使您的测试结果更易于阅读。请注意,
"Example"
可以更改为链接的名称或 ID。I recommend using
have_link
andfind_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.Note that
"Example"
can be changed to the name or id of the link.have_link 需要一个选项哈希,如果您不提供任何选项,则该哈希为空。您可以指定链接应具有的任何属性 - 只需确保在一个哈希中传递所有选项即可。
希望这会有所帮助
PS:对于像 data-method 这样的属性,您必须将属性名称作为字符串传递,因为连字符会破坏符号。
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.
只要有可能,您应该尝试使用 Capybara 提供的包装器,这将在各个驱动程序之间更加一致地工作。
对于
disabled
的特殊情况,在2.1中引入了一个包装器:https://github.com/jnicklas/capybara/blob/fc56557a5463b9d944207f2efa401faa5b49d9ef/History.md#version-210如果您使用它,您将在 RackTest 和 Poltergeist 上获得合理的结果:
HTML :
测试:
请注意,如果您开始使用支持 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-210If you use it, you will get sensible results on both RackTest and Poltergeist:
HTML:
Tests:
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.
只需使用
page.has_css?
方法,如果元素存在,该方法将返回
true
。根据验证执行一些操作。
Simply you can use
page.has_css?
methodthis will return
true
if element exists.Do some action based on validation.
bowsersenior,感谢您的提示
这是一个示例:
bowsersenior, thanks for a hint
Here is an example:
使用 Rspec3 的语法,我这样做了:
Using Rspec3's syntax i did it this way:
根据 文档 您可以使用[attribute] 访问器语法:
对于禁用的情况,您还可以执行以下操作:
According to the docs you can use the [attribute] accessor syntax:
For disabled, you could also do this: