class Watir::Browser

The main class through which you control the browser.

Attributes

after_hooks[R]
driver[R]
wd[R]

Public Class Methods

new(browser = :firefox, *args) click to toggle source

Creates a Watir::Browser instance.

@param [Symbol, Selenium::WebDriver] browser :firefox, :ie, :chrome, :remote or Selenium::WebDriver instance @param args Passed to the underlying driver

# File lib/watir-webdriver/browser.rb, line 43
def initialize(browser = :firefox, *args)
  case browser
  when ::Symbol, String
    @driver = Selenium::WebDriver.for browser.to_sym, *args
  when Selenium::WebDriver::Driver
    @driver = browser
  else
    raise ArgumentError, "expected Symbol or Selenium::WebDriver::Driver, got #{browser.class}"
  end

  @after_hooks = AfterHooks.new(self)
  @current_frame  = nil
  @closed = false
end
start(url, browser = :firefox, *args) click to toggle source

Creates a Watir::Browser instance and goes to URL.

@example

browser = Watir::Browser.start "www.google.com", :phantomjs
#=> #<Watir::Browser:0x..fa45a499cb41e1752 url="http://www.google.com" title="Google">

@param [String] url @param [Symbol, Selenium::WebDriver] browser :firefox, :ie, :chrome, :remote or Selenium::WebDriver instance @return [Watir::Browser]

# File lib/watir-webdriver/browser.rb, line 28
def start(url, browser = :firefox, *args)
  b = new(browser, *args)
  b.goto url

  b
end

Public Instance Methods

add_checker(checker = nil, &block) click to toggle source

@deprecated Use `Watir::AfterHooks#add` instead

# File lib/watir-webdriver/browser.rb, line 288
def add_checker(checker = nil, &block)
  warn 'Browser#add_checker is deprecated. Use Browser#after_hooks#add instead.'
  @after_hooks.add(checker, &block)
end
alert() click to toggle source

Handles JavaScript alerts, confirms and prompts.

@return [Watir::Alert]

# File lib/watir-webdriver/browser.rb, line 193
def alert
  Alert.new(self)
end
assert_exists() click to toggle source

Protocol shared with Watir::Element

@api private

# File lib/watir-webdriver/browser.rb, line 340
def assert_exists
  if @closed
    raise Exception::Error, "browser was closed"
  elsif !window.present?
    raise Exception::NoMatchingWindowFoundException, "browser window was closed"
  else
    driver.switch_to.default_content
    true
  end
end
Also aliased as: ensure_not_stale
back() click to toggle source

Navigates back in history.

# File lib/watir-webdriver/browser.rb, line 87
def back
  @driver.navigate.back
end
browser() click to toggle source
# File lib/watir-webdriver/browser.rb, line 356
def browser
  self
end
close() click to toggle source

Closes browser.

# File lib/watir-webdriver/browser.rb, line 134
def close
  return if @closed
  @driver.quit
  @closed = true
end
Also aliased as: quit
cookies() click to toggle source

Handles cookies.

@return [Watir::Cookies]

# File lib/watir-webdriver/browser.rb, line 147
def cookies
  @cookies ||= Cookies.new driver.manage
end
disable_checker(checker) click to toggle source

@deprecated Use `Watir::AfterHooks#delete` instead

# File lib/watir-webdriver/browser.rb, line 297
def disable_checker(checker)
  warn 'Browser#disable_checker is deprecated. Use Browser#after_hooks#delete instead.'
  @after_hooks.delete(checker)
end
ensure_not_stale()
Alias for: assert_exists
execute_script(script, *args) click to toggle source

Executes JavaScript snippet.

If you are going to use the value snippet returns, make sure to use `return` explicitly.

@example Check that Ajax requests are completed with jQuery

browser.execute_script("return jQuery.active") == 0
#=> true

@param [String] script JavaScript snippet to execute @param *args Arguments will be available in the given script in the 'arguments' pseudo-array

# File lib/watir-webdriver/browser.rb, line 253
def execute_script(script, *args)
  args.map! { |e| e.kind_of?(Watir::Element) ? e.wd : e }
  returned = @driver.execute_script(script, *args)

  wrap_elements_in(returned)
end
exist?() click to toggle source

Returns true if browser is not closed and false otherwise.

@return [Boolean]

# File lib/watir-webdriver/browser.rb, line 326
def exist?
  assert_exists
  true
rescue Exception::NoMatchingWindowFoundException, Exception::Error
  false
end
Also aliased as: exists?
exists?()
Alias for: exist?
forward() click to toggle source

Navigates forward in history.

# File lib/watir-webdriver/browser.rb, line 95
def forward
  @driver.navigate.forward
end
goto(uri) click to toggle source

Goes to the given URL.

