Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

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

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:

How do I write a Capybara assertion that checks for the presence of a button and its 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.


Follow up based on gregates's answer:

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

Then you can type:

user_license_area.find_button('Save').should be_disabled
question from:https://stackoverflow.com/questions/12917227/how-do-i-write-a-capybara-assertion-that-checks-for-the-presence-of-a-button-and

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

This format works with has_field?, find_field, etc.

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

UPDATE

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...