更新:这个猴子补丁已合并到 watir-webdriver 中,因此在 watir-webdriver v0.6.5 中不再需要。您将能够使用以下方法设置超时:
Watir.default_timeout = 90
等待方法的定义类似于:
def when_present(timeout = 30)
message = "waiting for #{selector_string} to become present"
if block_given?
Watir::Wait.until(timeout, message) { present? }
yield self
else
WhenPresentDecorator.new(self, timeout, message)
end
end
如您所见,30 秒的默认超时时间是硬编码的。因此,没有简单的方法可以在任何地方更改它。
但是,您可以修改等待方法以使用默认时间并将其设置为您想要的。以下猴子补丁将默认超时设置为 90 秒。
require 'watir-webdriver'
module Watir
# Can be changed within a script with Watir.default_wait_time = 30
@default_wait_time = 90
class << self
attr_accessor :default_wait_time
end
module Wait
class << self
alias old_until until
def until(timeout = Watir.default_wait_time, message = nil, &block)
old_until(timeout, message, &block)
end
alias old_while while
def while(timeout = Watir.default_wait_time, message = nil, &block)
old_while(timeout, message, &block)
end
end # self
end # Wait
module EventuallyPresent
alias old_when_present when_present
def when_present(timeout = Watir.default_wait_time, &block)
old_when_present(timeout, &block)
end
alias old_wait_until_present wait_until_present
def wait_until_present(timeout = Watir.default_wait_time)
old_wait_until_present(timeout)
end
alias old_wait_while_present wait_while_present
def wait_while_present(timeout = Watir.default_wait_time)
old_wait_while_present(timeout)
end
end # EventuallyPresent
end # Watir
在加载 watir webdriver 代码后包含补丁。