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
Constants included from Atoms
Instance Attribute Summary collapse
-
#driver ⇒ Object
(also: #wd)
readonly
Returns the value of attribute driver.
Class Method Summary collapse
-
.start(url, browser = :firefox, *args) ⇒ Watir::Browser
Creates a Watir::Browser instance and goes to URL.
Instance Method Summary collapse
-
#add_checker(checker = nil) {|| ... } ⇒ Object
Adds new checker.
-
#alert ⇒ Watir::Alert
Handles JavaScript alerts, confirms and prompts.
-
#assert_exists ⇒ Object
(also: #ensure_not_stale)
private
Protocol shared with Watir::Element.
-
#back ⇒ Object
Navigates back in history.
- #browser ⇒ Object
-
#close ⇒ Object
(also: #quit)
Closes browser.
-
#cookies ⇒ Watir::Cookies
Handles cookies.
-
#disable_checker(checker) ⇒ Object
Deletes checker.
-
#execute_script(script, *args) ⇒ Object
Executes JavaScript snippet.
-
#exist? ⇒ Boolean
(also: #exists?)
Returns true if browser is not closed and false otherwise.
-
#forward ⇒ Object
Navigates forward in history.
-
#goto(uri) ⇒ String
Goes to the given URL.
-
#html ⇒ String
Returns HTML code of current page.
-
#initialize(browser = :firefox, *args) ⇒ Browser
constructor
Creates a Watir::Browser instance.
- #inspect ⇒ Object
-
#name ⇒ Symbol
Returns browser name.
-
#ready_state ⇒ String
Returns readyState of document.
-
#refresh ⇒ Object
Refreshes current page.
- #reset! ⇒ Object
-
#run_checkers ⇒ Object
Runs checkers.
-
#screenshot ⇒ Watir::Screenshot
Handles screenshots of current pages.
-
#send_keys(*args) ⇒ Object
Sends sequence of keystrokes to currently active element.
-
#status ⇒ String
Returns the text of status bar.
-
#text ⇒ String
Returns text of page body.
-
#title ⇒ String
Returns title of current page.
-
#url ⇒ String
Returns URL of current page.
-
#wait(timeout = 5) ⇒ Object
Waits until readyState of document is complete.
-
#without_checkers {|| ... } ⇒ Object
Executes a block without running error checkers.
Methods included from AlertHelper
Methods included from Waitable
Methods included from HasWindow
Methods included from Container
#a, #abbr, #abbrs, #address, #addresses, #animate, #animate_motion, #animate_motions, #animate_transform, #animate_transforms, #animates, #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, #circle, #circles, #cite, #cites, #code, #codes, #col, #colgroup, #colgroups, #cols, #cursor, #cursors, #data, #datalist, #datalists, #datas, #dd, #dds, #defs, #defss, #del, #dels, #desc, #descs, #details, #detailses, #dfn, #dfns, #dialog, #dialogs, #discard, #discards, #div, #divs, #dl, #dls, #dt, #dts, #element, #elements, #ellipse, #ellipses, #em, #embed, #embeds, #ems, #extract_selector, #field_set, #field_sets, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #foreign_object, #foreign_objects, #form, #forms, #frame, #frames, #frameset, #framesets, #g, #gs, #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, #line, #linear_gradient, #linear_gradients, #lines, #link, #links, #lis, #main, #mains, #map, #maps, #mark, #marker, #markers, #marks, #menu, #menuitem, #menuitems, #menus, #mesh_gradient, #mesh_gradients, #mesh_patch, #mesh_patches, #mesh_row, #mesh_rows, #meta, #metadata, #metadatas, #metas, #meter, #meters, #mpath, #mpaths, #nav, #navs, #noscript, #noscripts, #object, #objects, #ol, #ols, #optgroup, #optgroups, #option, #options, #output, #outputs, #p, #param, #params, #path, #paths, #pattern, #patterns, #polygon, #polygons, #polyline, #polylines, #pre, #pres, #progress, #progresses, #ps, #q, #qs, #radial_gradient, #radial_gradients, #radio, #radios, #rect, #rects, #rp, #rps, #rt, #rts, #rubies, #ruby, #s, #samp, #samps, #script, #scripts, #section, #sections, #select, #select_list, #select_lists, #selects, #set, #sets, #small, #smalls, #source, #sources, #span, #spans, #ss, #stop, #stops, #strong, #strongs, #style, #styles, #sub, #subs, #summaries, #summary, #sup, #sups, #svg, #svgs, #switch, #switches, #symbol, #symbols, #table, #tables, #tbody, #tbodys, #td, #tds, #template, #templates, #text_field, #text_fields, #text_path, #text_paths, #textarea, #textareas, #tfoot, #tfoots, #th, #thead, #theads, #ths, #time, #times, #titles, #tr, #track, #tracks, #trs, #tspan, #tspans, #u, #ul, #uls, #us, #use, #uses, #var, #vars, #video, #videos, #view, #views, #wbr, #wbrs
Methods included from Atoms
Methods included from XpathSupport
Constructor Details
#initialize(browser = :firefox, *args) ⇒ Browser
Creates a Watir::Browser instance.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/watir-webdriver/browser.rb', line 42 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
#driver ⇒ Object (readonly) Also known as: wd
Returns the value of attribute driver.
12 13 14 |
# File 'lib/watir-webdriver/browser.rb', line 12 def driver @driver end |
Class Method Details
.start(url, browser = :firefox, *args) ⇒ Watir::Browser
Creates a Watir::Browser instance and goes to URL.
27 28 29 30 31 32 |
# File 'lib/watir-webdriver/browser.rb', line 27 def start(url, browser = :firefox, *args) b = new(browser, *args) b.goto url b end |
Instance Method Details
#add_checker(checker = nil) {|| ... } ⇒ Object
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
304 305 306 307 308 309 310 311 312 |
# File 'lib/watir-webdriver/browser.rb', line 304 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 |
#alert ⇒ Watir::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 |
#assert_exists ⇒ Object Also known as: ensure_not_stale
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
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/watir-webdriver/browser.rb', line 379 def assert_exists if @closed raise Exception::Error, "browser was closed" elsif !window.present? raise Exception::NoMatchingWindowFoundException, "browser window was closed" else driver.switch_to.default_content true end end |
#back ⇒ Object
Navigates back in history.
86 87 88 |
# File 'lib/watir-webdriver/browser.rb', line 86 def back @driver.navigate.back end |
#browser ⇒ Object
395 396 397 |
# File 'lib/watir-webdriver/browser.rb', line 395 def browser self end |
#close ⇒ Object 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 |
#cookies ⇒ Watir::Cookies
Handles cookies.
146 147 148 |
# File 'lib/watir-webdriver/browser.rb', line 146 def @cookies ||= Cookies.new driver.manage end |
#disable_checker(checker) ⇒ Object
Deletes checker.
328 329 330 |
# File 'lib/watir-webdriver/browser.rb', line 328 def disable_checker(checker) @error_checkers.delete(checker) end |
#execute_script(script, *args) ⇒ Object
Executes JavaScript snippet.
If you are going to use the value snippet returns, make sure to use ‘return` explicitly.
252 253 254 255 256 257 |
# File 'lib/watir-webdriver/browser.rb', line 252 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 true if browser is not closed and false otherwise.
365 366 367 368 369 370 |
# File 'lib/watir-webdriver/browser.rb', line 365 def exist? assert_exists true rescue Exception::NoMatchingWindowFoundException, Exception::Error false end |
#forward ⇒ Object
Navigates forward in history.
94 95 96 |
# File 'lib/watir-webdriver/browser.rb', line 94 def forward @driver.navigate.forward end |
#goto(uri) ⇒ String
Goes to the given URL.
73 74 75 76 77 78 79 80 |
# File 'lib/watir-webdriver/browser.rb', line 73 def goto(uri) uri = "http://#{uri}" unless uri =~ URI.regexp @driver.navigate.to uri run_checkers url end |
#html ⇒ String
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 |
#inspect ⇒ Object
57 58 59 60 61 |
# File 'lib/watir-webdriver/browser.rb', line 57 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 |
#name ⇒ Symbol
Returns browser name.
161 162 163 |
# File 'lib/watir-webdriver/browser.rb', line 161 def name @driver.browser end |
#ready_state ⇒ String
Returns readyState of document.
224 225 226 |
# File 'lib/watir-webdriver/browser.rb', line 224 def ready_state execute_script 'return document.readyState' end |
#refresh ⇒ Object
Refreshes current page.
200 201 202 203 |
# File 'lib/watir-webdriver/browser.rb', line 200 def refresh @driver.navigate.refresh run_checkers end |
#reset! ⇒ Object
391 392 393 |
# File 'lib/watir-webdriver/browser.rb', line 391 def reset! # no-op end |
#run_checkers ⇒ Object
Runs checkers.
336 337 338 |
# File 'lib/watir-webdriver/browser.rb', line 336 def run_checkers @error_checkers.each { |e| e.call(self) } if !@error_checkers.empty? && window.present? end |
#screenshot ⇒ Watir::Screenshot
Handles screenshots of current pages.
279 280 281 |
# File 'lib/watir-webdriver/browser.rb', line 279 def screenshot Screenshot.new driver end |
#send_keys(*args) ⇒ Object
Sends sequence of keystrokes to currently active element.
269 270 271 |
# File 'lib/watir-webdriver/browser.rb', line 269 def send_keys(*args) @driver.switch_to.active_element.send_keys(*args) end |
#status ⇒ String
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 |
#text ⇒ String
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 |
#title ⇒ String
Returns title of current page.
125 126 127 |
# File 'lib/watir-webdriver/browser.rb', line 125 def title @driver.title end |
#url ⇒ String
Returns URL of current page.
109 110 111 112 |
# File 'lib/watir-webdriver/browser.rb', line 109 def url assert_exists @driver.current_url end |
#wait(timeout = 5) ⇒ Object
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 |
#without_checkers {|| ... } ⇒ Object
Executes a block without running error checkers.
351 352 353 354 355 356 357 |
# File 'lib/watir-webdriver/browser.rb', line 351 def without_checkers current_checkers = @error_checkers @error_checkers = [] yield(self) ensure @error_checkers = current_checkers end |