Class: Capybara::Cuprite::Browser::Page

Inherits:
Object
  • Object
show all
Includes:
DOM, Frame, Input, Net, Runtime
Defined in:
lib/capybara/cuprite/browser/page.rb

Constant Summary

Constants included from Input

Input::KEYS, Input::KEYS_MAPPING, Input::MODIFIERS

Constants included from Runtime

Runtime::DEFAULT_OPTIONS, Runtime::EVALUATE_ASYNC_OPTIONS, Runtime::EXECUTE_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Input

#click, #click_coordinates, #combine_strings, #double_click, #drag, #drag_by, #hover, #normalize_keys, #right_click, #scroll_to, #select, #send_keys, #set, #trigger

Methods included from DOM

#all_text, #attribute, #attributes, #body, #current_url, #disabled?, #path, #property, #title, #value, #visible?

Methods included from Runtime

#evaluate, #evaluate_async, #evaluate_in, #evaluate_on, #execute

Methods included from Frame

#execution_context_id, #frame_name, #frame_title, #frame_url, #switch_to_frame

Methods included from Net

#authorize, #continue_request, #intercept_request, #proxy_authorize

Constructor Details

#initialize(target_id, browser) ⇒ Page

Returns a new instance of Page.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capybara/cuprite/browser/page.rb', line 38

def initialize(target_id, browser)
  @wait = 0
  @target_id, @browser = target_id, browser
  @mutex, @resource = Mutex.new, ConditionVariable.new
  @network_traffic = []

  @frames = {}
  @waiting_frames ||= Set.new
  @frame_stack = []
  @accept_modal = []
  @modal_messages = []

  begin
    @session_id = @browser.command("Target.attachToTarget", targetId: @target_id)["sessionId"]
  rescue BrowserError => e
    if e.message == "No target with given id found"
      raise NoSuchWindowError
    else
      raise
    end
  end

  host = @browser.process.host
  port = @browser.process.port
  ws_url = "ws://#{host}:#{port}/devtools/page/#{@target_id}"
  @client = Client.new(browser, ws_url)

  subscribe_events
  prepare_page
end

Instance Attribute Details

#referrerObject

Returns the value of attribute referrer.



35
36
37
# File 'lib/capybara/cuprite/browser/page.rb', line 35

def referrer
  @referrer
end

#response_headersObject (readonly)

Returns the value of attribute response_headers.



36
37
38
# File 'lib/capybara/cuprite/browser/page.rb', line 36

def response_headers
  @response_headers
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



36
37
38
# File 'lib/capybara/cuprite/browser/page.rb', line 36

def status_code
  @status_code
end

#target_idObject (readonly)

Returns the value of attribute target_id.



36
37
38
# File 'lib/capybara/cuprite/browser/page.rb', line 36

def target_id
  @target_id
end

Instance Method Details

#accept_confirmObject



139
140
141
# File 'lib/capybara/cuprite/browser/page.rb', line 139

def accept_confirm
  @accept_modal << true
end

#accept_prompt(modal_response) ⇒ Object



147
148
149
150
# File 'lib/capybara/cuprite/browser/page.rb', line 147

def accept_prompt(modal_response)
  @accept_modal << true
  @modal_response = modal_response
end

#clear_network_trafficObject



127
128
129
# File 'lib/capybara/cuprite/browser/page.rb', line 127

def clear_network_traffic
  @network_traffic = []
end

#closeObject



88
89
90
91
92
# File 'lib/capybara/cuprite/browser/page.rb', line 88

def close
  @browser.command("Target.detachFromTarget", sessionId: @session_id)
  @browser.command("Target.closeTarget", targetId: @target_id)
  close_connection
end

#close_connectionObject



94
95
96
# File 'lib/capybara/cuprite/browser/page.rb', line 94

def close_connection
  @client.close
end

#command(*args) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/capybara/cuprite/browser/page.rb', line 182

