class Watir::Alert

Public Class Methods

new(browser) click to toggle source
# File lib/watir/alert.rb, line 7
def initialize(browser)
  @browser = browser
  @alert = nil
end

Public Instance Methods

close() click to toggle source

Closes alert or cancels prompts/confirms.

@example

browser.alert.close
browser.alert.exists?
#=> false
# File lib/watir/alert.rb, line 51
def close
  wait_for_exists
  @alert.dismiss
  @browser.after_hooks.run
end
exist?()
Alias for: exists?
exists?() click to toggle source

Returns true if alert, confirm or prompt is present and false otherwise.

@example

browser.alert.exists?
#=> true
# File lib/watir/alert.rb, line 80
def exists?
  assert_exists
  true
rescue UnknownObjectException
  false
end
Also aliased as: present?, exist?
ok() click to toggle source

Closes alert or accepts prompts/confirms.

@example

browser.alert.ok
browser.alert.exists?
#=> false
# File lib/watir/alert.rb, line 36
def ok
  wait_for_exists
  @alert.accept
  @browser.after_hooks.run
end
present?()
Alias for: exists?
selector_string() click to toggle source

@api private @see Watir::Wait

# File lib/watir/alert.rb, line 94
def selector_string
  'alert'
end
set(value) click to toggle source

Enters text to prompt.

@example

browser.alert.set "Text for prompt"
browser.alert.ok

@param [String] value

# File lib/watir/alert.rb, line 67
def set(value)
  wait_for_exists
  @alert.send_keys(value)
end
text() click to toggle source

Returns text of alert.

@example

browser.alert.text
#=> "ok"

@return [String]

# File lib/watir/alert.rb, line 22
def text
  wait_for_exists
  @alert.text
end

Private Instance Methods

assert_exists() click to toggle source
# File lib/watir/alert.rb, line 100
def assert_exists
  @alert = @browser.driver.switch_to.alert
rescue Selenium::WebDriver::Error::NoSuchAlertError
  raise UnknownObjectException, 'unable to locate alert'
end
wait_for_exists() click to toggle source
# File lib/watir/alert.rb, line 106
def wait_for_exists
  return assert_exists unless Watir.relaxed_locate?

  begin
    wait_until(message: 'waiting for alert', &:exists?)
  rescue Wait::TimeoutError
    raise UnknownObjectException, 'unable to locate alert'
  end
end