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
13 changes: 13 additions & 0 deletions tools/webdriver/webdriver/bidi/modules/emulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,16 @@ def set_user_agent_override(
"contexts": contexts,
"userContexts": user_contexts,
}

@command
def set_network_conditions(
self,
network_conditions: Nullable[Dict[str, Any]],
contexts: Maybe[List[str]] = UNDEFINED,
user_contexts: Maybe[List[str]] = UNDEFINED,
) -> Mapping[str, Any]:
return {
"networkConditions": network_conditions,
"contexts": contexts,
"userContexts": user_contexts,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OFFLINE_NETWORK_CONDITIONS = {"type": "offline"}
78 changes: 78 additions & 0 deletions webdriver/tests/bidi/emulation/set_network_conditions/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import uuid

import pytest_asyncio

from webdriver.bidi.error import UnknownErrorException
from webdriver.bidi.modules.script import ContextTarget, \
ScriptEvaluateResultException


@pytest_asyncio.fixture
async def get_navigator_online(bidi_session):
async def get_navigator_online(context):
result = await bidi_session.script.evaluate(
expression="navigator.onLine",
target=ContextTarget(context["context"]),
await_promise=True,
)

return result["value"]

return get_navigator_online


@pytest_asyncio.fixture
async def get_can_fetch(bidi_session, url):
async def get_can_fetch(context):
try:
await bidi_session.script.call_function(
function_declaration=f"(url)=>fetch(url)",
arguments=[{
"type": "string",
"value": url(f"/common/blank.html?{uuid.uuid4()}")
}],
target=ContextTarget(context["context"]),
await_promise=True,
)
return True
except ScriptEvaluateResultException:
return False

return get_can_fetch


@pytest_asyncio.fixture
async def get_can_navigate(bidi_session, url):
async def get_can_navigate(context):
try:
await bidi_session.browsing_context.navigate(
context=context["context"],
url=url(f"/common/blank.html?{uuid.uuid4()}"),
wait="complete")
return True
except UnknownErrorException:
return False

return get_can_navigate


@pytest_asyncio.fixture(params=['default', 'new'],
ids=["Default user context", "Custom user context"])
async def target_user_context(request, create_user_context):
return request.param


@pytest_asyncio.fixture
async def affected_user_context(target_user_context, create_user_context):
""" Returns either a new or default user context. """
if target_user_context == 'default':
return 'default'
return await create_user_context()


@pytest_asyncio.fixture
async def not_affected_user_context(target_user_context, create_user_context):
""" Returns opposite to `affected_user_context user context. """
if target_user_context == 'new':
return 'default'
return await create_user_context()
171 changes: 171 additions & 0 deletions webdriver/tests/bidi/emulation/set_network_conditions/contexts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import pytest

from . import OFFLINE_NETWORK_CONDITIONS

pytestmark = pytest.mark.asyncio


async def test_isolation(bidi_session, top_context,
get_navigator_online):
another_context = await bidi_session.browsing_context.create(
type_hint="tab")

assert await get_navigator_online(top_context)
assert await get_navigator_online(another_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
contexts=[top_context["context"]])

assert not await get_navigator_online(top_context)
assert await get_navigator_online(another_context)
yet_another_context = await bidi_session.browsing_context.create(
type_hint="tab")
assert await get_navigator_online(yet_another_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
contexts=[top_context["context"]])

assert await get_navigator_online(top_context)
assert await get_navigator_online(another_context)
assert await get_navigator_online(yet_another_context)


@pytest.mark.parametrize("domain", ["", "alt"],
ids=["same_origin", "cross_origin"])
async def test_frame(bidi_session, url, get_navigator_online,
top_context, create_iframe, domain):
iframe_id = await create_iframe(top_context, url('/', domain=domain));

assert await get_navigator_online(iframe_id)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
contexts=[top_context["context"]])

assert not await get_navigator_online(iframe_id)

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
contexts=[top_context["context"]])

assert await get_navigator_online(iframe_id)


async def test_overrides_user_contexts(bidi_session, get_navigator_online,
affected_user_context):
affected_context = await bidi_session.browsing_context.create(
type_hint="tab", user_context=affected_user_context)

assert await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
contexts=[affected_context["context"]])

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
user_contexts=[affected_user_context])

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
user_contexts=[affected_user_context])

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
contexts=[affected_context["context"]])

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
user_contexts=[affected_user_context])

assert await get_navigator_online(affected_context)


async def test_restores_to_user_contexts_when_removed(bidi_session,
get_navigator_online,
affected_user_context):
affected_context = await bidi_session.browsing_context.create(
type_hint="tab", user_context=affected_user_context)

assert await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
contexts=[affected_context["context"]])

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
user_contexts=[affected_user_context])

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
contexts=[affected_context["context"]])

assert not await get_navigator_online(affected_context)


async def test_overrides_global(bidi_session, get_navigator_online,
affected_user_context):
affected_context = await bidi_session.browsing_context.create(
type_hint="tab", user_context=affected_user_context)

assert await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
contexts=[affected_context["context"]])

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=None)

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS)

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
contexts=[affected_context["context"]])

assert not await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=None)

assert await get_navigator_online(affected_context)


async def test_restores_to_global_when_removed(bidi_session,
get_navigator_online,
affected_user_context):
affected_context = await bidi_session.browsing_context.create(
type_hint="tab", user_context=affected_user_context)

assert await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS,
contexts=[affected_context["context"]])

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS)

await bidi_session.emulation.set_network_conditions(
network_conditions=None,
contexts=[affected_context["context"]])

assert not await get_navigator_online(affected_context)
47 changes: 47 additions & 0 deletions webdriver/tests/bidi/emulation/set_network_conditions/global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest

from . import OFFLINE_NETWORK_CONDITIONS

pytestmark = pytest.mark.asyncio


async def test_top_level(bidi_session, create_user_context,
get_navigator_online, affected_user_context):
affected_context = await bidi_session.browsing_context.create(
type_hint="tab", user_context=affected_user_context)

assert await get_navigator_online(affected_context)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS)

assert not await get_navigator_online(affected_context)

another_affected_context = await bidi_session.browsing_context.create(
type_hint="tab", user_context=affected_user_context)
assert not await get_navigator_online(another_affected_context)

await bidi_session.emulation.set_network_conditions(network_conditions=None)

assert await get_navigator_online(affected_context)
assert await get_navigator_online(another_affected_context)


@pytest.mark.parametrize("domain", ["", "alt"],
ids=["same_origin", "cross_origin"])
async def test_iframe(bidi_session, url, get_navigator_online,
top_context, create_iframe, domain, affected_user_context):
affected_context = await bidi_session.browsing_context.create(
type_hint="tab", user_context=affected_user_context)
iframe_id = await create_iframe(affected_context, url('/', domain=domain))

assert await get_navigator_online(iframe_id)

await bidi_session.emulation.set_network_conditions(
network_conditions=OFFLINE_NETWORK_CONDITIONS)

assert not await get_navigator_online(iframe_id)

await bidi_session.emulation.set_network_conditions(network_conditions=None)

assert await get_navigator_online(iframe_id)
Loading