|
| 1 | +import pytest |
| 2 | +import webdriver.bidi.error as error |
| 3 | + |
| 4 | +from .. import PAGE_EMPTY_TEXT, RESPONSE_COMPLETED_EVENT |
| 5 | + |
| 6 | +pytestmark = pytest.mark.asyncio |
| 7 | + |
| 8 | + |
| 9 | +async def test_user_context( |
| 10 | + bidi_session, |
| 11 | + add_data_collector, |
| 12 | + create_user_context, |
| 13 | + setup_network_test, |
| 14 | + wait_for_event, |
| 15 | + wait_for_future_safe, |
| 16 | + url, |
| 17 | + fetch, |
| 18 | +): |
| 19 | + user_context = await create_user_context() |
| 20 | + context_in_user_context_1 = await bidi_session.browsing_context.create( |
| 21 | + user_context=user_context, type_hint="tab" |
| 22 | + ) |
| 23 | + await setup_network_test( |
| 24 | + events=[RESPONSE_COMPLETED_EVENT], context=context_in_user_context_1["context"] |
| 25 | + ) |
| 26 | + |
| 27 | + # Add a collector for this user context |
| 28 | + collector = await add_data_collector(user_contexts=[user_context]) |
| 29 | + |
| 30 | + # Trigger a request in `context_in_user_context_1` |
| 31 | + on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT) |
| 32 | + await fetch(url(PAGE_EMPTY_TEXT), context=context_in_user_context_1) |
| 33 | + event = await wait_for_future_safe(on_response_completed) |
| 34 | + request = event["request"]["request"] |
| 35 | + |
| 36 | + # Check that the request data from `context_in_user_context_1` can be retrieved |
| 37 | + await bidi_session.network.get_data(request=request, data_type="response") |
| 38 | + |
| 39 | + # Create a new context in the user context. |
| 40 | + context_in_user_context_2 = await bidi_session.browsing_context.create( |
| 41 | + user_context=user_context, type_hint="tab" |
| 42 | + ) |
| 43 | + await setup_network_test( |
| 44 | + events=[RESPONSE_COMPLETED_EVENT], context=context_in_user_context_2["context"] |
| 45 | + ) |
| 46 | + |
| 47 | + # Trigger a request in `context_in_user_context_2` |
| 48 | + on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT) |
| 49 | + await fetch(url(PAGE_EMPTY_TEXT), context=context_in_user_context_2) |
| 50 | + event = await wait_for_future_safe(on_response_completed) |
| 51 | + request = event["request"]["request"] |
| 52 | + |
| 53 | + # Check that the request data from `context_in_user_context_2` can be retrieved |
| 54 | + await bidi_session.network.get_data(request=request, data_type="response") |
| 55 | + |
| 56 | + # Create a new context in the default user context. |
| 57 | + context_in_default_context = await bidi_session.browsing_context.create( |
| 58 | + type_hint="tab" |
| 59 | + ) |
| 60 | + await setup_network_test( |
| 61 | + events=[RESPONSE_COMPLETED_EVENT], context=context_in_default_context["context"] |
| 62 | + ) |
| 63 | + |
| 64 | + # Trigger a request in `context_in_default_context` |
| 65 | + on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT) |
| 66 | + await fetch(url(PAGE_EMPTY_TEXT), context=context_in_default_context) |
| 67 | + event = await wait_for_future_safe(on_response_completed) |
| 68 | + request = event["request"]["request"] |
| 69 | + |
| 70 | + # Check that the request data from `context_in_default_context` can NOT be retrieved |
| 71 | + with pytest.raises(error.NoSuchNetworkDataException): |
| 72 | + await bidi_session.network.get_data(request=request, data_type="response") |
| 73 | + |
| 74 | + |
| 75 | +async def test_multiple_user_context( |
| 76 | + bidi_session, |
| 77 | + add_data_collector, |
| 78 | + create_user_context, |
| 79 | + setup_network_test, |
| 80 | + wait_for_event, |
| 81 | + wait_for_future_safe, |
| 82 | + url, |
| 83 | + fetch, |
| 84 | +): |
| 85 | + # Setup a user context and create a tab in it. |
| 86 | + user_context_a = await create_user_context() |
| 87 | + context_in_user_context_a = await bidi_session.browsing_context.create( |
| 88 | + user_context=user_context_a, type_hint="tab" |
| 89 | + ) |
| 90 | + await setup_network_test( |
| 91 | + events=[RESPONSE_COMPLETED_EVENT], context=context_in_user_context_a["context"] |
| 92 | + ) |
| 93 | + |
| 94 | + # Setup another user context and create a tab in it. |
| 95 | + user_context_b = await create_user_context() |
| 96 | + context_in_user_context_b = await bidi_session.browsing_context.create( |
| 97 | + user_context=user_context_b, type_hint="tab" |
| 98 | + ) |
| 99 | + await setup_network_test( |
| 100 | + events=[RESPONSE_COMPLETED_EVENT], context=context_in_user_context_b["context"] |
| 101 | + ) |
| 102 | + |
| 103 | + # Add a collector for both user contexts |
| 104 | + collector = await add_data_collector(user_contexts=[user_context_a, user_context_b]) |
| 105 | + |
| 106 | + # Trigger a request in `context_in_user_context_a` |
| 107 | + on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT) |
| 108 | + await fetch(url(PAGE_EMPTY_TEXT), context=context_in_user_context_a) |
| 109 | + event = await wait_for_future_safe(on_response_completed) |
| 110 | + request = event["request"]["request"] |
| 111 | + |
| 112 | + # Check that the request data from `context_in_user_context_a` can be retrieved |
| 113 | + await bidi_session.network.get_data(request=request, data_type="response") |
| 114 | + |
| 115 | + # Trigger a request in `context_in_user_context_b` |
| 116 | + on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT) |
| 117 | + await fetch(url(PAGE_EMPTY_TEXT), context=context_in_user_context_b) |
| 118 | + event = await wait_for_future_safe(on_response_completed) |
| 119 | + request = event["request"]["request"] |
| 120 | + |
| 121 | + # Check that the request data from `context_in_user_context_b` can be retrieved |
| 122 | + await bidi_session.network.get_data(request=request, data_type="response") |
| 123 | + |
| 124 | + # Create a new context in the default user context. |
| 125 | + context_in_default_context = await bidi_session.browsing_context.create( |
| 126 | + type_hint="tab" |
| 127 | + ) |
| 128 | + await setup_network_test( |
| 129 | + events=[RESPONSE_COMPLETED_EVENT], context=context_in_default_context["context"] |
| 130 | + ) |
| 131 | + |
| 132 | + # Trigger a request in `context_in_default_context` |
| 133 | + on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT) |
| 134 | + await fetch(url(PAGE_EMPTY_TEXT), context=context_in_default_context) |
| 135 | + event = await wait_for_future_safe(on_response_completed) |
| 136 | + request = event["request"]["request"] |
| 137 | + |
| 138 | + # Check that the request data from `context_in_default_context` can NOT be retrieved |
| 139 | + with pytest.raises(error.NoSuchNetworkDataException): |
| 140 | + await bidi_session.network.get_data(request=request, data_type="response") |
0 commit comments