@example

browser.goto "watir.github.io"

@param [String] uri The url. @return [String] The url you end up at.

# File lib/watir-webdriver/browser.rb, line 74
def goto(uri)
  uri = "http://#{uri}" unless uri =~ URI.regexp

  @driver.navigate.to uri
  @after_hooks.run

  uri
end
html() click to toggle source

Returns HTML code of current page.

@return [String]

# File lib/watir-webdriver/browser.rb, line 182
def html
  # use body.html instead?
  @driver.page_source
end
inspect() click to toggle source
# File lib/watir-webdriver/browser.rb, line 58
def inspect
  '#<%s:0x%x url=%s title=%s>' % [self.class, hash*2, url.inspect, title.inspect]
rescue
  '#<%s:0x%x closed=%s>' % [self.class, hash*2, @closed.to_s]
end
name() click to toggle source

Returns browser name.

@example

browser = Watir::Browser.new :phantomjs
browser.name
#=> :phantomjs

@return [Symbol]

# File lib/watir-webdriver/browser.rb, line 162
def name
  @driver.browser
end
quit()
Alias for: close
ready_state() click to toggle source

Returns readyState of document.

@return [String]

# File lib/watir-webdriver/browser.rb, line 225
def ready_state
  execute_script 'return document.readyState'
end
refresh() click to toggle source

Refreshes current page.

# File lib/watir-webdriver/browser.rb, line 201
def refresh
  @driver.navigate.refresh
  @after_hooks.run
end
reset!() click to toggle source
# File lib/watir-webdriver/browser.rb, line 352
def reset!
  # no-op
end
run_checkers() click to toggle source

@deprecated Use `Watir::AfterHooks#run` instead

# File lib/watir-webdriver/browser.rb, line 306
def run_checkers
  warn 'Browser#run_checkers is deprecated. Use Browser#after_hooks#run instead.'
  @after_hooks.run
end
screenshot() click to toggle source

Handles screenshots of current pages.

@return [Watir::Screenshot]

# File lib/watir-webdriver/browser.rb, line 280
def screenshot
  Screenshot.new driver
end
send_keys(*args) click to toggle source

Sends sequence of keystrokes to currently active element.

@example

browser.goto "www.google.com"
browser.send_keys "Watir", :return

@param [String, Symbol] *args

# File lib/watir-webdriver/browser.rb, line 270
def send_keys(*args)
  @driver.switch_to.active_element.send_keys(*args)
end
status() click to toggle source

Returns the text of status bar.

@return [String]

# File lib/watir-webdriver/browser.rb, line 235
def status
  execute_script "return window.status;"
end
text() click to toggle source

Returns text of page body.

@return [String]

# File lib/watir-webdriver/browser.rb, line 172
def text
  body.text
end
title() click to toggle source

Returns title of current page.

@example

browser.goto "watir.github.io"
browser.title
#=> "Watir is... – Watir Project – Watir stands for Web Application Testing In Ruby. It facilitates the writing of automated tests by mimicking the behavior of a user interacting with a website."

@return [String]

# File lib/watir-webdriver/browser.rb, line 126
def title
  @driver.title
end
url() click to toggle source

Returns URL of current page.

@example

browser.goto "watir.github.io"
browser.url
#=> "http://watir.github.io/"

@return [String]

# File lib/watir-webdriver/browser.rb, line 110
def url
  assert_exists
  @driver.current_url
end
wait(timeout = 5) click to toggle source

Waits until readyState of document is complete.

@param [Fixnum] timeout @raise [Watir::Wait::TimeoutError] if timeout is exceeded

# File lib/watir-webdriver/browser.rb, line 213
def wait(timeout = 5)
  wait_until(timeout, "waiting for document.readyState == 'complete'") do
    ready_state == "complete"
  end
end
without_checkers(&block) click to toggle source

@deprecated Use `Watir::AfterHooks#without` instead

# File lib/watir-webdriver/browser.rb, line 315
def without_checkers(&block)
  warn 'Browser#without_checkers is deprecated. Use Browser#after_hooks#without instead.'
  @after_hooks.without(&block)
end

Private Instance Methods

wrap_element(element) click to toggle source
# File lib/watir-webdriver/browser.rb, line 377
def wrap_element(element)
  Watir.element_class_for(element.tag_name.downcase).new(self, element: element)
end
wrap_elements_in(obj) click to toggle source
# File lib/watir-webdriver/browser.rb, line 362
def wrap_elements_in(obj)
  case obj
  when Selenium::WebDriver::Element
    wrap_element(obj)
  when Array
    obj.map { |e| wrap_elements_in(e) }
  when Hash
    obj.each { |k,v| obj[k] = wrap_elements_in(v) }

    obj
  else
    obj
  end
end