如何编写一个Capybara断言来检查按钮的存在及其启用或禁用状态? [英] How do I write a Capybara assertion that checks for the presence of a button and its enabled or disabled state?

查看:87
本文介绍了如何编写一个Capybara断言来检查按钮的存在及其启用或禁用状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它可以根据用户界面中发生的事情启用/禁用按钮。

I have an app that enables/disables buttons in response to things that happen in the UI.

我可以轻松地使用水豚来检测按钮是否存在

I can easily use capybara to detect if a button exists

should have_button 'save'

但我不知道如何验证保存按钮的状态。那就是:

but I have no idea how to verify the state of the save button. That is:

我该如何编写一个Capybara断言来检查按钮的存在以及它的启用或禁用状态?

我将一张被禁用按钮的支票合在一起;对于启用,我想我可以验证是否有匹配的按钮,并且没有匹配的禁用按钮。

I have hacked together a check for a disabled button; for enabled, I suppose that I could verify that there is a matching button and that there is no matching disabled button. But this, to say the least, is clunky.

这似乎是一项基本的UI检查,我确定我已经错过了一些东西,但是我不能

This seems like such a fundamental UI check, that I am sure that I have missed something, but I can't seem to figure out what.

根据汇总答案回答:

正如我在评论中提到的那样,水豚的行为取决于底层驱动程序。我们正在使用webkit,它返回 true / false字符串结果。显然,其他驱动程序返回true / false。 Capybara的人们已经知道了这个问题(github.com/jnicklas/capybara/issues/705),但他们(可能是正确的)感到这并不是真正要解决的问题。

As I mentioned in the comment, the Capybara behavior is dependent upon the underlying driver. We are using webkit, and it returns "true"/"false" string results. Apparently, other drivers return true/false. The folks at Capybara are aware of the issue (github.com/jnicklas/capybara/issues/705), but they feel (probably correctly) that it isn't really their issue to resolve.

我没有让测试依赖于我使用的驱动程序,而是最终创建了一个自定义匹配器:

Rather than have my tests depend upon the driver I am using, I ended up creating a custom matcher:

RSpec::Matchers.define :be_enabled do
  match do |actual|
    driver_result = actual[:disabled]
    # nil, false, or "false" will all satisfy this matcher
    (driver_result.nil? || driver_result == false || driver_result == "false").should be_true
  end
end

RSpec::Matchers.define :be_disabled do
  match do |actual|
    driver_result = actual[:disabled]
    (driver_result == "disabled" || driver_result == true || driver_result == "true").should be_true
  end
end

然后您可以输入:

user_license_area.find_button('Save').should be_disabled


推荐答案

似乎Capybara对禁用元素的处理方式已经更改,所以我认为我要进行更新。

It appears that Capybara's handling of disabled elements has changed, so I figured I'd give an update.

测试页面是否具有按钮并启用,使用:

To test whether a page has a button and is enabled, use:

expect(page).to have_button('Save')

要测试页面是否具有按钮并被禁用,请使用:

To test whether a page has a button and is disabled, use:

expect(page).to have_button('Save', disabled: true)

此格式可用于 has_field? find_field 等。

您可以在 http://www.el上了解有关此更新的更多信息。 abs.se/tag/capybara

更新

旧链接断开。 这是讨论该功能的GitHub问题不幸的是,这是该方法的贫瘠文档

这篇关于如何编写一个Capybara断言来检查按钮的存在及其启用或禁用状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