Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions webdriver/tests/bidi/emulation/set_locale_override/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,102 @@ def another_locale(default_locale, some_locale):
raise Exception(
f"Unexpectedly could not find locale different from the default {default_locale} and {some_locale}"
)


@pytest_asyncio.fixture
async def get_current_navigator_language(bidi_session):
async def get_current_navigator_language(context, sandbox=None):
result = await bidi_session.script.evaluate(
expression="navigator.language",
target=ContextTarget(context["context"], sandbox=sandbox),
await_promise=False,
)

return result["value"]

return get_current_navigator_language


@pytest_asyncio.fixture
async def default_navigator_language(get_current_navigator_language, top_context):
"""Returns default navigator.language value."""
return await get_current_navigator_language(top_context)


@pytest_asyncio.fixture
async def get_current_navigator_languages(bidi_session):
async def get_current_navigator_languages(context, sandbox=None):
result = await bidi_session.script.evaluate(
expression="navigator.languages",
target=ContextTarget(context["context"], sandbox=sandbox),
await_promise=False,
)

return [item["value"] for item in result["value"]]

return get_current_navigator_languages


@pytest_asyncio.fixture
async def default_navigator_languages(get_current_navigator_languages, top_context):
"""Returns default navigator.languages value."""
return await get_current_navigator_languages(top_context)


@pytest_asyncio.fixture
async def assert_locale_against_default(
top_context,
get_current_locale,
get_current_navigator_language,
get_current_navigator_languages,
default_locale,
default_navigator_language,
default_navigator_languages,
):
"""
Assert JS locale and navigator.language/s against default values.

Note: it's possible to have slightly different values between JS locale and
navigator.language/s, that's why we have to retrieve the default values
for each API.
"""

async def assert_locale_against_default(context, sandbox_name=None):
assert (
context != top_context
), "Provided context should be different from top_context"

assert await get_current_locale(context, sandbox_name) == default_locale
assert (
await get_current_navigator_language(context, sandbox_name)
== default_navigator_language
)
assert (
await get_current_navigator_languages(context, sandbox_name)
== default_navigator_languages
)

return assert_locale_against_default


@pytest_asyncio.fixture
async def assert_locale_against_value(
top_context,
get_current_locale,
get_current_navigator_language,
get_current_navigator_languages,
):
"""
Assert JS locale and navigator.language/s against provided value
"""

async def assert_locale_against_value(value, context, sandbox_name=None):
assert (
context != top_context
), "Provided context should be different from top_context"

assert await get_current_locale(context, sandbox_name) == value
assert await get_current_navigator_language(context, sandbox_name) == value
assert await get_current_navigator_languages(context, sandbox_name) == [value]

return assert_locale_against_value
77 changes: 45 additions & 32 deletions webdriver/tests/bidi/emulation/set_locale_override/contexts.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,81 @@
import pytest
from webdriver.bidi.modules.script import ContextTarget

pytestmark = pytest.mark.asyncio


async def test_contexts(bidi_session, new_tab, top_context, get_current_locale,
default_locale, some_locale):
async def test_contexts(
bidi_session,
new_tab,
some_locale,
assert_locale_against_default,
assert_locale_against_value,
):
new_context = await bidi_session.browsing_context.create(type_hint="tab")

# Set locale override.
await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"]],
locale=some_locale
contexts=[new_tab["context"]], locale=some_locale
)

# Assert locale emulated only in the required context.
assert await get_current_locale(new_tab) == some_locale
assert await get_current_locale(top_context) == default_locale
await assert_locale_against_value(some_locale, new_tab)
await assert_locale_against_default(new_context)

# Reset locale override.
await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"]],
locale=None)
contexts=[new_tab["context"]], locale=None
)

# Assert the locale is restored to the initial one.
assert await get_current_locale(new_tab) == default_locale
assert await get_current_locale(top_context) == default_locale
await assert_locale_against_default(new_tab)
await assert_locale_against_default(new_context)


async def test_multiple_contexts(bidi_session, new_tab, get_current_locale,
default_locale, some_locale):
async def test_multiple_contexts(
bidi_session,
new_tab,
assert_locale_against_value,
assert_locale_against_default,
some_locale,
):
new_context = await bidi_session.browsing_context.create(type_hint="tab")

# Set locale override.
await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"], new_context["context"]],
locale=some_locale
contexts=[new_tab["context"], new_context["context"]], locale=some_locale
)

# Assert locale emulated in all the required contexts.
assert await get_current_locale(new_tab) == some_locale
assert await get_current_locale(new_context) == some_locale
await assert_locale_against_value(some_locale, new_tab)
await assert_locale_against_value(some_locale, new_context)

# Reset locale override.
await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"], new_context["context"]],
locale=None)
contexts=[new_tab["context"], new_context["context"]], locale=None
)

# Assert the locale is restored to the initial one.
assert await get_current_locale(new_tab) == default_locale
assert await get_current_locale(new_context) == default_locale
await assert_locale_against_default(new_tab)
await assert_locale_against_default(new_context)


@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"])
async def test_iframe(
bidi_session,
new_tab,
get_current_locale,
some_locale,
domain,
inline,
another_locale,
assert_locale_against_value,
):
# Set locale override.
await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"]], locale=some_locale
)

# Assert locale emulated in the required context.
assert await get_current_locale(new_tab) == some_locale
await assert_locale_against_value(some_locale, new_tab)

iframe_url = inline("<div id='in-iframe'>foo</div>", domain=domain)
page_url = inline(f"<iframe src='{iframe_url}'></iframe>")
Expand All @@ -82,45 +91,49 @@ async def test_iframe(
iframe = contexts[0]["children"][0]

# Assert locale is emulated in the iframe context.
assert await get_current_locale(iframe) == some_locale
await assert_locale_against_value(some_locale, iframe)

sandbox_name = "test"
# Assert locale is emulated in the newly created sandbox in the iframe context.
assert await get_current_locale(iframe, sandbox_name) == some_locale
await assert_locale_against_value(some_locale, iframe, sandbox_name)

# Set another locale override.
await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"]], locale=another_locale
)

# Assert locale is emulated in the iframe context.
assert await get_current_locale(iframe) == another_locale
await assert_locale_against_value(another_locale, iframe)
# Assert locale is emulated in the existing sandbox in the iframe context.
assert await get_current_locale(iframe, sandbox_name) == another_locale
await assert_locale_against_value(another_locale, iframe, sandbox_name)


async def test_locale_override_applies_to_new_sandbox(
bidi_session, new_tab, some_locale, get_current_locale
bidi_session, new_tab, some_locale, assert_locale_against_value
):
await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"]], locale=some_locale
)

# Make sure the override got applied to the newly created sandbox.
assert await get_current_locale(new_tab, "test") == some_locale
await assert_locale_against_value(some_locale, new_tab, "test")


async def test_locale_override_applies_to_existing_sandbox(
bidi_session, new_tab, default_locale, another_locale, get_current_locale
bidi_session,
new_tab,
another_locale,
assert_locale_against_value,
assert_locale_against_default,
):
sandbox_name = "test"

# Create a sandbox.
assert await get_current_locale(new_tab, sandbox_name) == default_locale
await assert_locale_against_default(new_tab, sandbox_name)

await bidi_session.emulation.set_locale_override(
contexts=[new_tab["context"]], locale=another_locale
)

# Make sure the override got applied to the existing sandbox.
assert await get_current_locale(new_tab, sandbox_name) == another_locale
await assert_locale_against_value(another_locale, new_tab, sandbox_name)
Loading