Skip to content

Commit

Permalink
[wdspec] add a test for verifying local storage isolation in user con…
Browse files Browse the repository at this point in the history
…texts (#44378)
  • Loading branch information
OrKoN authored Feb 2, 2024
1 parent 5a5bd1e commit 14d6dc2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
29 changes: 29 additions & 0 deletions webdriver/tests/bidi/browser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
from webdriver.bidi.modules.script import ContextTarget

async def get_user_context_ids(bidi_session):
"""
Returns the list of string ids of the current user contexts.
"""
user_contexts = await bidi_session.browser.get_user_contexts()
return [user_context_info["userContext"] for user_context_info in user_contexts]


async def set_local_storage(bidi_session, context: str, key: str, value: str):
"""
Sets the value for the key in the context's localStorage.
"""
await bidi_session.script.call_function(
function_declaration="""(key, value) => localStorage.setItem(key, value)""",
arguments=[{"type": "string", "value": key}, {"type": "string", "value": value}],
await_promise=False,
target=ContextTarget(context["context"]),
)


async def get_local_storage(bidi_session, context: str, key: str):
"""
Returns the value identified by the key from the context's localStorage.
"""
result = await bidi_session.script.call_function(
function_declaration="""(key) => localStorage.getItem(key)""",
arguments=[{"type": "string", "value": key}],
await_promise=False,
target=ContextTarget(context["context"]),
)
if not "value" in result:
return None
return result["value"]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from .. import get_user_context_ids
from .. import get_local_storage, set_local_storage


@pytest.mark.asyncio
Expand All @@ -23,3 +24,37 @@ async def test_unique_id(bidi_session, create_user_context):
assert other_context in await get_user_context_ids(bidi_session)

assert first_context != other_context


@pytest.mark.asyncio
async def test_storage_isolation(bidi_session, create_user_context, inline):
first_context = await create_user_context()
other_context = await create_user_context()

test_key = "test"

tab_first_context = await bidi_session.browsing_context.create(
type_hint="tab",
user_context=first_context
)

await bidi_session.browsing_context.navigate(context=tab_first_context["context"],
url=inline("test"),
wait="complete")

tab_other_context = await bidi_session.browsing_context.create(
type_hint="tab",
user_context=other_context
)

await bidi_session.browsing_context.navigate(context=tab_other_context["context"],
url=inline("test"),
wait="complete")

assert await get_local_storage(bidi_session, tab_first_context, test_key) == None
assert await get_local_storage(bidi_session, tab_other_context, test_key) == None

await set_local_storage(bidi_session, tab_first_context, test_key, "value")

assert await get_local_storage(bidi_session, tab_first_context, test_key) == "value"
assert await get_local_storage(bidi_session, tab_other_context, test_key) == None

0 comments on commit 14d6dc2

Please sign in to comment.