Class: Watir::Browser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Container

add

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



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/watir-webdriver/browser.rb', line 25

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
end

Instance Attribute Details

#driverObject (readonly) Also known as: wd

Returns the value of attribute driver.



6
7
8
# File 'lib/watir-webdriver/browser.rb', line 6

def driver
  @driver
end

Class Method Details

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



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

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

  b
end

Instance Method Details

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



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

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 BaseElement



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

def assert_exists
  driver.switch_to.default_content
  true
end

#backObject



59
60
61
# File 'lib/watir-webdriver/browser.rb', line 59

def back
  @driver.navigate.back
end

#browserObject



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

def browser
  self
end

#clear_cookiesObject



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

def clear_cookies
  @driver.manage.delete_all_cookies
end

#closeObject Also known as: quit



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

def close
  @driver.quit
end

#disable_checker(checker) ⇒ Object



122
123
124
# File 'lib/watir-webdriver/browser.rb', line 122

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

#execute_script(script, *args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/watir-webdriver/browser.rb', line 101

def execute_script(script, *args)
  args.map! { |e| e.kind_of?(Watir::BaseElement) ? 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)


141
142
143
# File 'lib/watir-webdriver/browser.rb', line 141

def exist?
  true
end

#forwardObject



63
64
65
# File 'lib/watir-webdriver/browser.rb', line 63

def forward
  @driver.navigate.forward
end

#goto(uri) ⇒ String

Goto the given URL

Parameters:

Returns:

  • (String)

    The url you end up at.



50
51
52
53
54
55
56
57
# File 'lib/watir-webdriver/browser.rb', line 50

def goto(uri)
  uri = "http://#{uri}" unless uri.include?("://") || uri =~ /^\w+:/

  @driver.navigate.to uri
  run_checkers

  url
end

#htmlObject



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

def html
  @driver.page_source
end

#inspectObject



39
40
41
# File 'lib/watir-webdriver/browser.rb', line 39

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

#refreshObject



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

def refresh
  @driver.navigate.refresh
  run_checkers
end

#run_checkersObject



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

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

#statusObject



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

def status
  execute_script "return window.status;"
end

#textObject



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

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

#titleObject



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

def title
  @driver.title
end

#urlObject



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

def url
  @driver.current_url
end