-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WebDriver BiDi]
addPreloadScript
supports contexts
- Loading branch information
1 parent
946284d
commit 2b16dcf
Showing
5 changed files
with
166 additions
and
2 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
85 changes: 85 additions & 0 deletions
85
webdriver/tests/bidi/script/add_preload_script/contexts.py
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,85 @@ | ||
import pytest | ||
|
||
from webdriver.bidi.modules.script import ContextTarget | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"]) | ||
async def test_add_preload_script_in_frame_with_top_context_specified( | ||
bidi_session, add_preload_script, new_tab, | ||
inline, iframe, domain): | ||
|
||
iframe_content = f"<div>{domain}</div>" | ||
url = inline(f"{iframe(iframe_content, domain=domain)}") | ||
|
||
await add_preload_script( | ||
function_declaration="() => { window.bar='foo'; }", | ||
contexts=[new_tab["context"]]) | ||
|
||
await bidi_session.browsing_context.navigate( | ||
context=new_tab["context"], | ||
url=url, | ||
wait="complete", | ||
) | ||
|
||
# Check that preload script applied the changes to the window | ||
result = await bidi_session.script.evaluate( | ||
expression="window.bar", | ||
target=ContextTarget(new_tab["context"]), | ||
await_promise=True, | ||
) | ||
assert result == {"type": "string", "value": "foo"} | ||
|
||
contexts = await bidi_session.browsing_context.get_tree( | ||
root=new_tab["context"]) | ||
|
||
assert len(contexts[0]["children"]) == 1 | ||
frame_context = contexts[0]["children"][0] | ||
|
||
# Check that preload script applied the changes to the iframe | ||
result = await bidi_session.script.evaluate( | ||
expression="window.bar", | ||
target=ContextTarget(frame_context["context"]), | ||
await_promise=True, | ||
) | ||
assert result == {"type": "string", "value": "foo"} | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize("type_hint", ["tab", "window"]) | ||
async def test_page_script_context_isolation(bidi_session, add_preload_script, | ||
top_context, type_hint, | ||
test_page): | ||
await add_preload_script(function_declaration="() => { window.baz = 42; }", | ||
contexts=[top_context['context']]) | ||
|
||
new_context = await bidi_session.browsing_context.create( | ||
type_hint=type_hint) | ||
|
||
# Navigate both contexts to ensure preload script is triggered | ||
await bidi_session.browsing_context.navigate( | ||
context=top_context['context'], | ||
url=test_page, | ||
wait="complete", | ||
) | ||
await bidi_session.browsing_context.navigate( | ||
context=new_context["context"], | ||
url=test_page, | ||
wait="complete", | ||
) | ||
|
||
# Check that preload script applied the changes to the window | ||
result = await bidi_session.script.evaluate( | ||
expression="window.baz", | ||
target=ContextTarget(top_context["context"]), | ||
await_promise=True, | ||
) | ||
assert result == {"type": "number", "value": 42} | ||
|
||
# Check that preload script did *not* apply the changes to the other window | ||
result = await bidi_session.script.evaluate( | ||
expression="window.baz", | ||
target=ContextTarget(new_context["context"]), | ||
await_promise=True, | ||
) | ||
assert result == {type: "undefined"} |
28 changes: 28 additions & 0 deletions
28
webdriver/tests/bidi/script/add_preload_script/contexts_tentative.py
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,28 @@ | ||
import pytest | ||
|
||
from webdriver.bidi.modules.script import ContextTarget | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_add_preload_script_specify_context_multiple_times( | ||
bidi_session, add_preload_script, new_tab, | ||
test_page_same_origin_frame): | ||
|
||
await add_preload_script( | ||
function_declaration= | ||
"() => { window.foo = window.foo ? window.foo + 1 : 1; }", | ||
contexts=[new_tab["context"], new_tab["context"]]) | ||
|
||
await bidi_session.browsing_context.navigate( | ||
context=new_tab["context"], | ||
url=test_page_same_origin_frame, | ||
wait="complete", | ||
) | ||
|
||
# Check that preload script applied the changes to the window | ||
result = await bidi_session.script.evaluate( | ||
expression="window.foo", | ||
target=ContextTarget(new_tab["context"]), | ||
await_promise=True, | ||
) | ||
assert result == {"type": "number", "value": "1"} |
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
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