Class: Watir::Browser

Inherits:
Object show all
Includes:
Container, WindowSwitching
Defined in:
lib/watir-webdriver/browser.rb,
lib/watir-webdriver/extensions/nokogiri.rb

Overview

The main class through which you control the browser.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WindowSwitching

#window, #windows

Methods included from Container

#a, #abbr, #abbrs, #address, #addresses, #area, #areas, #article, #articles, #as, #aside, #asides, #audio, #audios, #b, #base, #bases, #bdo, #bdos, #blockquote, #blockquotes, #body, #bodys, #br, #brs, #bs, #button, #buttons, #canvas, #canvases, #caption, #captions, #cell, #cells, #checkbox, #checkboxes, #cite, #cites, #code, #codes, #col, #colgroup, #colgroups, #cols, #command, #commands, #datalist, #datalists, #dd, #dds, #del, #dels, #details, #dfn, #dfns, #div, #divs, #dl, #dls, #dt, #dts, #em, #embed, #embeds, #ems, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #form, #forms, #frame, #frames, #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, #row, #rows, #rp, #rps, #rt, #rts, #rubies, #ruby, #samp, #samps, #script, #scripts, #section, #sections, #select, #select_list, #select_lists, #selects, #small, #smalls, #source, #sources, #span, #spans, #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, #ul, #uls, #var, #vars, #video, #videos, #wbr, #wbrs

Methods included from XpathSupport

#element_by_xpath, #elements_by_xpath

Constructor Details

#initialize(browser, *args) ⇒ Browser

Create a Watir::Browser instance

Parameters:

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

    Passed to the underlying driver



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

def initialize(browser, *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



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

def driver
  @driver
end

Class Method Details

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



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

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

  b
end

Instance Method Details

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



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

def add_checker(checker = nil, &block)
  if block_given?
    @error_checkers << block
  elsif Proc === checker
    @error_checkers << checker
  else
    raise ArgumentError, "argument must be a Proc or block"
  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



146
147
148
149
150
151
152
153
# File 'lib/watir-webdriver/browser.rb', line 146

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

#backObject



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

def back
  @driver.navigate.back
end

#browserObject



159
160
161
# File 'lib/watir-webdriver/browser.rb', line 159

def browser
  self
end

#clear_cookiesObject



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

def clear_cookies
  @driver.manage.delete_all_cookies
end

#closeObject Also known as: quit



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

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

#disable_checker(checker) ⇒ Object



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

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

#execute_script(script, *args) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/watir-webdriver/browser.rb', line 111

def execute_script(script, *args)
  args.map! { |e| e.kind_of?(Watir::Element) ? e.element : e }
  returned = @driver.execute_script(script, *args)

  if returned.kind_of? WebDriver::Element
    Watir.element_class_for(returned.tag_name).new(self, :element => returned)
  else
    returned
  end
end

#exist?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/watir-webdriver/browser.rb', line 155

def exist?
  not @closed
end

#forwardObject



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

def forward
  @driver.navigate.forward
end

#goto(uri) ⇒ String

Goto the given URL

Parameters:

Returns:

  • (String)

    The url you end up at.



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

def goto(uri)
  uri = "http://#{uri}" unless uri.include?("://")

  @driver.navigate.to uri
  run_checkers

  url
end

#htmlObject



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

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

#inspectObject



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

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

#refreshObject



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

def refresh
  @driver.navigate.refresh
  run_checkers
end

#run_checkersObject



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

def run_checkers
  @error_checkers.each { |e| e[self] }
end

#statusObject



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

def status
  execute_script "return window.status;"
end

#textObject



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

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

#titleObject



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

def title
  @driver.title
end

#urlObject



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

def url
  @driver.current_url
end