[英]watir webdriver using a variable
我在瀏覽器參考中使用變量時遇到問題。 我試圖傳遞諸如ul,ol等字段類型。如果我輸入ol,代碼可以工作,但我想使用標記為'field'的傳遞變量。 我收到了“未定義的字段方法”錯誤。
我也試過使用#{field},但這也不起作用。 錯誤是該字段未定義。
def CheckNewPageNoUl(browser, field, text1, output)
browser.a(:id => 'submitbtn').hover
browser.a(:id => 'submitbtn').click
output.puts("text1 #{text1}")
browser.body(:id => 'page').wait_until_present
if browser.table.field.exists?
output.puts(" #{text1} was found in CheckNewPageNoUl")
end
end
field = "ol"
text1 = "<ol>"
CheckText.CheckNewPageNoUl(b, field, text1, outputt)
要將字符串轉換為方法調用,請使用Object#send
,它可以使用兩個參數:
一些例子:
field = 'ol'
browser.send(field).exists?
#=> Translates to browser.ol.exists?
specifiers = {:text => 'text', :class => 'class'}
browser.send(field, specifiers).exists?
#=> Translates to browser.ol(:text => 'text', :class => 'class').exists?
對於您的代碼,您可能希望:
if browser.table.send(field).exists?
output.puts(" #{text1} was found in CheckNewPageNoUl")
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.