在使用jQuery的prop方法时,如何使用Capybara测试表单元素是否被禁用?

时间:2021-04-28 21:14:27

In my Rails 3 app, I've been using jQuery's attr() method to do things like this:

在我的Rails 3应用程序中,我一直在使用jQuery的attr()方法来执行以下操作:

$('#application_dust_type_id').attr('disabled', 'disabled');

I would use Test/Unit, capybara and capybara-webkit to test this with:

我会使用Test / Unit,capybara和capybara-webkit来测试这个:

assert page.has_selector? 'select[id=application_dust_type_id][disabled=disabled]'

Now, I'm trying to switch to using the prop method, as jQuery recommends:

现在,我正在尝试切换到使用prop方法,因为jQuery建议:

$('#application_dust_type_id').prop('disabled', true)

But now the above assertion fails, even though the javascript still works the same in the browser.

但是现在上面的断言失败了,即使javascript在浏览器中仍然有效。

How can I make an assertion based on an element's property (instead of an attribute) using capybara?

如何使用capybara基于元素的属性(而不是属性)进行断言?

1 个解决方案

#1


7  

Try just checking for the existance of a disabled attribute as that is all that is really required to disable an input:

尝试检查已禁用属性是否存在,因为这是禁用输入所需的全部内容:

assert page.has_selector? 'select[id=application_dust_type_id][disabled]'

Note that the value of a disabled attribute is moot. You could set disabled="false" or disabled="foobar" and the element would be rendered as disabled.

请注意,禁用属性的值没有实际意义。您可以设置disabled =“false”或disabled =“foobar”,该元素将被渲染为禁用。

#1


7  

Try just checking for the existance of a disabled attribute as that is all that is really required to disable an input:

尝试检查已禁用属性是否存在,因为这是禁用输入所需的全部内容:

assert page.has_selector? 'select[id=application_dust_type_id][disabled]'

Note that the value of a disabled attribute is moot. You could set disabled="false" or disabled="foobar" and the element would be rendered as disabled.

请注意,禁用属性的值没有实际意义。您可以设置disabled =“false”或disabled =“foobar”,该元素将被渲染为禁用。