Skip to content

Commit

Permalink
[WebDriver BiDi] addPreloadScript supports contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Sep 12, 2023
1 parent 83b11c3 commit 1d65c07
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,75 @@ async def test_page_script_can_access_preload_script_properties(
await_promise=True,
)
assert result == {"type": "number", "value": 42}


@pytest.mark.asyncio
async def test_add_preload_script_in_iframe_with_top_context_specified(
bidi_session, add_preload_script, new_tab, test_page_same_origin_frame
):
await add_preload_script(function_declaration="() => { window.bar='foo'; }", contexts=[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.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
async def test_page_script_context_isolation(
bidi_session, add_preload_script, top_context, new_tab, test_page
):
await add_preload_script(
function_declaration="() => { window.baz = 42; }",
contexts=[top_context['context']]
)

# Navigate both context to insure 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_tab["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* applied the changes to the window
result = await bidi_session.script.evaluate(
expression="window.baz",
target=ContextTarget(new_tab["context"]),
await_promise=True,
)
assert result == {type: "undefined"}
29 changes: 28 additions & 1 deletion webdriver/tests/bidi/script/add_preload_script/invalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ async def test_params_arguments_channel_ownership_invalid_value(bidi_session):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.script.add_preload_script(
function_declaration="() => {}",
arguments=[{"type": "channel", "value": {"ownership": "_UNKNOWN_"}}],
arguments=[{"type": "channel", "value": {
"ownership": "_UNKNOWN_"}}],
)


Expand Down Expand Up @@ -192,3 +193,29 @@ async def test_params_sandbox_invalid_type(bidi_session, sandbox):
await bidi_session.script.add_preload_script(
function_declaration="() => {}", sandbox=sandbox
),


async def test_params_contexts_invalid_context(bidi_session):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.script.add_preload_script(
function_declaration="() => {}",
contexts=["_UNKNOWN_"]
),


async def test_params_contexts_non_top_level_context(bidi_session, new_tab, test_page_same_origin_frame):
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=test_page_same_origin_frame
)

contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])

assert len(contexts) == 1
assert len(contexts[0]["children"]) == 1
child_info = contexts[0]["children"][0]

with pytest.raises(error.InvalidArgumentException):
await bidi_session.script.add_preload_script(
function_declaration="() => {}",
contexts=[child_info['context']]
),
3 changes: 2 additions & 1 deletion webdriver/tests/support/fixtures_bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
async def add_preload_script(bidi_session):
preload_scripts_ids = []

async def add_preload_script(function_declaration, arguments=None, sandbox=None):
async def add_preload_script(function_declaration, arguments=None, sandbox=None, contexts=None):
script = await bidi_session.script.add_preload_script(
function_declaration=function_declaration,
arguments=arguments,
sandbox=sandbox,
contexts=contexts,
)
preload_scripts_ids.append(script)

Expand Down

0 comments on commit 1d65c07

Please sign in to comment.