-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[🚀 Feature] [py]: Implement BiDi add/remove_request handler #14738
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
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2929801
[py] Add bidi.py to define base classes for generated bidi classes
jkim2492 3d1eb5f
[py] Add generated BiDi objects required for request handlers
jkim2492 d189f70
[py] Implement add/remove_request_handler
jkim2492 8de6863
[py] Remove xfail_edge from bidi_network_tests
jkim2492 1235efc
[pu] Fix typing imports in script.py
jkim2492 64e568f
Merge branch 'trunk' into network
pujagani f710e32
Merge branch 'trunk' into network
jkim2492 46253bc
Run callbacks in threads
jkim2492 a8dd1fe
Move from trio to threading
jkim2492 47904f7
Merge branch 'trunk' into network
shbenzer e1c6820
Merge branch 'trunk' into network
jkim2492 4275902
remove callback when removing intercepts
jkim2492 0a106f2
[py] Remove cache disable from webdriver.py
jkim2492 cabe7e8
Add browsingcontext.navigate
jkim2492 9e74012
Remove xfail_firefox
jkim2492 bb6bc44
Merge branch 'trunk' into network
jkim2492 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from dataclasses import dataclass | ||
from dataclasses import fields | ||
from dataclasses import is_dataclass | ||
from typing import get_type_hints | ||
|
||
|
||
@dataclass | ||
class BidiObject: | ||
def to_json(self): | ||
json = {} | ||
for field in fields(self): | ||
value = getattr(self, field.name) | ||
if value is None: | ||
continue | ||
if is_dataclass(value): | ||
value = value.to_json() | ||
elif isinstance(value, list): | ||
value = [v.to_json() if hasattr(v, "to_json") else v for v in value] | ||
elif isinstance(value, dict): | ||
value = {k: v.to_json() if hasattr(v, "to_json") else v for k, v in value.items()} | ||
key = field.name[:-1] if field.name.endswith("_") else field.name | ||
json[key] = value | ||
return json | ||
|
||
@classmethod | ||
def from_json(cls, json): | ||
return cls(**json) | ||
|
||
|
||
@dataclass | ||
class BidiEvent(BidiObject): | ||
@classmethod | ||
def from_json(cls, json): | ||
params = get_type_hints(cls)["params"].from_json(json) | ||
return cls(params) | ||
|
||
|
||
@dataclass | ||
class BidiCommand(BidiObject): | ||
def cmd(self): | ||
result = yield self.to_json() | ||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import typing | ||
from dataclasses import dataclass | ||
|
||
from .bidi import BidiCommand | ||
from .bidi import BidiObject | ||
|
||
ReadinessState = typing.Literal["none", "interactive", "complete"] | ||
|
||
|
||
@dataclass | ||
class NavigateParameters(BidiObject): | ||
context: str | ||
url: str | ||
wait: typing.Optional[ReadinessState] = None | ||
|
||
|
||
@dataclass | ||
class Navigate(BidiCommand): | ||
params: NavigateParameters | ||
method: typing.Literal["browsingContext.navigate"] = "browsingContext.navigate" | ||
|
||
|
||
Navigation = str | ||
|
||
class BrowsingContext: | ||
def __init__(self, conn): | ||
self.conn = conn | ||
|
||
def navigate(self, params: NavigateParameters): | ||
result = self.conn.execute(NavigateParameters(params).cmd()) | ||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import typing | ||
from dataclasses import dataclass | ||
|
||
from selenium.webdriver.common.bidi.cdp import import_devtools | ||
|
||
from . import browsing_context | ||
from . import script | ||
from .bidi import BidiCommand | ||
from .bidi import BidiEvent | ||
from .bidi import BidiObject | ||
|
||
devtools = import_devtools("") | ||
event_class = devtools.util.event_class | ||
|
||
InterceptPhase = typing.Literal["beforeRequestSent", "responseStarted", "authRequired"] | ||
|
||
|
||
@dataclass | ||
class UrlPatternPattern(BidiObject): | ||
type_: typing.Literal["pattern"] = "pattern" | ||
protocol: typing.Optional[str] = None | ||
hostname: typing.Optional[str] = None | ||
port: typing.Optional[str] = None | ||
pathname: typing.Optional[str] = None | ||
search: typing.Optional[str] = None | ||
|
||
|
||
@dataclass | ||
class UrlPatternString(BidiObject): | ||
pattern: str | ||
type_: typing.Literal["string"] = "string" | ||
|
||
|
||
UrlPattern = typing.Union[UrlPatternPattern, UrlPatternString] | ||
|
||
|
||
@dataclass | ||
class AddInterceptParameters(BidiObject): | ||
phases: typing.List[InterceptPhase] | ||
contexts: typing.Optional[typing.List[browsing_context.BrowsingContext]] = None | ||
urlPatterns: typing.Optional[typing.List[UrlPattern]] = None | ||
|
||
|
||
@dataclass | ||
class AddIntercept(BidiCommand): | ||
params: AddInterceptParameters | ||
method: typing.Literal["network.addIntercept"] = "network.addIntercept" | ||
|
||
|
||
Request = str | ||
|
||
|
||
@dataclass | ||
class StringValue(BidiObject): | ||
value: str | ||
type_: typing.Literal["string"] = "string" | ||
|
||
|
||
@dataclass | ||
class Base64Value(BidiObject): | ||
value: str | ||
type_: typing.Literal["base64"] = "base64" | ||
|
||
|
||
BytesValue = typing.Union[StringValue, Base64Value] | ||
|
||
|
||
@dataclass | ||
class CookieHeader(BidiObject): | ||
name: str | ||
value: BytesValue | ||
|
||
|
||
@dataclass | ||
class Header(BidiObject): | ||
name: str | ||
value: BytesValue | ||
|
||
|
||
@dataclass | ||
class ContinueRequestParameters(BidiObject): | ||
request: Request | ||
body: typing.Optional[BytesValue] = None | ||
cookies: typing.Optional[typing.List[CookieHeader]] = None | ||
headers: typing.Optional[typing.List[Header]] = None | ||
method: typing.Optional[str] = None | ||
url: typing.Optional[str] = None | ||
|
||
|
||
@dataclass | ||
class ContinueRequest(BidiCommand): | ||
params: ContinueRequestParameters | ||
method: typing.Literal["network.continueRequest"] = "network.continueRequest" | ||
|
||
|
||
Intercept = str | ||
|
||
|
||
@dataclass | ||
class RemoveInterceptParameters(BidiObject): | ||
intercept: Intercept | ||
|
||
|
||
@dataclass | ||
class RemoveIntercept(BidiCommand): | ||
params: RemoveInterceptParameters | ||
method: typing.Literal["network.removeIntercept"] = "network.removeIntercept" | ||
|
||
|
||
SameSite = typing.Literal["strict", "lax", "none"] | ||
|
||
|
||
@dataclass | ||
class Cookie(BidiObject): | ||
name: str | ||
value: BytesValue | ||
domain: str | ||
path: str | ||
size: int | ||
httpOnly: bool | ||
secure: bool | ||
sameSite: SameSite | ||
expiry: typing.Optional[int] = None | ||
|
||
|
||
@dataclass | ||
class FetchTimingInfo(BidiObject): | ||
timeOrigin: float | ||
requestTime: float | ||
redirectStart: float | ||
redirectEnd: float | ||
fetchStart: float | ||
dnsStart: float | ||
dnsEnd: float | ||
connectStart: float | ||
connectEnd: float | ||
tlsStart: float | ||
requestStart: float | ||
responseStart: float | ||
responseEnd: float | ||
|
||
|
||
@dataclass | ||
class RequestData(BidiObject): | ||
request: Request | ||
url: str | ||
method: str | ||
headersSize: int | ||
timings: FetchTimingInfo | ||
headers: typing.Optional[typing.List[Header]] = None | ||
cookies: typing.Optional[typing.List[Cookie]] = None | ||
bodySize: typing.Optional[int] = None | ||
|
||
|
||
@dataclass | ||
class Initiator(BidiObject): | ||
type_: typing.Literal["parser", "script", "preflight", "other"] | ||
columnNumber: typing.Optional[int] = None | ||
lineNumber: typing.Optional[int] = None | ||
stackTrace: typing.Optional[script.StackTrace] = None | ||
request: typing.Optional[Request] = None | ||
|
||
|
||
@dataclass | ||
class BeforeRequestSentParameters(BidiObject): | ||
isBlocked: bool | ||
redirectCount: int | ||
request: RequestData | ||
timestamp: int | ||
initiator: Initiator | ||
context: typing.Optional[browsing_context.BrowsingContext] = None | ||
navigation: typing.Optional[browsing_context.Navigation] = None | ||
intercepts: typing.Optional[typing.List[Intercept]] = None | ||
|
||
|
||
@dataclass | ||
@event_class("network.beforeRequestSent") | ||
class BeforeRequestSent(BidiEvent): | ||
params: BeforeRequestSentParameters | ||
method: typing.Literal["network.beforeRequestSent"] = "network.beforeRequestSent" | ||
|
||
|
||
class Network: | ||
def __init__(self, conn): | ||
self.conn = conn | ||
|
||
def add_intercept(self, params: AddInterceptParameters): | ||
result = self.conn.execute(AddIntercept(params).cmd()) | ||
return result | ||
|
||
def continue_request(self, params: ContinueRequestParameters): | ||
result = self.conn.execute(ContinueRequest(params).cmd()) | ||
return result | ||
|
||
def remove_intercept(self, params: RemoveInterceptParameters): | ||
self.conn.execute(RemoveIntercept(params).cmd()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.