Skip to content

Commit 312970d

Browse files
committed
Rescue error on disposeBrowserContext when context is missing
1 parent 180f292 commit 312970d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/ferrum/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def raise_browser_error(error)
165165
"Inspected target navigated or closed"
166166
raise NodeNotFoundError, error
167167
# Context is lost, page is reloading
168-
when "Cannot find context with specified id"
168+
when "Cannot find context with specified id", /Failed to find context with id/
169169
raise NoExecutionContextError, error
170170
when "No target with given id found"
171171
raise NoSuchPageError

lib/ferrum/contexts.rb

Lines changed: 10 additions & 2 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

@@ -49,8 +50,15 @@ def create(**options)
4950
def dispose(context_id)
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)