簡體   English   中英

清除輸入字段:#的未定義方法“ clear” <Watir::Input:XYZ> (NoMethodError)

[英]Clear input field: undefined method `clear' for #<Watir::Input:XYZ> (NoMethodError)

我不確定為什么無法clear輸入字段。

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

步驟文件

page.set_search_value(search_entry)

輸出

undefined method `clear' for #'<'Watir::Input:0x00000003980d20'>' (NoMethodError)

沒有為通用輸入元素(即Watir::Input )定義clear (和set )方法。 它們僅針對特定的輸入類型(文本字段,復選框等)定義。

為了使代碼正常工作,您需要將input轉換為更具體的類型,這很可能是文本字段。 您可以使用to_subtype方法執行此操作:

test_input_field.when_present.to_subtype.clear
test_input_field.when_present.to_subtype.set(search_entry)

正如@SaurabhGaur提到的那樣, set已經從清除現有值開始,因此您可以執行以下操作:

test_input_field.when_present.to_subtype.set(search_entry)

除非input類型發生變化,否則將元素定義為text_field更為有意義,因此您無需轉換它。 這可能取決於您使用的頁面對象庫,但是我希望您可以這樣做:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM