Module: Watir::Waitable

Included in:
Alert, Browser, Element, Window
Defined in:
lib/watir/wait.rb

Overview

Wait

Instance Method Summary collapse

Instance Method Details

#wait_until(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk) ⇒ Object

Waits until the condition is true.

Examples:

browser.wait_until(timeout: 2) do |browser|
  browser.windows.size == 1
end
browser.text_field(name: "new_user_first_name").wait_until(&:present?).click
browser.text_field(name: "new_user_first_name").wait_until(message: 'foo') { |field| field.present? }
browser.text_field(name: "new_user_first_name").wait_until(timeout: 60, &:present?)
browser.text_field(name: "new_user_first_name").wait_until(timeout: 60, name: 'new_user_first_name')

Parameters:

  • timeout (Integer) (defaults to: nil)

    seconds to wait before timing out

  • message (String) (defaults to: nil)

    error message for when times out


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/watir/wait.rb', line 114

def wait_until(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk)
  if depr_message || depr_timeout
    Watir.logger.deprecate 'Using arguments for #wait_until', 'keywords', ids: [:timeout_arguments]
    timeout = depr_timeout
    message = depr_message
  end
  message ||= proc { |obj| "waiting for true condition on #{obj.inspect}" }

  # TODO: Consider throwing argument error for mixing block & options
  proc = create_proc(opt, &blk)

  Wait.until(timeout: timeout, message: message, interval: interval, object: self, &proc)

  self
end

#wait_until_present(depr_timeout = nil, timeout: nil, interval: nil, message: nil) ⇒ Object

Waits until the element is present. Element is always relocated, so this can be used in the case of an element going away and returning

Examples:

browser.text_field(name: "new_user_first_name").wait_until_present

Parameters:

  • timeout (Integer) (defaults to: nil)

    seconds to wait before timing out

  • interval (Float) (defaults to: nil)

    seconds to wait before each try

  • message (String) (defaults to: nil)

    error message for when times out

See Also:


176
177
178
179
180
181
182
183
184
# File 'lib/watir/wait.rb', line 176

def wait_until_present(depr_timeout = nil, timeout: nil, interval: nil, message: nil)
  timeout = depr_timeout if depr_timeout
  Watir.logger.deprecate "#{self.class}#wait_until_present",
                         "#{self.class}#wait_until(&:present?)",
                         ids: [:wait_until_present]

  message ||= proc { |obj| "waiting for #{obj.inspect} to become present" }
  wait_until(timeout: timeout, interval: interval, message: message, element_reset: true, &:present?)
end

#wait_while(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk) ⇒ Object

TODO:

add element example

Waits while the condition is true.

Examples:

browser.wait_while(timeout: 2) do |browser|
  !browser.exists?
end
browser.wait_while(timeout: 2, title: 'No')

Parameters:

  • timeout (Integer) (defaults to: nil)

    seconds to wait before timing out

  • message (String) (defaults to: nil)

    error message for when times out


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/watir/wait.rb', line 145

def wait_while(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk)
  if depr_message || depr_timeout
    Watir.logger.deprecate 'Using arguments for #wait_while', 'keywords', ids: [:timeout_arguments]
    timeout = depr_timeout
    message = depr_message
  end
  message ||= proc { |obj| "waiting for false condition on #{obj.inspect}" }

  # TODO: Consider throwing argument error for mixing block & options
  proc = create_proc(opt, &blk)

  Wait.while(timeout: timeout, message: message, interval: interval, object: self, &proc)

  self
end

#wait_while_present(depr_timeout = nil, timeout: nil, interval: nil, message: nil) ⇒ Object

Waits while the element is present. Element is always relocated, so this can be used in the case of the element changing attributes

Examples:

browser.text_field(name: "abrakadbra").wait_while_present

Parameters:

  • timeout (Integer) (defaults to: nil)

    seconds to wait before timing out

  • interval (Float) (defaults to: nil)

    seconds to wait before each try

  • message (String) (defaults to: nil)

    error message for when times out

See Also:


201
202
203
204
205
206
207
208
209
# File 'lib/watir/wait.rb', line 201

def wait_while_present(depr_timeout = nil, timeout: nil, interval: nil, message: nil)
  timeout = depr_timeout if depr_timeout
  Watir.logger.deprecate "#{self.class}#wait_while_present",
                         "#{self.class}#wait_while(&:present?)",
                         ids: [:wait_while_present]

  message ||= proc { |obj| "waiting for #{obj.inspect} not to be present" }
  wait_while(timeout: timeout, interval: interval, message: message, element_reset: true, &:present?)
end