class Watir::Browser
The main class through which you control the browser.
Attributes
Public Class Methods
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
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
@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
Handles JavaScript alerts, confirms and prompts.
@return [Watir::Alert]
# File lib/watir-webdriver/browser.rb, line 193 def alert Alert.new(self) end
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
Navigates back in history.
# File lib/watir-webdriver/browser.rb, line 87 def back @driver.navigate.back end
# File lib/watir-webdriver/browser.rb, line 356 def browser self end
Closes browser.
# File lib/watir-webdriver/browser.rb, line 134 def close return if @closed @driver.quit @closed = true end
@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
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
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
Navigates forward in history.
# File lib/watir-webdriver/browser.rb, line 95 def forward @driver.navigate.forward end
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
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
# 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
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
Returns readyState of document.
@return [String]
# File lib/watir-webdriver/browser.rb, line 225 def ready_state execute_script 'return document.readyState' end
Refreshes current page.
# File lib/watir-webdriver/browser.rb, line 201 def refresh @driver.navigate.refresh @after_hooks.run end
# File lib/watir-webdriver/browser.rb, line 352 def reset! # no-op end
@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
Handles screenshots of current pages.
@return [Watir::Screenshot]
# File lib/watir-webdriver/browser.rb, line 280 def screenshot Screenshot.new driver end
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
Returns the text of status bar.
@return [String]
# File lib/watir-webdriver/browser.rb, line 235 def status execute_script "return window.status;" end
Returns text of page body.
@return [String]
# File lib/watir-webdriver/browser.rb, line 172 def text body.text end
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
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
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
@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
# 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
# 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