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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…