|
14 | 14 | import pytest |
15 | 15 | from faker import Faker |
16 | 16 | from playwright.sync_api import APIRequestContext, BrowserContext, Page, WebSocket |
| 17 | +from playwright.sync_api._generated import Playwright |
17 | 18 | from pydantic import AnyUrl, TypeAdapter |
18 | 19 | from pytest_simcore.logging_utils import log_context |
19 | 20 | from pytest_simcore.playwright_utils import ( |
@@ -75,20 +76,27 @@ def pytest_addoption(parser: pytest.Parser) -> None: |
75 | 76 | default=None, |
76 | 77 | help="Service Key", |
77 | 78 | ) |
| 79 | + group.addoption( |
| 80 | + "--user-agent", |
| 81 | + action="store", |
| 82 | + type=str, |
| 83 | + default="e2e-playwright", |
| 84 | + help="defines a specific user agent osparc header", |
| 85 | + ) |
78 | 86 |
|
79 | 87 |
|
80 | 88 | @pytest.fixture(autouse=True) |
81 | | -def osparc_test_id_attribute(playwright): |
| 89 | +def osparc_test_id_attribute(playwright: Playwright) -> None: |
82 | 90 | # Set a custom test id attribute |
83 | 91 | playwright.selectors.set_test_id_attribute("osparc-test-id") |
84 | 92 |
|
85 | 93 |
|
86 | 94 | @pytest.fixture |
87 | | -def api_request_context(context: BrowserContext): |
| 95 | +def api_request_context(context: BrowserContext) -> APIRequestContext: |
88 | 96 | return context.request |
89 | 97 |
|
90 | 98 |
|
91 | | -@pytest.fixture |
| 99 | +@pytest.fixture(scope="session") |
92 | 100 | def product_url(request: pytest.FixtureRequest) -> AnyUrl: |
93 | 101 | if passed_product_url := request.config.getoption("--product-url"): |
94 | 102 | return TypeAdapter(AnyUrl).validate_python(passed_product_url) |
@@ -118,33 +126,49 @@ def user_password( |
118 | 126 | return os.environ["USER_PASSWORD"] |
119 | 127 |
|
120 | 128 |
|
121 | | -@pytest.fixture |
| 129 | +@pytest.fixture(scope="session") |
122 | 130 | def product_billable(request: pytest.FixtureRequest) -> bool: |
123 | 131 | billable = request.config.getoption("--product-billable") |
124 | 132 | return TypeAdapter(bool).validate_python(billable) |
125 | 133 |
|
126 | 134 |
|
127 | | -@pytest.fixture |
| 135 | +@pytest.fixture(scope="session") |
128 | 136 | def service_test_id(request: pytest.FixtureRequest) -> str: |
129 | 137 | if test_id := request.config.getoption("--service-test-id"): |
130 | 138 | assert isinstance(test_id, str) |
131 | 139 | return test_id |
132 | 140 | return os.environ["SERVICE_TEST_ID"] |
133 | 141 |
|
134 | 142 |
|
135 | | -@pytest.fixture |
| 143 | +@pytest.fixture(scope="session") |
136 | 144 | def service_key(request: pytest.FixtureRequest) -> str: |
137 | 145 | if key := request.config.getoption("--service-key"): |
138 | 146 | assert isinstance(key, str) |
139 | 147 | return key |
140 | 148 | return os.environ["SERVICE_KEY"] |
141 | 149 |
|
142 | 150 |
|
143 | | -@pytest.fixture |
| 151 | +@pytest.fixture(scope="session") |
144 | 152 | def auto_register(request: pytest.FixtureRequest) -> bool: |
145 | 153 | return bool(request.config.getoption("--autoregister")) |
146 | 154 |
|
147 | 155 |
|
| 156 | +@pytest.fixture(scope="session") |
| 157 | +def user_agent(request: pytest.FixtureRequest) -> str: |
| 158 | + return str(request.config.getoption("--user-agent")) |
| 159 | + |
| 160 | + |
| 161 | +@pytest.fixture(scope="session") |
| 162 | +def browser_context_args( |
| 163 | + browser_context_args: dict[str, dict[str, str]], user_agent: str |
| 164 | +) -> dict[str, dict[str, str]]: |
| 165 | + # Override browser context options, see https://playwright.dev/python/docs/test-runners#fixtures |
| 166 | + return { |
| 167 | + **browser_context_args, |
| 168 | + "extra_http_headers": {"X-Simcore-User-Agent": user_agent}, |
| 169 | + } |
| 170 | + |
| 171 | + |
148 | 172 | @pytest.fixture |
149 | 173 | def register( |
150 | 174 | page: Page, |
|
0 commit comments