[wdspec] add a test for verifying local storage isolation in user contexts (#44378)
diff --git a/webdriver/tests/bidi/browser/__init__.py b/webdriver/tests/bidi/browser/__init__.py
index b9cb10f..e1327d5 100644
--- a/webdriver/tests/bidi/browser/__init__.py
+++ b/webdriver/tests/bidi/browser/__init__.py
@@ -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"]
diff --git a/webdriver/tests/bidi/browser/create_user_context/create_user_context.py b/webdriver/tests/bidi/browser/create_user_context/create_user_context.py
index d62be41..f495498 100644
--- a/webdriver/tests/bidi/browser/create_user_context/create_user_context.py
+++ b/webdriver/tests/bidi/browser/create_user_context/create_user_context.py
@@ -1,6 +1,7 @@
import pytest
from .. import get_user_context_ids
+from .. import get_local_storage, set_local_storage
@pytest.mark.asyncio
@@ -23,3 +24,37 @@
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