-
-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using default browser context #471
base: main
Are you sure you want to change the base?
Changes from all commits
30efe09
556ccad
f24136d
e821a08
9bfd0fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ class Contexts | |
def initialize(client) | ||
@contexts = Concurrent::Map.new | ||
@client = client | ||
@default_context = create_default_context if @client.options.use_default_context | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One side effect of setting this in the constructor is that any existing tabs/targets in the browser's default context will be available as targets (e.g. It also seems to me that doing this actually makes the |
||
subscribe | ||
auto_attach | ||
discover | ||
|
@@ -20,6 +21,20 @@ def default_context | |
@default_context ||= create | ||
end | ||
|
||
def create_default_context | ||
default_context_id = compute_default_context_id | ||
# Targets created in this context will not be created with a browserContextId | ||
@contexts[default_context_id] = Context.new(@client, self, nil) | ||
end | ||
|
||
# Compute the default context ID by looking for contexts not returned by Target.getBrowserContexts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kinda hacky but I didn't find a better way to find this looking through https://chromedevtools.github.io/devtools-protocol. Another way to get the ID that might be more reliable is to create a target in the default context and then get the ID from there, but that would require creating a throwaway target or something. I guess it'd be fine to create a Target, get its browserContextId, and then destroy it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm this method was also suggested (independently) by one of the Chromium engineers, so it might be fine. I am slightly nervous about the case when no tabs exist in the default context which presumably could happen, but I think generally people using browser automation should be able to ensure that the correct prerequisites are met. One other approach might be to have the Edit: The above might be slightly confusing, but this is what I mean: ibrahima@b1b7732 |
||
def compute_default_context_id | ||
created_contexts = Set.new(@client.command("Target.getBrowserContexts")["browserContextIds"]) | ||
targets = @client.command("Target.getTargets")["targetInfos"] | ||
all_contexts = Set.new(targets.map { |target| target["browserContextId"] }) | ||
(all_contexts - created_contexts).first | ||
end | ||
|
||
def each(&block) | ||
return enum_for(__method__) unless block_given? | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what the best name for this is... it's kinda confusing since
Contexts#default_context
is already a thing and the current behavior of that method is to create a new BrowserContext. But I felt like "default" should be the right thing because the effective change is to not pass a browserContextId when creating a target.Perhaps a less confusing name would be
use_persistent_context
? Similar to how Playwright haslaunchPersistentContext
.