Class: Watir::Browser
- Inherits:
-
Object
- Object
- Watir::Browser
- 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
Instance Attribute Summary (collapse)
-
- (Object) driver
(also: #wd)
readonly
Returns the value of attribute driver.
Class Method Summary (collapse)
-
+ (Watir::Browser) start(url, browser = :firefox, *args)
Creates a Watir::Browser instance and goes to URL.
Instance Method Summary (collapse)
-
- (Object) add_checker(checker = nil) {|| ... }
Adds new checker.
-
- (Watir::Alert) alert
Handles JavaScript alerts, confirms and prompts.
-
- (Object) assert_exists
private
Protocol shared with Watir::Element.
-
- (Object) back
Navigates back in history.
- - (Object) browser
-
- (Object) close
(also: #quit)
Closes browser.
-
- (Watir::Cookies) cookies
Handles cookies.
-
- (Object) disable_checker(checker)
Deletes checker.
-
- (Object) execute_script(script, *args)
Executes JavaScript snippet.
-
- (Boolean) exist?
(also: #exists?)
Returns true if browser is not closed and false otherwise.
-
- (Object) forward
Navigates forward in history.
-
- (String) goto(uri)
Goes to the given URL.
-
- (String) html
Returns HTML code of current page.
-
- (Browser) initialize(browser = :firefox, *args)
constructor
Creates a Watir::Browser instance.
- - (Object) inspect
-
- (String) name
Returns browser name.
-
- (String) ready_state
Returns readyState of document.
-
- (Object) refresh
Refreshes current page.
- - (Object) reset!
-
- (Object) run_checkers
Runs checkers.
-
- (Watir::Screenshot) screenshot
Handles screenshots of current pages.
-
- (Object) send_keys(*args)
Sends sequence of keystrokes to currently active element.
-
- (String) status
Returns the text of status bar.
-
- (String) text
Returns text of page body.
-
- (String) title
Returns title of current page.
-
- (String) url
Returns URL of current page.
-
- (Object) wait(timeout = 5)
Waits until readyState of document is complete.
Methods included from AlertHelper
Methods included from Waitable
Methods included from HasWindow
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, #datas, #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
Methods included from XpathSupport
Constructor Details
- (Browser) initialize(browser = :firefox, *args)
Creates a Watir::Browser instance.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 @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
+ (Watir::Browser) start(url, browser = :firefox, *args)
Creates a Watir::Browser instance and goes to URL.
28 29 30 31 32 33 |
# File 'lib/watir-webdriver/browser.rb', line 28 def start(url, browser = :firefox, *args) b = new(browser, *args) b.goto url b end |
Instance Method Details
- (Object) add_checker(checker = nil) {|| ... }
Adds new checker.
Checkers are generally used to ensure application under test does not encounter any error. They are automatically executed after following events:
1. Open URL
2. Refresh page
3. Click, double-click or right-click on element
309 310 311 312 313 314 315 316 317 |
# File 'lib/watir-webdriver/browser.rb', line 309 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 |
- (Watir::Alert) alert
Handles JavaScript alerts, confirms and prompts.
192 193 194 |
# File 'lib/watir-webdriver/browser.rb', line 192 def alert Alert.new driver.switch_to 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
362 363 364 365 366 367 368 369 |
# File 'lib/watir-webdriver/browser.rb', line 362 def assert_exists if @closed raise Exception::Error, "browser was closed" else driver.switch_to.default_content true end end |
- (Object) back
Navigates back in history.
87 88 89 |
# File 'lib/watir-webdriver/browser.rb', line 87 def back @driver.navigate.back end |
- (Object) browser
375 376 377 |
# File 'lib/watir-webdriver/browser.rb', line 375 def browser self end |
- (Object) close Also known as: quit
Closes browser.
133 134 135 136 137 |
# File 'lib/watir-webdriver/browser.rb', line 133 def close return if @closed @driver.quit @closed = true end |
- (Watir::Cookies) cookies
Handles cookies.
146 147 148 |
# File 'lib/watir-webdriver/browser.rb', line 146 def @cookies ||= Cookies.new driver.manage end |
- (Object) disable_checker(checker)
Deletes checker.
333 334 335 |
# File 'lib/watir-webdriver/browser.rb', line 333 def disable_checker(checker) @error_checkers.delete(checker) end |
- (Object) execute_script(script, *args)
Executes JavaScript snippet.
If you are going to use the value snippet returns, make sure to use `return` explicitly.
257 258 259 260 261 262 |
# File 'lib/watir-webdriver/browser.rb', line 257 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 true if browser is not closed and false otherwise.
351 352 353 |
# File 'lib/watir-webdriver/browser.rb', line 351 def exist? not @closed end |
- (Object) forward
Navigates forward in history.
95 96 97 |
# File 'lib/watir-webdriver/browser.rb', line 95 def forward @driver.navigate.forward end |
- (String) goto(uri)
Goes to the given URL.
74 75 76 77 78 79 80 81 |
# File 'lib/watir-webdriver/browser.rb', line 74 def goto(uri) uri = "http://#{uri}" unless uri =~ URI.regexp @driver.navigate.to uri run_checkers url end |
- (String) html
Returns HTML code of current page.
181 182 183 184 |
# File 'lib/watir-webdriver/browser.rb', line 181 def html # use body.html instead? @driver.page_source end |
- (Object) inspect
58 59 60 61 62 |
# 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 |
- (String) name
Returns browser name.
161 162 163 |
# File 'lib/watir-webdriver/browser.rb', line 161 def name @driver.browser end |
- (String) ready_state
Returns readyState of document.
224 225 226 |
# File 'lib/watir-webdriver/browser.rb', line 224 def ready_state execute_script 'return document.readyState' end |
- (Object) refresh
Refreshes current page.
200 201 202 203 |
# File 'lib/watir-webdriver/browser.rb', line 200 def refresh @driver.navigate.refresh run_checkers end |
- (Object) reset!
371 372 373 |
# File 'lib/watir-webdriver/browser.rb', line 371 def reset! # no-op end |
- (Object) run_checkers
Runs checkers.
341 342 343 |
# File 'lib/watir-webdriver/browser.rb', line 341 def run_checkers @error_checkers.each { |e| e.call(self) } end |
- (Watir::Screenshot) screenshot
Handles screenshots of current pages.
284 285 286 |
# File 'lib/watir-webdriver/browser.rb', line 284 def screenshot Screenshot.new driver end |
- (Object) send_keys(*args)
Sends sequence of keystrokes to currently active element.
274 275 276 |
# File 'lib/watir-webdriver/browser.rb', line 274 def send_keys(*args) @driver.switch_to.active_element.send_keys(*args) end |
- (String) status
Returns the text of status bar.
234 235 236 |
# File 'lib/watir-webdriver/browser.rb', line 234 def status execute_script "return window.status;" end |
- (String) text
Returns text of page body.
171 172 173 |
# File 'lib/watir-webdriver/browser.rb', line 171 def text @driver.find_element(:tag_name, "body").text end |
- (String) title
Returns title of current page.
125 126 127 |
# File 'lib/watir-webdriver/browser.rb', line 125 def title @driver.title end |
- (String) url
Returns URL of current page.
110 111 112 |
# File 'lib/watir-webdriver/browser.rb', line 110 def url @driver.current_url end |
- (Object) wait(timeout = 5)
Waits until readyState of document is complete.
212 213 214 215 216 |
# File 'lib/watir-webdriver/browser.rb', line 212 def wait(timeout = 5) wait_until(timeout, "waiting for document.readyState == 'complete'") do ready_state == "complete" end end |