Class: Capybara::Apparition::Browser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Auth, Cookie, Frame, Header, Modal, Render, Window
Defined in:
lib/capybara/apparition/browser.rb,
lib/capybara/apparition/browser/auth.rb,
lib/capybara/apparition/browser/frame.rb,
lib/capybara/apparition/browser/modal.rb,
lib/capybara/apparition/browser/cookie.rb,
lib/capybara/apparition/browser/header.rb,
lib/capybara/apparition/browser/render.rb,
lib/capybara/apparition/browser/window.rb,
lib/capybara/apparition/driver/launcher.rb

Defined Under Namespace

Modules: Auth, Cookie, Frame, Header, Modal, Render, Window Classes: Launcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Auth

#set_http_auth, #set_proxy_auth

Methods included from Frame

#switch_to_frame

Methods included from Modal

#accept_alert, #accept_confirm, #accept_prompt, #dismiss_confirm, #dismiss_prompt, #modal_message

Methods included from Cookie

#all_cookies, #clear_cookies, #cookies, #cookies_enabled=, #get_raw_cookies, #remove_cookie, #set_cookie

Methods included from Render

#paper_size=, #render, #render_base64

Methods included from Window

#close_window, #open_new_window, #switch_to_window, #window_handle, #window_handles, #within_window

Methods included from Header

#add_header, #add_headers, #headers, #headers=

Constructor Details

#initialize(client, logger = nil) ⇒ Browser

Returns a new instance of Browser.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/capybara/apparition/browser.rb', line 30

def initialize(client, logger = nil)
  @client = client
  @current_page_handle = nil
  @targets = Capybara::Apparition::DevToolsProtocol::TargetManager.new(self)
  @context_id = nil
  @js_errors = true
  @ignore_https_errors = false
  @logger = logger
  @console = Console.new(logger)
  @proxy_auth = nil

  initialize_handlers

  command('Target.setDiscoverTargets', discover: true)
  while @current_page_handle.nil?
    puts 'waiting for target...'
    sleep 0.1
  end
  @context_id = current_target.context_id
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



19
20
21
# File 'lib/capybara/apparition/browser.rb', line 19

def client
  @client
end

#consoleObject (readonly)

Returns the value of attribute console.



19
20
21
# File 'lib/capybara/apparition/browser.rb', line 19

def console
  @console
end

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



146
147
148
# File 'lib/capybara/apparition/browser.rb', line 146

def debug=(value)
  @debug = value
end

#ignore_https_errorsObject

Returns the value of attribute ignore_https_errors.



125
126
127
# File 'lib/capybara/apparition/browser.rb', line 125

def ignore_https_errors
  @ignore_https_errors
end

#js_errorsObject

Returns the value of attribute js_errors.



125
126
127
# File 'lib/capybara/apparition/browser.rb', line 125

def js_errors
  @js_errors
end

#paper_sizeObject (readonly)

Returns the value of attribute paper_size.



19
20
21
# File 'lib/capybara/apparition/browser.rb', line 19

def paper_size
  @paper_size
end

#proxy_authObject (readonly)

Returns the value of attribute proxy_auth.



19
20
21
# File 'lib/capybara/apparition/browser.rb', line 19

def proxy_auth
  @proxy_auth
end

#zoom_factorObject (readonly)

Returns the value of attribute zoom_factor.



19
20
21
# File 'lib/capybara/apparition/browser.rb', line 19

def zoom_factor
  @zoom_factor
end

Instance Method Details

#bodyObject



62
63
64
# File 'lib/capybara/apparition/browser.rb', line 62

def body
  current_page.content
end

#clear_memory_cacheObject



148
149
150
# File 'lib/capybara/apparition/browser.rb', line 148

def clear_memory_cache
  current_page.command('Network.clearBrowserCache')
end

#click_coordinates(x, y) ⇒ Object



71
72
73
# File 'lib/capybara/apparition/browser.rb', line 71

def click_coordinates(x, y)
  current_page.click_at(x, y)
end

#command(name, params = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/capybara/apparition/browser.rb', line 152

def command(name, params = {})
  result = client.send_cmd(name, params).result
  log result

  result || raise(Capybara::Apparition::ObsoleteNode.new(nil, nil))
rescue DeadClient
  restart
  raise
end

#command_for_session(session_id, name, params) ⇒ Object



162
163
164
165
166
167
# File 'lib/capybara/apparition/browser.rb', line 162

def command_for_session(session_id, name, params)
  client.send_cmd_to_session(session_id, name, params)
rescue DeadClient
  restart
  raise
end

#console_messages(type = nil) ⇒ Object



173
174
175
# File 'lib/capybara/apparition/browser.rb', line 173

def console_messages(type = nil)
  console.messages(type)
end

#current_pageObject



169
170
171
# File 'lib/capybara/apparition/browser.rb', line 169

def current_page
  current_target.page
end

#extensions=(filenames) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/capybara/apparition/browser.rb', line 127

def extensions=(filenames)
  @extensions = filenames
  Array(filenames).each do |name|
    begin
      current_page.command('Page.addScriptToEvaluateOnNewDocument', source: File.read(name))
    rescue Errno::ENOENT
      raise ::Capybara::Apparition::BrowserError.new('name' => "Unable to load extension: #{name}", 'args' => nil)
    end
  end
end

#network_traffic(type = nil) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/capybara/apparition/browser.rb', line 116

def network_traffic(type = nil)
  case type
  when :blocked
    current_page.network_traffic.select(&:blocked?)
  else
    current_page.network_traffic
  end
end

#resetObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/capybara/apparition/browser.rb', line 83

def reset
  current_page_targets = @targets.of_type('page').values

  new_context_id = command('Target.createBrowserContext')['browserContextId']
  new_target_response = client.send_cmd('Target.createTarget', url: 'about:blank', browserContextId: new_context_id)

  current_page_targets.each do |target|
    begin
      client.send_cmd('Target.disposeBrowserContext', browserContextId: target.context_id).discard_result
    rescue WrongWorld
      puts 'Unknown browserContextId'
    end
    @targets.delete(target.id)
  end

  new_target_id = new_target_response['targetId']

  timer = Capybara::Helpers.timer(expire_in: 10)
  until @targets.get(new_target_id)&.page&.usable?
    if timer.expired?
      puts 'Timedout waiting for reset'
      raise TimeoutError.new('reset')
    end
    sleep 0.01
  end
  @current_page_handle = new_target_id
  true
end

#resize(width, height, screen: nil) ⇒ Object



112
113
114
# File 'lib/capybara/apparition/browser.rb', line 112

def resize(width, height, screen: nil)
  current_page.set_viewport width: width, height: height, screen: screen
end

#restartObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/capybara/apparition/browser.rb', line 51

def restart
  puts 'handle client restart'
  # client.restart

  self.debug = @debug if defined?(@debug)
  self.js_errors = @js_errors if defined?(@js_errors)
  self.zoom_factor = @zoom_factor if defined?(@zoom_factor)
  self.extensions = @extensions if @extensions
  current_page.clear_network_traffic
end

#sourceObject



66
67
68
69
# File 'lib/capybara/apparition/browser.rb', line 66

def source
  # Is this still useful?
  # command 'source'
end

#url_blacklist=(blacklist) ⇒ Object



142
143
144
# File 'lib/capybara/apparition/browser.rb', line 142

def url_blacklist=(blacklist)
  current_page&.url_blacklist = blacklist
end

#url_whitelist=(whitelist) ⇒ Object



138
139
140
# File 'lib/capybara/apparition/browser.rb', line 138

def url_whitelist=(whitelist)
  current_page&.url_whitelist = whitelist
end