class Watir::Browser

The main class through which you control the browser.

Attributes

after_hooks[R]
default_context[W]
driver[R]
locator_namespace[W]
original_window[W]
timer[W]
wd[R]

Public Class Methods

new(browser = :chrome, *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/browser.rb, line 42
def initialize(browser = :chrome, *args)
  case browser
  when ::Symbol, String
    selenium_args = Capabilities.new(browser, *args).to_args
    @driver = Selenium::WebDriver.for(*selenium_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)
  @closed = false
  @default_context = true
end
start(url, browser = :chrome, *args) click to toggle source

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

@example

browser = Watir::Browser.start "www.google.com", :chrome
#=> #<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/browser.rb, line 30
def start(url, browser = :chrome, *args)
  new(browser, *args).tap { |b| b.goto url }
end

Public Instance Methods

alert() click to toggle source

Handles JavaScript alerts, confirms and prompts.

@return [Watir::Alert]

# File lib/watir/browser.rb, line 162
def alert
  Alert.new(self)
end
browser() click to toggle source
# File lib/watir/browser.rb, line 273
def browser
  self
end
close() click to toggle source

Closes browser.

# File lib/watir/browser.rb, line 103
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/browser.rb, line 117
def cookies
  @cookies ||= Cookies.new driver.manage
end
ensure_context() click to toggle source
# File lib/watir/browser.rb, line 265
def ensure_context
  return if @default_context

  driver.switch_to.default_content
  @default_context = true
  after_hooks.run
end
execute_script(script, *args, function_name: nil) 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/browser.rb, line 216
def execute_script(script, *args, function_name: nil)
  args.map! do |e|
    e.is_a?(Element) ? e.wait_until(&:exists?).wd : e
  end
  Watir.logger.info "Executing Script on Browser: #{function_name}" if function_name
  wrap_elements_in(self, @driver.execute_script(script, *args))
end
exist?() click to toggle source

Returns true if browser is not closed and false otherwise.

@return [Boolean]

# File lib/watir/browser.rb, line 254
def exist?
  !@closed && window.present?
end
Also aliased as: exists?
exists?()
Alias for: exist?
html() click to toggle source

Returns HTML code of current page.

@return [String]

# File lib/watir/browser.rb, line 152
def html
  @driver.page_source
end
inspect() click to toggle source
# File lib/watir/browser.rb, line 58
def inspect
  if alert.exists?
    format('#<%s:0x%x alert=true>', self.class, hash * 2)
  else
    format('#<%s:0x%x url=%s title=%s>', self.class, hash * 2, url.inspect, title.inspect)
  end
rescue Errno::ECONNREFUSED
  format('#<%s:0x%x closed=true>', self.class, hash * 2)
end
Also aliased as: selector_string
locate() click to toggle source
# File lib/watir/browser.rb, line 259
def locate
  raise Error, 'browser was closed' if @closed

  ensure_context
end
locator_namespace() click to toggle source

Whether the locators should be used from a different namespace. Defaults to Watir::Locators.

# File lib/watir/browser.rb, line 282
def locator_namespace
  @locator_namespace ||= Locators
end
name() click to toggle source

Returns browser name.

@example

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

@return [Symbol]

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

Returns readyState of document.

@return [String]

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

Handles screenshots of current pages.

@return [Watir::Screenshot]

# File lib/watir/browser.rb, line 244
def screenshot
  Screenshot.new self
end
selector_string()
Alias for: inspect
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/browser.rb, line 234
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/browser.rb, line 198
def status
  execute_script 'return window.status;'
end
text() click to toggle source

Returns text of page body.

@return [String]

# File lib/watir/browser.rb, line 142
def text
  body.text
end
timer() click to toggle source
# File lib/watir/browser.rb, line 303
def timer
  @timer ||= Wait::Timer.new
end
title() click to toggle source

Returns title of current page.

@example

browser.goto "watir.github.io"
browser.title
#=> "Watir Project"

@return [String]

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

Returns URL of current page.

@example

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

@return [String]

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

Waits until readyState of document is complete.

@example

browser.wait

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

# File lib/watir/browser.rb, line 176
def wait(timeout = 5)
  wait_until(timeout: timeout, message: "waiting for document.readyState == 'complete'") do
    ready_state == 'complete'
  end
end
wrap_elements_in(scope, obj) click to toggle source

@api private

# File lib/watir/browser.rb, line 290
def wrap_elements_in(scope, obj)
  case obj
  when Selenium::WebDriver::Element
    wrap_element(scope, obj)
  when Array
    obj.map { |e| wrap_elements_in(scope, e) }
  when Hash
    obj.each { |k, v| obj[k] = wrap_elements_in(scope, v) }
  else
    obj
  end
end

Private Instance Methods

wrap_element(scope, element) click to toggle source
# File lib/watir/browser.rb, line 309
def wrap_element(scope, element)
  Watir.element_class_for(element.tag_name.downcase).new(scope, element: element)
end