def command(*args)
  id = nil

  @mutex.synchronize do
    id = @client.command(*args)
    stop_at = Capybara::Helpers.monotonic_time + @wait

    while @wait > 0 && (remain = stop_at - Capybara::Helpers.monotonic_time) > 0
      @resource.wait(@mutex, remain)
    end

    @wait = 0
  end

  @client.wait(id: id)
end

#dismiss_confirmObject



143
144
145
# File 'lib/capybara/cuprite/browser/page.rb', line 143

def dismiss_confirm
  @accept_modal << false
end

#dismiss_promptObject



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

def dismiss_prompt
  @accept_modal << false
end

#find_modal(options) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/capybara/cuprite/browser/page.rb', line 156

def find_modal(options)
  start_time    = Capybara::Helpers.monotonic_time
  timeout_sec   = options.fetch(:wait) { session_wait_time }
  expect_text   = options[:text]
  expect_regexp = expect_text.is_a?(Regexp) ? expect_text : Regexp.escape(expect_text.to_s)
  not_found_msg = "Unable to find modal dialog"
  not_found_msg += " with #{expect_text}" if expect_text

  begin
    modal_text = @modal_messages.shift
    raise Capybara::ModalNotFound if modal_text.nil? || (expect_text && !modal_text.match(expect_regexp))
  rescue Capybara::ModalNotFound => e
    raise e, not_found_msg if (Capybara::Helpers.monotonic_time - start_time) >= timeout_sec
    sleep(0.05)
    retry
  end

  modal_text
end

#go_backObject



131
132
133
# File 'lib/capybara/cuprite/browser/page.rb', line 131

def go_back
  go(-1)
end

#go_forwardObject



135
136
137
# File 'lib/capybara/cuprite/browser/page.rb', line 135

def go_forward
  go(1)
end

#network_traffic(type = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/capybara/cuprite/browser/page.rb', line 116

def network_traffic(type = nil)
  case type.to_s
  when "all"
    @network_traffic
  when "blocked"
    @network_traffic.select { |r| r.response.nil? } # when request blocked
  else
    @network_traffic.select { |r| r.response } # when request isn't blocked
  end
end

#refreshObject



111
112
113
114
# File 'lib/capybara/cuprite/browser/page.rb', line 111

def refresh
  @wait = timeout
  command("Page.reload")
end

#reset_modalsObject



176
177
178
179
180
# File 'lib/capybara/cuprite/browser/page.rb', line 176

def reset_modals
  @accept_modal = []
  @modal_response = nil
  @modal_messages = []
end

#resize(width: nil, height: nil, fullscreen: false) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/capybara/cuprite/browser/page.rb', line 98

def resize(width: nil, height: nil, fullscreen: false)
  result = @browser.command("Browser.getWindowForTarget", targetId: @target_id)
  @window_id, @bounds = result.values_at("windowId", "bounds")

  if fullscreen
    @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "fullscreen" })
  else
    @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "normal" })
    @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { width: width, height: height, windowState: "normal" })
    command("Emulation.setDeviceMetricsOverride", width: width, height: height, deviceScaleFactor: 1, mobile: false)
  end
end

#timeoutObject



69
70
71
# File 'lib/capybara/cuprite/browser/page.rb', line 69

def timeout
  @browser.timeout
end

#visit(url) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/capybara/cuprite/browser/page.rb', line 73

def visit(url)
  @wait = timeout
  options = { url: url }
  options.merge!(referrer: referrer) if referrer
  response = command("Page.navigate", **options)
  # https://cs.chromium.org/chromium/src/net/base/net_error_list.h
  if %w[net::ERR_NAME_NOT_RESOLVED
        net::ERR_NAME_RESOLUTION_FAILED
        net::ERR_INTERNET_DISCONNECTED
        net::ERR_CONNECTION_TIMED_OUT].include?(response["errorText"])
    raise StatusFailError, "url" => url
  end
  response["frameId"]
end