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

#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, #dialog, #dialogs, #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



152
153
154
155
156
157
158
159
160
# File 'lib/watir-webdriver/browser.rb', line 152

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

#alertObject



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

def alert
  Alert.new driver.switch_to
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



176
177
178
179
180
181
182
183
# File 'lib/watir-webdriver/browser.rb', line 176

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

#backObject



69
70
71
# File 'lib/watir-webdriver/browser.rb', line 69

def back
  @driver.navigate.back
end

#browserObject



194
195
196
# File 'lib/watir-webdriver/browser.rb', line 194

def browser
  self
end

#closeObject Also known as: quit



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

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

#cookiesObject



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

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

#disable_checker(checker) ⇒ Object



162
163
164
# File 'lib/watir-webdriver/browser.rb', line 162

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

#execute_script(script, *args) ⇒ Object



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

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)


185
186
187
# File 'lib/watir-webdriver/browser.rb', line 185

def exist?
  not @closed
end

#forwardObject



73
74
75
# File 'lib/watir-webdriver/browser.rb', line 73

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.



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

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

  @driver.navigate.to uri
  run_checkers

  url
end

#htmlObject



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

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

#inspectObject



47
48
49
50
51
# 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]
rescue
  '#<%s:0x%x closed=%s>' % [self.class, hash*2, @closed.to_s]
end

#ready_stateObject



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

def ready_state
  execute_script 'return document.readyState'
end

#refreshObject



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

def refresh
  @driver.navigate.refresh
  run_checkers
end

#reset!Object



190
191
192
# File 'lib/watir-webdriver/browser.rb', line 190

def reset!
  # no-op
end

#run_checkersObject



166
167
168
# File 'lib/watir-webdriver/browser.rb', line 166

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

#screenshotWatir::Screenshot

Handles screenshot of current pages.

Examples:

browser.screenshot.save("screenshot.png")

Returns:



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

def screenshot
  Screenshot.new driver
end

#send_keys(*args) ⇒ Object



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

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

#statusObject



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

def status
  execute_script "return window.status;"
end

#textObject



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

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

#titleObject



81
82
83
# File 'lib/watir-webdriver/browser.rb', line 81

def title
  @driver.title
end

#urlObject



77
78
79
# File 'lib/watir-webdriver/browser.rb', line 77

def url
  @driver.current_url
end

#wait(timeout = 5) ⇒ Object



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

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