-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: #293
- Loading branch information
Thiago Perrotta
committed
Jul 7, 2023
1 parent
d3fc4cf
commit ab98471
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,97 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2023 Google LLC. | ||
# Copyright (c) Microsoft Corporation. | ||
# | ||
# Licensed 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 asyncio | ||
import itertools | ||
import logging | ||
from pathlib import Path | ||
|
||
from _helpers import get_websocket, run_and_wait_command | ||
|
||
ID = itertools.count(1000) | ||
|
||
logging.basicConfig( | ||
format="%(message)s", | ||
level=logging.DEBUG, | ||
) | ||
|
||
|
||
async def main(): | ||
# Open browser | ||
websocket = await get_websocket() | ||
await run_and_wait_command( | ||
{ | ||
"id": next(ID), | ||
"method": "session.new", | ||
"params": {} | ||
}, websocket) | ||
|
||
# Add preload script | ||
await run_and_wait_command( | ||
{ | ||
"id": next(ID), | ||
"method": "script.addPreloadScript", | ||
"params": { | ||
"functionDeclaration": """() => { window.foo='bar'; }""", | ||
} | ||
}, websocket) | ||
|
||
# Open tab | ||
command_result = await run_and_wait_command( | ||
{ | ||
"id": next(ID), | ||
"method": "browsingContext.create", | ||
"params": { | ||
"type": "tab" | ||
} | ||
}, websocket) | ||
context_id = command_result['result']['context'] | ||
|
||
# Navigate to page: https://news.ycombinator.com/ | ||
# To avoid network dependency in this test, use a local (static) copy. | ||
page_url = f'file://{Path(__file__).parent.resolve()}/app.html' | ||
await run_and_wait_command( | ||
{ | ||
"id": next(ID), | ||
"method": "browsingContext.navigate", | ||
"params": { | ||
"url": page_url, | ||
"context": context_id, | ||
"wait": "complete" | ||
} | ||
}, websocket) | ||
|
||
# Verify preload script is executed. | ||
result = await run_and_wait_command( | ||
{ | ||
"id": next(ID), | ||
"method": "script.evaluate", | ||
"params": { | ||
"expression": "window.foo", | ||
"target": { | ||
"context": context_id | ||
}, | ||
"awaitPromise": True, | ||
"resultOwnership": "root", | ||
} | ||
}, websocket) | ||
assert result["result"]["result"] == {"type": "string", "value": "bar"} | ||
|
||
|
||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
result = loop.run_until_complete(main()) |