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/browser/launcher.rb,
lib/capybara/apparition/browser/page_manager.rb,
lib/capybara/apparition/browser/launcher/local.rb,
lib/capybara/apparition/browser/launcher/remote.rb

Defined Under Namespace

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

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

#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, #current_window_handle, #open_new_window, #switch_to_window, #window_handles

Methods included from Header

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

Constructor Details

#initialize(client, logger = nil) {|_self| ... } ⇒ Browser

Returns a new instance of Browser.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(client, logger = nil)
  @client = client
  @current_page_handle = nil
  @pages = PageManager.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)
  yield self if block_given?
  reset
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#consoleObject (readonly)

Returns the value of attribute console.



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

def console
  @console
end

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



150
151
152
# File 'lib/capybara/apparition/browser.rb', line 150

def debug=(value)
  @debug = value
end

#ignore_https_errorsObject

Returns the value of attribute ignore_https_errors.



133
134
135
# File 'lib/capybara/apparition/browser.rb', line 133

def ignore_https_errors
  @ignore_https_errors
end

#js_errorsObject

Returns the value of attribute js_errors.



133
134
135
# File 'lib/capybara/apparition/browser.rb', line 133

def js_errors
  @js_errors
end

#paper_sizeObject (readonly)

Returns the value of attribute paper_size.



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

def paper_size
  @paper_size
end

#proxy_authObject (readonly)

Returns the value of attribute proxy_auth.



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

def proxy_auth
  @proxy_auth
end

#zoom_factorObject (readonly)

Returns the value of attribute zoom_factor.



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

def zoom_factor
  @zoom_factor
end

Instance Method Details

#bodyObject



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

def body
  current_page.content
end

#clear_memory_cacheObject



152
153
154
# File 'lib/capybara/apparition/browser.rb', line 152

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

#click_coordinates(x, y) ⇒ Object



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

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

#command(name, **params) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/capybara/apparition/browser.rb', line 156

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



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

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



183
184
185
# File 'lib/capybara/apparition/browser.rb', line 183

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

#current_page(allow_nil: false) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/capybara/apparition/browser.rb', line 173

def current_page(allow_nil: false)
  @pages[@current_page_handle] || begin
    puts "No current page: #{@current_page_handle} : #{caller}" if ENV['DEBUG']
    @current_page_handle = nil
    raise NoSuchWindowError unless allow_nil

    @current_page_handle
  end
end

#extensions=(filenames) ⇒ Object



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

def extensions=(filenames)
  @extensions = filenames
  Array(filenames).each do |name|
    current_page(allow_nil: true)&.add_extension(name)
  end
end

#network_traffic(type = nil) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/capybara/apparition/browser.rb', line 124

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

#refresh_pages(opener:) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/capybara/apparition/browser.rb', line 112

def refresh_pages(opener:)
  @pages.refresh(opener: opener,
                 ignore_https_errors: ignore_https_errors,
                 js_errors: js_errors,
                 url_blacklist: @url_blacklist,
                 url_whitelist: @url_whitelist)
end

#resetObject



82
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 82

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

  @pages.reset

  new_target_id = new_target_response['targetId']

  session_id = command('Target.attachToTarget', targetId: new_target_id)['sessionId']
  session = Capybara::Apparition::DevToolsProtocol::Session.new(self, client, session_id)

  @pages.create(new_target_id, session, new_context_id,
                ignore_https_errors: ignore_https_errors,
                js_errors: js_errors, extensions: @extensions,
                url_blacklist: @url_blacklist,
                url_whitelist: @url_whitelist).send(:main_frame).loaded!

  timer = Capybara::Helpers.timer(expire_in: 10)
  until @pages[new_target_id].usable?
    if timer.expired?
      puts 'Timedout waiting for reset'
      raise TimeoutError, 'reset'
    end
    sleep 0.01
  end
  console.clear
  @current_page_handle = new_target_id
  true
end

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



120
121
122
# File 'lib/capybara/apparition/browser.rb', line 120

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

#restartObject



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

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



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

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

#url_blacklist=(blacklist) ⇒ Object



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

def url_blacklist=(blacklist)
  @url_blacklist = @pages.blacklist = blacklist
end

#url_whitelist=(whitelist) ⇒ Object



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

def url_whitelist=(whitelist)
  @url_whitelist = @pages.whitelist = whitelist
end