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

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

- (Browser) initialize(browser = :firefox, *args)

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

- (Object) driver (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

+ (Object) start(url, browser = :firefox)



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

- (Object) add_checker(checker = nil, &block)



135
136
137
138
139
140
141
142
143
# File 'lib/watir-webdriver/browser.rb', line 135

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

- (Object) assert_exists

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



159
160
161
162
163
164
165
166
# File 'lib/watir-webdriver/browser.rb', line 159

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

- (Object) back



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

def back
  @driver.navigate.back
end

- (Object) browser



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

def browser
  self
end

- (Object) close 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

- (Object) cookies



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

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

- (Object) disable_checker(checker)



145
146
147
# File 'lib/watir-webdriver/browser.rb', line 145

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

- (Object) execute_script(script, *args)



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

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

- (Boolean) exist? Also known as: exists?

Returns:

  • (Boolean)


168
169
170
# File 'lib/watir-webdriver/browser.rb', line 168

def exist?
  not @closed
end

- (Object) forward



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

def forward
  @driver.navigate.forward
end

- (String) goto(uri)

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

- (Object) html



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

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

- (Object) inspect



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

- (Object) ready_state



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

def ready_state
  execute_script 'return document.readyState'
end

- (Object) refresh



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

def refresh
  @driver.navigate.refresh
  run_checkers
end

- (Object) reset!



173
174
175
# File 'lib/watir-webdriver/browser.rb', line 173

def reset!
  # no-op
end

- (Object) run_checkers



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

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

- (Object) send_keys(*args)



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

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

- (Object) status



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

def status
  execute_script "return window.status;"
end

- (Object) text



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

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

- (Object) title



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

def title
  @driver.title
end

- (Object) url



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

def url
  @driver.current_url
end

- (Object) wait(timeout = 5)



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

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