Ruby-on-rails 如何编写 Capybara 断言来检查按钮是否存在及其启用或禁用状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12917227/
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-02 20:52:47  来源:igfitidea点击:

How do I write a Capybara assertion that checks for the presence of a button and its enabled or disabled state?

ruby-on-railscapybara

提问by Andy Davis

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

我有一个应用程序可以启用/禁用按钮以响应 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:

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

How do I write a Capybara assertion that checks for the presence of a button andits enabled or disabled state?

我怎样写一个水豚断言,对于一个按钮存在检查它的启用或禁用状态?

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.

我已经破解了一个禁用按钮的检查;对于启用,我想我可以验证有一个匹配的按钮并且没有匹配的禁用按钮。但是,至少可以说,这很笨拙。

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.

这似乎是一个基本的 UI 检查,我确信我错过了一些东西,但我似乎无法弄清楚是什么。



Follow up based on gregates's answer:

根据 gregates 的回答进行跟进:

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.

正如我在评论中提到的,Capybara 的行为取决于底层驱动程序。我们正在使用 webkit,它返回“真”/“假”字符串结果。显然,其他驱动程序返回真/假。Capybara 的人知道这个问题 (github.com/jnicklas/capybara/issues/705),但他们觉得(可能是正确的)这不是他们真正需要解决的问题。

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

Then you can type:

然后你可以输入:

user_license_area.find_button('Save').should be_disabled

回答by J3RN

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

Capybara 对禁用元素的处理似乎发生了变化,所以我想我会提供更新。

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)

This format works with has_field?, find_field, etc.

这种格式能够在has_field?find_field等等。

You can read more on this update at http://www.elabs.se/tag/capybara

您可以在http://www.elabs.se/tag/capybara上阅读有关此更新的更多信息

UPDATE

更新

The old link is broken. This is the GitHub issue where the feature was discussedand this is the unfortunately barren documentation for the method.

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

回答by gregates

find_button('save')[:disabled].should eq "disabled"

Note that one line will effectively test for both existence and disabled state.

请注意,一行将有效地测试存在和禁用状态。

Edit: for enabled, try

编辑:启用,尝试

find_button('save')[:disabled].should_not be

(Either of these tests may need to be tweaked depending on precisely how you disable/enable the button.)

(这些测试中的任何一个都可能需要根据您禁用/启用按钮的精确方式进行调整。)

回答by Thomas

There are many ways to do this. I think the best way is to just use css selectors

有很多方法可以做到这一点。我认为最好的方法是只使用 css 选择器

expect(page).to have_css("#save:enabled")

This example asumes that you have set the css id to save.

此示例假设您已将 css id 设置为保存。

回答by Piotr K?dziak

There is be_disabled check

有 be_disabled 检查

it "allows finding elements and checking if they are disabled" do
  expect(string.find('//form/input[@name="bleh"]')).to be_disabled
  expect(string.find('//form/input[@name="meh"]')).not_to be_disabled
end

https://github.com/jnicklas/capybara/blob/2a51b817b355f6c1a5e95a471a87f1a492562e55/spec/basic_node_spec.rb#L108

https://github.com/jnicklas/capybara/blob/2a51b817b355f6c1a5e95a471a87f1a492562e55/spec/basic_node_spec.rb#L108

回答by Jon Kern

From a similar situation where the user's actions in the UI (selecting document types) triggers buttons to become enabled...

从用户在 UI 中的操作(选择文档类型)触发按钮变为启用状态的类似情况...

With an id on the control, it becomes pretty clean to find the button (regardless of state) and assert a state of "disabled-ness".

有了控件上的 id,找到按钮(无论状态如何)并断言“禁用”状态变得非常干净。

  @javascript
  Scenario: Package generation not allowed unless a document type is selected
    Given I view the "Liberty and 4th Building Renovation" project
    When I view the Package Creator
    Then both document package generation buttons should be disabled

  @javascript
  Scenario: Package generation allowed when documents types are selected
    Given I view the "Liberty and 4th Building Renovation" project
    When I view the Package Creator
    And I select any document type
    Then both document package generation buttons should be enabled

And the supporting cucumber feature steps with Capybara:

和 Capybara 的配套黄瓜功能步骤:

Then(/^both document package generation buttons should be disabled$/) do
  expect(find_by_id('generate_pdf')).to be_disabled
  expect(find_by_id('generate_zip')).to be_disabled
end

Then(/^both document package generation buttons should be enabled/) do
  expect(find_by_id('generate_pdf')).not_to be_disabled
  expect(find_by_id('generate_zip')).not_to be_disabled
end