我有一个应用程序,可以根据UI中发生的事情启用/禁用按钮.
我可以很容易地用水豚来检测按钮是否存在
should have_button 'save'
但我不知道如何验证保存按钮的状态.也就是说:
How do I write a Capybara assertion that checks for the presence of a button 100 its enabled or disabled state?
我拼凑了一张支票,上面有一个禁用的按钮;对于enabled,我想我可以验证是否有匹配的按钮,以及是否没有匹配的disabled按钮.但至少可以说,这是笨重的.
这看起来像是一个基本的UI判断,我确信我错过了一些东西,但我似乎不知道是什么.
根据格雷盖茨的回答进行跟进:
正如我在 comments 中提到的,水豚的行为取决于潜在的驱动因素.我们正在使用webkit,它会返回"true"/"false"字符串结果.显然,其他司机的回答是真/假.Capybara的员工知道这个问题(github.com/jnicklas/Capybara/issues/705),但他们觉得(可能是正确的)这不是他们真正需要解决的问题.
我没有让测试依赖于我使用的驱动程序,而是创建了一个自定义匹配器:
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