Class: Watir::Browser

Inherits:
Object
  • Object
show all
Includes:
AlertHelper, Container, HasWindow, Waitable
Defined in:
lib/watir-webdriver/browser.rb,
lib/watir-webdriver/extensions/alerts.rb,
lib/watir-webdriver/extensions/nokogiri.rb

Overview

The main class through which you control the browser.

Constant Summary

Constants included from Atoms

Atoms::ATOMS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AlertHelper

#alert, #confirm, #prompt

Methods included from Waitable

#wait_until, #wait_while

Methods included from HasWindow

#window, #windows

Methods included from Container

#a, #abbr, #abbrs, #address, #addresses, #area, #areas, #article, #articles, #as, #aside, #asides, #audio, #audios, #b, #base, #bases, #bdi, #bdis, #bdo, #bdos, #blockquote, #blockquotes, #body, #bodys, #br, #brs, #bs, #button, #buttons, #canvas, #canvases, #caption, #captions, #checkbox, #checkboxes, #cite, #cites, #code, #codes, #col, #colgroup, #colgroups, #cols, #command, #commands, #data, #datalist, #datalists, #dd, #dds, #del, #dels, #details, #detailses, #dfn, #dfns, #div, #divs, #dl, #dls, #dt, #dts, #element, #elements, #em, #embed, #embeds, #ems, #extract_selector, #field_set, #field_sets, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #form, #forms, #frame, #frames, #frameset, #framesets, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #head, #header, #headers, #heads, #hgroup, #hgroups, #hidden, #hiddens, #hr, #hrs, #htmls, #i, #iframe, #iframes, #image, #images, #img, #imgs, #input, #inputs, #ins, #inses, #is, #kbd, #kbds, #keygen, #keygens, #label, #labels, #legend, #legends, #li, #link, #links, #lis, #map, #maps, #mark, #marks, #menu, #menus, #meta, #metas, #meter, #meters, #nav, #navs, #noscript, #noscripts, #object, #objects, #ol, #ols, #optgroup, #optgroups, #option, #options, #output, #outputs, #p, #param, #params, #pre, #pres, #progress, #progresses, #ps, #q, #qs, #radio, #radios, #rp, #rps, #rt, #rts, #rubies, #ruby, #s, #samp, #samps, #script, #scripts, #section, #sections, #select, #select_list, #select_lists, #selects, #small, #smalls, #source, #sources, #span, #spans, #ss, #strong, #strongs, #style, #styles, #sub, #subs, #summaries, #summary, #sup, #sups, #table, #tables, #tbody, #tbodys, #td, #tds, #text_field, #text_fields, #textarea, #textareas, #tfoot, #tfoots, #th, #thead, #theads, #ths, #time, #times, #titles, #tr, #track, #tracks, #trs, #u, #ul, #uls, #us, #var, #vars, #video, #videos, #wbr, #wbrs

Methods included from Atoms

load

Methods included from XpathSupport

escape

Constructor Details

#initialize(browser = :firefox, *args) ⇒ Browser

Create a Watir::Browser instance

Parameters:

  • browser (:firefox, :ie, :chrome, :remote, Selenium::WebDriver) (defaults to: :firefox)
  • args

    Passed to the underlying driver



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/watir-webdriver/browser.rb', line 32

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

  @error_checkers = []
  @current_frame  = nil
  @closed         = false
end

Instance Attribute Details

#driverObject (readonly) Also known as: wd

Returns the value of attribute driver.



13
14
15
# File 'lib/watir-webdriver/browser.rb', line 13

def driver
  @driver
end

Class Method Details

.start(url, browser = :firefox) ⇒ Object



17
18
19
20
21
22
# File 'lib/watir-webdriver/browser.rb', line 17

def start(url, browser = :firefox)
  b = new(browser)
  b.goto url

  b
end

Instance Method Details

#add_checker(checker = nil, &block) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/watir-webdriver/browser.rb', line 138

def add_checker(checker = nil, &block)
  if block_given?
    @error_checkers << block
  elsif checker.respond_to? :call
    @error_checkers << checker
  else
    raise ArgumentError, "expected block or object responding to #call"
  end
end

#assert_existsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Protocol shared with Watir::Element



