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/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
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
Handles JavaScript alerts, confirms and prompts.
@return [Watir::Alert]
# File lib/watir/browser.rb, line 162 def alert Alert.new(self) end
# File lib/watir/browser.rb, line 273 def browser self end
Closes browser.
# File lib/watir/browser.rb, line 103 def close return if @closed @driver.quit @closed = true end
# 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
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
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
Returns HTML code of current page.
@return [String]
# File lib/watir/browser.rb, line 152 def html @driver.page_source end
# 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
# File lib/watir/browser.rb, line 259 def locate raise Error, 'browser was closed' if @closed ensure_context end
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
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
Returns readyState of document.
@return [String]
# File lib/watir/browser.rb, line 188 def ready_state execute_script 'return document.readyState' end
Handles screenshots of current pages.
@return [Watir::Screenshot]
# File lib/watir/browser.rb, line 244 def screenshot Screenshot.new self 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/browser.rb, line 234 def send_keys(*args) @driver.switch_to.active_element.send_keys(*args) end
Returns the text of status bar.
@return [String]
# File lib/watir/browser.rb, line 198 def status execute_script 'return window.status;' end
Returns text of page body.
@return [String]
# File lib/watir/browser.rb, line 142 def text body.text end
# File lib/watir/browser.rb, line 303 def timer @timer ||= Wait::Timer.new end
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
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
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
@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
# 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