[英]Clear input field: undefined method `clear' for #<Watir::Input:XYZ> (NoMethodError)
I am not sure why I can not clear
my input field. 我不确定为什么无法
clear
输入字段。
PageObject : PageObject :
element(:test_input_field) { |b| b.input(class: "search-field") }
def set_search_value(search_entry)
test_input_field.when_present.clear
test_input_field.when_present.set(search_entry)
end
Step_file : 步骤文件 :
page.set_search_value(search_entry)
Output : 输出 :
undefined method `clear' for #'<'Watir::Input:0x00000003980d20'>' (NoMethodError)
The clear
(and set
) method are not defined for generic input elements - ie Watir::Input
. 没有为通用输入元素(即
Watir::Input
)定义clear
(和set
)方法。 They are only defined for the specific input types - text field, checkbox, etc. 它们仅针对特定的输入类型(文本字段,复选框等)定义。
To make the code work, you would need to convert the input
into the more specific type, which is likely a text field. 为了使代码正常工作,您需要将
input
转换为更具体的类型,这很可能是文本字段。 You can do this using the to_subtype
method: 您可以使用
to_subtype
方法执行此操作:
test_input_field.when_present.to_subtype.clear
test_input_field.when_present.to_subtype.set(search_entry)
As @SaurabhGaur mentions, set
already starts by clearing the existing value, so you could just do: 正如@SaurabhGaur提到的那样,
set
已经从清除现有值开始,因此您可以执行以下操作:
test_input_field.when_present.to_subtype.set(search_entry)
Unless the input
type changes, it would make more sense to define the element as a text_field
so you do not need to convert it. 除非
input
类型发生变化,否则将元素定义为text_field
更为有意义,因此您无需转换它。 It might depend on which page object library you are using, but I would expect you could do: 这可能取决于您使用的页面对象库,但是我希望您可以这样做:
element(:test_input_field) { |b| b.text_field(class: "search-field") }
def set_search_value(search_entry)
test_input_field.when_present.clear
test_input_field.when_present.set(search_entry)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.