162
163
164
165
166
167
168
169
# File 'lib/watir-webdriver/browser.rb', line 162

def assert_exists
  if @closed
    raise Error, "browser was closed"
  else
    driver.switch_to.default_content
    true
  end
end

#backObject



67
68
69
# File 'lib/watir-webdriver/browser.rb', line 67

def back
  @driver.navigate.back
end

#browserObject



180
181
182
# File 'lib/watir-webdriver/browser.rb', line 180

def browser
  self
end

#clear_cookiesObject



90
91
92
93
# File 'lib/watir-webdriver/browser.rb', line 90

def clear_cookies
  warn 'Browser#clear_cookies is deprecated and replaced by Browser#cookies (i.e. browser.cookies.clear)'
  @driver.manage.delete_all_cookies
end

#closeObject Also known as: quit



83
84
85
86
87
# File 'lib/watir-webdriver/browser.rb', line 83

def close
  return if @closed
  @driver.quit
  @closed = true
end

#cookiesObject



95
96
97
# File 'lib/watir-webdriver/browser.rb', line 95

def cookies
  @cookies ||= Cookies.new driver.manage
end

#disable_checker(checker) ⇒ Object



148
149
150
# File 'lib/watir-webdriver/browser.rb', line 148

def disable_checker(checker)
  @error_checkers.delete(checker)
end

#execute_script(script, *args) ⇒ Object



127
128
129
130
131
132
# File 'lib/watir-webdriver/browser.rb', line 127

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?Boolean Also known as: exists?

Returns:

  • (Boolean)


171
172
173
# File 'lib/watir-webdriver/browser.rb', line 171

def exist?
  not @closed
end

#forwardObject



71
72
73
# File 'lib/watir-webdriver/browser.rb', line 71

def forward
  @driver.navigate.forward
end

#goto(uri) ⇒ String

Goto the given URL

Parameters:

  • uri (String)

    The url.

Returns:

  • (String)

    The url you end up at.



58
59
60
61
62
63
64
65
# File 'lib/watir-webdriver/browser.rb', line 58

def goto(uri)
  uri = "http://#{uri}" unless uri =~ URI.regexp

  @driver.navigate.to uri
  run_checkers

  url
end

#htmlObject



103
104
105
106
# File 'lib/watir-webdriver/browser.rb', line 103

def html
  # use body.html instead?
  @driver.page_source
end

#inspectObject



47
48
49
# File 'lib/watir-webdriver/browser.rb', line 47

def inspect
  '#<%s:0x%x url=%s title=%s>' % [self.class, hash*2, url.inspect, title.inspect]
end

#ready_stateObject



119
120
121
# File 'lib/watir-webdriver/browser.rb', line 119

def ready_state
  execute_script 'return document.readyState'
end

#refreshObject



108
109
110
111
# File 'lib/watir-webdriver/browser.rb', line 108

def refresh
  @driver.navigate.refresh
  run_checkers
end

#reset!Object



176
177
178
# File 'lib/watir-webdriver/browser.rb', line 176

def reset!
  # no-op
end

#run_checkersObject



152
153
154
# File 'lib/watir-webdriver/browser.rb', line 152

def run_checkers
  @error_checkers.each { |e| e.call(self) }
end

#send_keys(*args) ⇒ Object



134
135
136
# File 'lib/watir-webdriver/browser.rb', line 134

def send_keys(*args)
  @driver.switch_to.active_element.send_keys(*args)
end

#statusObject



123
124
125
# File 'lib/watir-webdriver/browser.rb', line 123

def status
  execute_script "return window.status;"
end

#textObject



99
100
101
# File 'lib/watir-webdriver/browser.rb', line 99

def text
  @driver.find_element(:tag_name, "body").text
end

#titleObject



79
80
81
# File 'lib/watir-webdriver/browser.rb', line 79

def title
  @driver.title
end

#urlObject



75
76
77
# File 'lib/watir-webdriver/browser.rb', line 75

def url
  @driver.current_url
end

#wait(timeout = 5) ⇒ Object



113
114
115
116
117
# File 'lib/watir-webdriver/browser.rb', line 113

def wait(timeout = 5)
  wait_until(timeout, "waiting for document.readyState == 'complete'") do
    ready_state == "complete"
  end
end