Class: Terminus::Browser
- Inherits:
-
Object
- Object
- Terminus::Browser
- Extended by:
- Forwardable
- Includes:
- Timeouts
- Defined in:
- lib/terminus/browser.rb
Constant Summary
Constants included from Timeouts
Instance Attribute Summary collapse
-
#connector ⇒ Object
readonly
Returns the value of attribute connector.
-
#sockets ⇒ Object
writeonly
Sets the attribute sockets.
Instance Method Summary collapse
- #===(params) ⇒ Object
- #ask(command, retries = RETRY_LIMIT) ⇒ Object
- #current_path ⇒ Object
- #current_url ⇒ Object
- #debug(*args) ⇒ Object
- #debugger ⇒ Object
- #docked? ⇒ Boolean
- #evaluate_script(expression) ⇒ Object
- #execute_script(expression) ⇒ Object
- #find(xpath, driver = nil) ⇒ Object
- #frame!(frame_browser) ⇒ Object
- #frames ⇒ Object
- #html ⇒ Object (also: #body)
- #id ⇒ Object
- #infinite_redirect? ⇒ Boolean
-
#initialize(controller, id) ⇒ Browser
constructor
A new instance of Browser.
- #name ⇒ Object
- #page_id ⇒ Object
- #ping!(message) ⇒ Object
- #raw_url ⇒ Object
- #reset! ⇒ Object
- #response_headers ⇒ Object
- #result(id) ⇒ Object
- #result!(message) ⇒ Object
- #return_to_dock ⇒ Object
- #save_screenshot(path, options = {}) ⇒ Object
- #sockets? ⇒ Boolean
- #source ⇒ Object
- #status_code ⇒ Object
- #tell(command) ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #visit(url, retries = RETRY_LIMIT) ⇒ Object
- #wait_for_ping ⇒ Object
Methods included from Timeouts
Constructor Details
#initialize(controller, id) ⇒ Browser
Returns a new instance of Browser.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/terminus/browser.rb', line 11 def initialize(controller, id) @controller = controller @attributes = {'id' => id} @docked = false @frames = Set.new @namespace = Faye::Namespace.new @results = {} add_timeout(:dead, Timeouts::TIMEOUT) { drop_dead! } end |
Instance Attribute Details
#connector ⇒ Object (readonly)
Returns the value of attribute connector.
5 6 7 |
# File 'lib/terminus/browser.rb', line 5 def connector @connector end |
#sockets=(value) ⇒ Object (writeonly)
Sets the attribute sockets
6 7 8 |
# File 'lib/terminus/browser.rb', line 6 def sockets=(value) @sockets = value end |
Instance Method Details
#===(params) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/terminus/browser.rb', line 22 def ===(params) return docked? if params == :docked return params == id if String === params return false if @parent return false unless @user_agent params.all? do |name, value| property = __send__(name) value === property end end |
#ask(command, retries = RETRY_LIMIT) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/terminus/browser.rb', line 34 def ask(command, retries = RETRY_LIMIT) debug(:ask, id, command) value = if @connector = Yajl::Encoder.encode('commandId' => '_', 'command' => command) response = @connector.request() if response.nil? retries == false ? false : ask(command) else result_hash = Yajl::Parser.parse(response) result_hash['value'] end else command_id = tell(command) result_hash = wait_with_timeout(:result) { result(command_id) } result_hash[:value] end debug(:val, id, command, value) raise ObsoleteElementError if value.nil? value rescue Timeouts::TimeoutError => e raise e if retries == 1 ask(command, retries - 1) end |
#current_path ⇒ Object
58 59 60 |
# File 'lib/terminus/browser.rb', line 58 def current_path URI.parse(current_url).path end |
#current_url ⇒ Object
62 63 64 65 66 |
# File 'lib/terminus/browser.rb', line 62 def current_url url = @attributes['url'] return '' unless url rewrite_local(ask([:current_url])) end |
#debug(*args) ⇒ Object
68 69 70 |
# File 'lib/terminus/browser.rb', line 68 def debug(*args) p args if Terminus.debug end |
#debugger ⇒ Object
72 73 74 75 |
# File 'lib/terminus/browser.rb', line 72 def debugger Client::PhantomJS.debugger if name == 'PhantomJS' Readline.readline('Driver paused, press ENTER to continue') end |
#docked? ⇒ Boolean
77 78 79 |
# File 'lib/terminus/browser.rb', line 77 def docked? @docked end |
#evaluate_script(expression) ⇒ Object
81 82 83 |
# File 'lib/terminus/browser.rb', line 81 def evaluate_script(expression) ask([:evaluate, expression]) end |
#execute_script(expression) ⇒ Object
85 86 87 88 |
# File 'lib/terminus/browser.rb', line 85 def execute_script(expression) @connector ? ask([:execute, expression]) : tell([:execute, expression]) nil end |
#find(xpath, driver = nil) ⇒ Object
90 91 92 |
# File 'lib/terminus/browser.rb', line 90 def find(xpath, driver = nil) ask([:find, xpath, false]).map { |id| Node.new(self, id, driver) } end |
#frame!(frame_browser) ⇒ Object
94 95 96 |
# File 'lib/terminus/browser.rb', line 94 def frame!(frame_browser) @frames.add(frame_browser) end |
#frames ⇒ Object
98 99 100 |
# File 'lib/terminus/browser.rb', line 98 def frames @frames.to_a end |
#html ⇒ Object Also known as: body
102 103 104 |
# File 'lib/terminus/browser.rb', line 102 def html ask([:body]) end |
#id ⇒ Object
107 108 109 |
# File 'lib/terminus/browser.rb', line 107 def id @attributes['id'] end |
#infinite_redirect? ⇒ Boolean
111 112 113 114 |
# File 'lib/terminus/browser.rb', line 111 def infinite_redirect? return @infinite_redirect unless @connector evaluate_script('!!window.TERMINUS_INFINITE_REDIRECT') end |
#name ⇒ Object
116 117 118 119 |
# File 'lib/terminus/browser.rb', line 116 def name return 'PhantomJS' if @user_agent.to_str =~ /\bPhantomJS\b/ @user_agent.browser end |
#page_id ⇒ Object
121 122 123 |
# File 'lib/terminus/browser.rb', line 121 def page_id @attributes['page'] end |
#ping!(message) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/terminus/browser.rb', line 125 def ping!() debug(:ping, id) debug(:recv, ) remove_timeout(:dead) add_timeout(:dead, Timeouts::TIMEOUT) { drop_dead! } @attributes['raw_url'] = ['url'] ['url'] = rewrite_local(['url']) @attributes = @attributes.merge() @user_agent = UserAgent.parse(['ua']) detect_dock_host @infinite_redirect = ['infinite'] if id =~ /\// @parent = Terminus.browser(id.gsub(/\/[^\/]+$/, '')) @parent.frame!(self) unless @parent == self end start_connector if ['sockets'] and sockets? @ping = true end |
#raw_url ⇒ Object
151 152 153 |
# File 'lib/terminus/browser.rb', line 151 def raw_url @attributes['raw_url'] end |
#reset! ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/terminus/browser.rb', line 155 def reset! if url = @attributes['url'] uri = URI.parse(url) visit("http://#{uri.host}:#{uri.port}/") end ask([:clear_cookies]) @attributes.delete('url') end |
#response_headers ⇒ Object
165 166 167 |
# File 'lib/terminus/browser.rb', line 165 def response_headers Headers.new(evaluate_script('TERMINUS_HEADERS')) end |
#result(id) ⇒ Object
174 175 176 177 |
# File 'lib/terminus/browser.rb', line 174 def result(id) return nil unless @results.has_key?(id) @results.delete(id) end |
#result!(message) ⇒ Object
169 170 171 172 |
# File 'lib/terminus/browser.rb', line 169 def result!() debug(:result, id, ['commandId'], ['result']) @results[['commandId']] = {:value => ['result']} end |
#return_to_dock ⇒ Object
179 180 181 182 |
# File 'lib/terminus/browser.rb', line 179 def return_to_dock return unless @dock_host visit("http://#{@dock_host}:#{Terminus.port}/") end |
#save_screenshot(path, options = {}) ⇒ Object
184 185 186 187 |
# File 'lib/terminus/browser.rb', line 184 def save_screenshot(path, = {}) raise Capybara::NotSupportedByDriverError.new unless name == 'PhantomJS' Client::PhantomJS.save_screenshot(path, ) end |
#sockets? ⇒ Boolean
189 190 191 |
# File 'lib/terminus/browser.rb', line 189 def sockets? @sockets.nil? ? Terminus.sockets != false : @sockets end |
#source ⇒ Object
193 194 195 |
# File 'lib/terminus/browser.rb', line 193 def source evaluate_script('TERMINUS_SOURCE') end |
#status_code ⇒ Object
197 198 199 |
# File 'lib/terminus/browser.rb', line 197 def status_code evaluate_script('TERMINUS_STATUS') end |
#tell(command) ⇒ Object
201 202 203 204 205 206 |
# File 'lib/terminus/browser.rb', line 201 def tell(command) command_id = @namespace.generate debug(:tell, id, command, command_id) messenger.publish(command_channel, 'command' => command, 'commandId' => command_id) command_id end |
#to_s ⇒ Object Also known as: inspect
238 239 240 |
# File 'lib/terminus/browser.rb', line 238 def to_s "<#{self.class.name} #{name} #{version} (#{os})>" end |
#visit(url, retries = RETRY_LIMIT) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/terminus/browser.rb', line 208 def visit(url, retries = RETRY_LIMIT) close_frames! uri = @controller.rewrite_remote(url, @dock_host) uri.host = @dock_host if uri.host =~ LOCALHOST @controller.visit_url(uri.to_s) if @connector ask([:visit, uri.to_s], false) @connector.drain_socket @attributes['url'] = rewrite_local(uri) else tell([:visit, uri.to_s]) wait_for_ping end if infinite_redirect? @infinite_redirect = nil raise Capybara::InfiniteRedirectError end rescue Timeouts::TimeoutError => e raise e if retries.zero? visit(url, retries - 1) end |
#wait_for_ping ⇒ Object
233 234 235 236 |
# File 'lib/terminus/browser.rb', line 233 def wait_for_ping @ping = false wait_with_timeout(:ping) { @ping or @dead } end |