Skip to content

Commit df75228

Browse files
authored
✨E2E Playwright: add extra headers for all requests (#5746)
1 parent bfcff8c commit df75228

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

tests/e2e-playwright/tests/conftest.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pytest
1515
from faker import Faker
1616
from playwright.sync_api import APIRequestContext, BrowserContext, Page, WebSocket
17+
from playwright.sync_api._generated import Playwright
1718
from pydantic import AnyUrl, TypeAdapter
1819
from pytest_simcore.logging_utils import log_context
1920
from pytest_simcore.playwright_utils import (
@@ -75,20 +76,27 @@ def pytest_addoption(parser: pytest.Parser) -> None:
7576
default=None,
7677
help="Service Key",
7778
)
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+
)
7886

7987

8088
@pytest.fixture(autouse=True)
81-
def osparc_test_id_attribute(playwright):
89+
def osparc_test_id_attribute(playwright: Playwright) -> None:
8290
# Set a custom test id attribute
8391
playwright.selectors.set_test_id_attribute("osparc-test-id")
8492

8593

8694
@pytest.fixture
87-
def api_request_context(context: BrowserContext):
95+
def api_request_context(context: BrowserContext) -> APIRequestContext:
8896
return context.request
8997

9098

91-
@pytest.fixture
99+
@pytest.fixture(scope="session")
92100
def product_url(request: pytest.FixtureRequest) -> AnyUrl:
93101
if passed_product_url := request.config.getoption("--product-url"):
94102
return TypeAdapter(AnyUrl).validate_python(passed_product_url)
@@ -118,33 +126,49 @@ def user_password(
118126
return os.environ["USER_PASSWORD"]
119127

120128

121-
@pytest.fixture
129+
@pytest.fixture(scope="session")
122130
def product_billable(request: pytest.FixtureRequest) -> bool:
123131
billable = request.config.getoption("--product-billable")
124132
return TypeAdapter(bool).validate_python(billable)
125133

126134

127-
@pytest.fixture
135+
@pytest.fixture(scope="session")
128136
def service_test_id(request: pytest.FixtureRequest) -> str:
129137
if test_id := request.config.getoption("--service-test-id"):
130138
assert isinstance(test_id, str)
131139
return test_id
132140
return os.environ["SERVICE_TEST_ID"]
133141

134142

135-
@pytest.fixture
143+
@pytest.fixture(scope="session")
136144
def service_key(request: pytest.FixtureRequest) -> str:
137145
if key := request.config.getoption("--service-key"):
138146
assert isinstance(key, str)
139147
return key
140148
return os.environ["SERVICE_KEY"]
141149

142150

143-
@pytest.fixture
151+
@pytest.fixture(scope="session")
144152
def auto_register(request: pytest.FixtureRequest) -> bool:
145153
return bool(request.config.getoption("--autoregister"))
146154

147155

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+
148172
@pytest.fixture
149173
def register(
150174
page: Page,

0 commit comments

Comments
 (0)