Skip to content

Commit bc4d0ef

Browse files
committed
port fix from rubycdp#545
1 parent 9d3394b commit bc4d0ef

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/ferrum/client.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def event_name(event)
6161

6262
class Client
6363
extend Forwardable
64+
6465
delegate %i[timeout timeout=] => :options
6566

6667
attr_reader :ws_url, :options, :subscriber
@@ -165,7 +166,7 @@ def raise_browser_error(error)
165166
"Inspected target navigated or closed"
166167
raise NodeNotFoundError, error
167168
# Context is lost, page is reloading
168-
when "Cannot find context with specified id"
169+
when "Cannot find context with specified id", /Failed to find context with id/
169170
raise NoExecutionContextError, error
170171
when "No target with given id found"
171172
raise NoSuchPageError

lib/ferrum/contexts.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module Ferrum
66
class Contexts
77
ALLOWED_TARGET_TYPES = %w[page iframe].freeze
8+
ABOUT_BLANK = "about:blank"
89

910
include Enumerable
1011

@@ -46,11 +47,18 @@ def create(**options)
4647
context
4748
end
4849

49-
def dispose(context_id)
50+
def dispose(context_id) # rubocop:disable Naming/PredicateMethod
5051
context = @contexts[context_id]
5152
context.close_targets_connection
52-
@client.command("Target.disposeBrowserContext", browserContextId: context.id)
53-
@contexts.delete(context_id)
53+
54+
begin
55+
@client.command("Target.disposeBrowserContext", browserContextId: context.id)
56+
rescue NoExecutionContextError
57+
warn "Browser context #{context.id} was already disposed" unless context.page&.url == ABOUT_BLANK
58+
ensure
59+
@contexts.delete(context_id)
60+
end
61+
5462
true
5563
end
5664

0 commit comments

Comments
 (0)