diff --git a/webdriver/tests/bidi/emulation/set_locale_override/conftest.py b/webdriver/tests/bidi/emulation/set_locale_override/conftest.py index e687010128a470..99560e70ac53a2 100644 --- a/webdriver/tests/bidi/emulation/set_locale_override/conftest.py +++ b/webdriver/tests/bidi/emulation/set_locale_override/conftest.py @@ -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 diff --git a/webdriver/tests/bidi/emulation/set_locale_override/contexts.py b/webdriver/tests/bidi/emulation/set_locale_override/contexts.py index b2a2ceaecae316..3e60adb4cd75d3 100644 --- a/webdriver/tests/bidi/emulation/set_locale_override/contexts.py +++ b/webdriver/tests/bidi/emulation/set_locale_override/contexts.py @@ -1,64 +1,73 @@ 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( @@ -66,7 +75,7 @@ async def test_iframe( ) # 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("
foo
", domain=domain) page_url = inline(f"") @@ -82,11 +91,11 @@ 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( @@ -94,33 +103,37 @@ async def test_iframe( ) # 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) diff --git a/webdriver/tests/bidi/emulation/set_locale_override/locale.py b/webdriver/tests/bidi/emulation/set_locale_override/locale.py index 3da7f62f520082..c7ea1473b9ca49 100644 --- a/webdriver/tests/bidi/emulation/set_locale_override/locale.py +++ b/webdriver/tests/bidi/emulation/set_locale_override/locale.py @@ -3,79 +3,96 @@ pytestmark = pytest.mark.asyncio -async def test_locale_set_override_and_reset(bidi_session, top_context, - get_current_locale, default_locale, some_locale, another_locale): - assert await get_current_locale(top_context) == default_locale +async def test_locale_set_override_and_reset( + bidi_session, + new_tab, + some_locale, + another_locale, + assert_locale_against_default, + assert_locale_against_value, +): + await assert_locale_against_default(new_tab) # Set locale override. await bidi_session.emulation.set_locale_override( - contexts=[top_context["context"]], - locale=some_locale + contexts=[new_tab["context"]], locale=some_locale ) - assert await get_current_locale(top_context) == some_locale + await assert_locale_against_value(some_locale, new_tab) - # Set locale override. + # Set another locale override. await bidi_session.emulation.set_locale_override( - contexts=[top_context["context"]], - locale=another_locale + contexts=[new_tab["context"]], locale=another_locale ) - assert await get_current_locale(top_context) == another_locale + await assert_locale_against_value(another_locale, new_tab) - # Set locale override. + # Reset locale override. await bidi_session.emulation.set_locale_override( - contexts=[top_context["context"]], - locale=None + contexts=[new_tab["context"]], locale=None ) - assert await get_current_locale(top_context) == default_locale - - -@pytest.mark.parametrize("value", [ - # Simple language code (2-letter). - "en", - # Language and region (both 2-letter). - "en-US", - # Language and script (4-letter). - "sr-Latn", - # Language, script, and region. - "zh-Hans-CN", -]) -async def test_locale_values(bidi_session, top_context, get_current_locale, - default_locale, value): - assert await get_current_locale(top_context) == default_locale + await assert_locale_against_default(new_tab) + + +@pytest.mark.parametrize( + "value", + [ + # Simple language code (2-letter). + "en", + # Language and region (both 2-letter). + "en-US", + # Language and script (4-letter). + "sr-Latn", + # Language, script, and region. + "zh-Hans-CN", + ], +) +async def test_locale_values( + bidi_session, + new_tab, + assert_locale_against_default, + assert_locale_against_value, + value, +): + await assert_locale_against_default(new_tab) # Set locale override. await bidi_session.emulation.set_locale_override( - contexts=[top_context["context"]], - locale=value + contexts=[new_tab["context"]], locale=value ) - assert await get_current_locale(top_context) == value - - -@pytest.mark.parametrize("locale,expected_locale", [ - # Locale with Unicode extension keyword for collation. - ("de-DE-u-co-phonebk", "de-DE"), - # Lowercase language and region. - ("fr-ca", "fr-CA"), - # Uppercase language and region (should be normalized by Intl.Locale). - ("FR-CA", "fr-CA"), - # Mixed case language and region (should be normalized by Intl.Locale). - ("fR-cA", "fr-CA"), - # Locale with transform extension (simple case). - ("en-t-zh", "en"), -]) -async def test_locale_values_normalized_by_intl(bidi_session, top_context, - get_current_locale, - default_locale, locale, expected_locale): + await assert_locale_against_value(value, new_tab) + + +@pytest.mark.parametrize( + "locale,expected_locale", + [ + # Locale with Unicode extension keyword for collation. + ("de-DE-u-co-phonebk", "de-DE"), + # Lowercase language and region. + ("fr-ca", "fr-CA"), + # Uppercase language and region (should be normalized by Intl.Locale). + ("FR-CA", "fr-CA"), + # Mixed case language and region (should be normalized by Intl.Locale). + ("fR-cA", "fr-CA"), + # Locale with transform extension (simple case). + ("en-t-zh", "en"), + ], +) +async def test_locale_values_normalized_by_intl( + bidi_session, + top_context, + get_current_locale, + default_locale, + locale, + expected_locale, +): assert await get_current_locale(top_context) == default_locale # Set locale override. await bidi_session.emulation.set_locale_override( - contexts=[top_context["context"]], - locale=locale + contexts=[top_context["context"]], locale=locale ) assert await get_current_locale(top_context) == expected_locale diff --git a/webdriver/tests/bidi/emulation/set_locale_override/user_contexts.py b/webdriver/tests/bidi/emulation/set_locale_override/user_contexts.py index 525dec61093ca7..fec2fc745f41cc 100644 --- a/webdriver/tests/bidi/emulation/set_locale_override/user_contexts.py +++ b/webdriver/tests/bidi/emulation/set_locale_override/user_contexts.py @@ -4,45 +4,46 @@ async def test_user_contexts( - bidi_session, - create_user_context, - new_tab, - get_current_locale, - default_locale, - some_locale + bidi_session, + create_user_context, + new_tab, + some_locale, + assert_locale_against_default, + assert_locale_against_value, ): user_context = await create_user_context() context_in_user_context = await bidi_session.browsing_context.create( - user_context=user_context, type_hint="tab") + user_context=user_context, type_hint="tab" + ) - assert await get_current_locale(new_tab) == default_locale + await assert_locale_against_default(new_tab) # Set locale override. await bidi_session.emulation.set_locale_override( - user_contexts=[user_context], - locale=some_locale) + user_contexts=[user_context], locale=some_locale + ) # Assert the locale is emulated in user context. - assert await get_current_locale(context_in_user_context) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context) # Assert the default user context is not affected. - assert await get_current_locale(new_tab) == default_locale + await assert_locale_against_default(new_tab) # Create a new context in the user context. another_context_in_user_context = await bidi_session.browsing_context.create( - user_context=user_context, type_hint="tab") + user_context=user_context, type_hint="tab" + ) # Assert the locale is emulated in a new browsing context of the user context. - assert await get_current_locale( - another_context_in_user_context) == some_locale + await assert_locale_against_value(some_locale, another_context_in_user_context) async def test_set_to_default_user_context( - bidi_session, - new_tab, - create_user_context, - get_current_locale, - default_locale, - some_locale + bidi_session, + new_tab, + create_user_context, + some_locale, + assert_locale_against_default, + assert_locale_against_value, ): user_context = await create_user_context() context_in_user_context = await bidi_session.browsing_context.create( @@ -56,29 +57,27 @@ async def test_set_to_default_user_context( # Make sure that the locale changes are only applied to the context # associated with default user context. - assert await get_current_locale(context_in_user_context) == default_locale - assert await get_current_locale(new_tab) == some_locale + await assert_locale_against_default(context_in_user_context) + await assert_locale_against_value(some_locale, new_tab) # Create a new context in the default context. context_in_default_context = await bidi_session.browsing_context.create( type_hint="tab" ) - assert await get_current_locale(context_in_default_context) == some_locale - assert await get_current_locale(context_in_default_context) == some_locale + await assert_locale_against_value(some_locale, context_in_default_context) # Reset locale override. await bidi_session.emulation.set_locale_override( - user_contexts=["default"], - locale=None + user_contexts=["default"], locale=None ) async def test_set_to_multiple_user_contexts( - bidi_session, - create_user_context, - get_current_locale, - some_locale, + bidi_session, + create_user_context, + some_locale, + assert_locale_against_value, ): user_context_1 = await create_user_context() context_in_user_context_1 = await bidi_session.browsing_context.create( @@ -89,21 +88,20 @@ async def test_set_to_multiple_user_contexts( user_context=user_context_2, type_hint="tab" ) await bidi_session.emulation.set_locale_override( - user_contexts=[user_context_1, user_context_2], - locale=some_locale + user_contexts=[user_context_1, user_context_2], locale=some_locale ) - assert await get_current_locale(context_in_user_context_1) == some_locale - assert await get_current_locale(context_in_user_context_2) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context_1) + await assert_locale_against_value(some_locale, context_in_user_context_2) async def test_set_to_user_context_and_then_to_context( - bidi_session, - create_user_context, - get_current_locale, - default_locale, - some_locale, - another_locale + bidi_session, + create_user_context, + some_locale, + another_locale, + assert_locale_against_default, + assert_locale_against_value, ): user_context = await create_user_context() context_in_user_context_1 = await bidi_session.browsing_context.create( @@ -112,30 +110,28 @@ async def test_set_to_user_context_and_then_to_context( # Apply locale override to the user context. await bidi_session.emulation.set_locale_override( - user_contexts=[user_context], - locale=some_locale + user_contexts=[user_context], locale=some_locale ) # Apply locale override now only to the context. await bidi_session.emulation.set_locale_override( - contexts=[context_in_user_context_1["context"]], - locale=another_locale + contexts=[context_in_user_context_1["context"]], locale=another_locale ) - assert await get_current_locale(context_in_user_context_1) == another_locale + await assert_locale_against_value(another_locale, context_in_user_context_1) await bidi_session.browsing_context.reload( context=context_in_user_context_1["context"], wait="complete" ) # Make sure that after reload the locale is still updated. - assert await get_current_locale(context_in_user_context_1) == another_locale + await assert_locale_against_value(another_locale, context_in_user_context_1) # Create a new context in the user context. context_in_user_context_2 = await bidi_session.browsing_context.create( user_context=user_context, type_hint="tab" ) # Make sure that the locale override for the user context is applied. - assert await get_current_locale(context_in_user_context_2) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context_2) # Reset the override for context. await bidi_session.emulation.set_locale_override( @@ -144,7 +140,7 @@ async def test_set_to_user_context_and_then_to_context( ) # Make sure that the locale override is set to user context value. - assert await get_current_locale(context_in_user_context_1) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context_1) # Reset the override for user context. await bidi_session.emulation.set_locale_override( @@ -153,16 +149,16 @@ async def test_set_to_user_context_and_then_to_context( ) # Make sure that the locale override is reset. - assert await get_current_locale(context_in_user_context_1) == default_locale + await assert_locale_against_default(context_in_user_context_1) async def test_set_to_context_and_then_to_user_context( bidi_session, create_user_context, - get_current_locale, - default_locale, some_locale, - another_locale + another_locale, + assert_locale_against_default, + assert_locale_against_value, ): user_context = await create_user_context() context_in_user_context_1 = await bidi_session.browsing_context.create( @@ -174,7 +170,7 @@ async def test_set_to_context_and_then_to_user_context( contexts=[context_in_user_context_1["context"]], locale=some_locale ) - assert await get_current_locale(context_in_user_context_1) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context_1) # Apply locale override to the user context. await bidi_session.emulation.set_locale_override( @@ -182,21 +178,21 @@ async def test_set_to_context_and_then_to_user_context( ) # Make sure that context has still the context locale override. - assert await get_current_locale(context_in_user_context_1) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context_1) await bidi_session.browsing_context.reload( context=context_in_user_context_1["context"], wait="complete" ) # Make sure that after reload the locale still has the context locale override. - assert await get_current_locale(context_in_user_context_1) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context_1) # Create a new context in the user context. context_in_user_context_2 = await bidi_session.browsing_context.create( user_context=user_context, type_hint="tab" ) # Make sure that the locale override for the user context is applied. - assert await get_current_locale(context_in_user_context_2) == another_locale + await assert_locale_against_value(another_locale, context_in_user_context_2) # Reset override for user context. await bidi_session.emulation.set_locale_override( @@ -205,6 +201,6 @@ async def test_set_to_context_and_then_to_user_context( ) # Make sure that the locale override for the first context is still set. - assert await get_current_locale(context_in_user_context_1) == some_locale + await assert_locale_against_value(some_locale, context_in_user_context_1) # Make sure that the locale override for the second context is reset. - assert await get_current_locale(context_in_user_context_2) == default_locale + await assert_locale_against_default(context_in_user_context_2)