From b68f17fd1f36377940c4212eae4585788d8caa55 Mon Sep 17 00:00:00 2001 From: Dhananjay Shah Date: Tue, 14 Jan 2025 17:30:09 +0100 Subject: [PATCH] gui-e2e: initialize 'user-agent' for specific test only. `user-agent` depends on the browser being used by a user. Some sites like 'ideas.checkmk.com' requires a reliable 'user-agent' for accessing the portal. 'user-agent' was being configured at a browser level, which affects all the tests (unideal). Thus, limiting the usage of 'user-agent' to only the required test-case. CMK-21050 Change-Id: Ie02621e8c4023fd273d1001204bae81e1a8e89b4 --- tests/gui_e2e/test_menu_help.py | 99 +++++++++++++++++++++++------- tests/testlib/playwright/plugin.py | 6 +- 2 files changed, 78 insertions(+), 27 deletions(-) diff --git a/tests/gui_e2e/test_menu_help.py b/tests/gui_e2e/test_menu_help.py index 6e5756e7a8f..5b6197f2345 100644 --- a/tests/gui_e2e/test_menu_help.py +++ b/tests/gui_e2e/test_menu_help.py @@ -3,62 +3,117 @@ # This file is part of Checkmk (https://checkmk.com). It is subject to the terms and # conditions defined in the file COPYING, which is part of this source code package. import re +from dataclasses import dataclass +from typing import Iterator import pytest +from playwright.sync_api import Page, Request, Route from tests.testlib.playwright.pom.dashboard import Dashboard from tests.testlib.playwright.timeouts import handle_playwright_timeouterror from tests.testlib.pytest_helpers.marks import skip_if_not_saas_edition -@pytest.mark.parametrize( - "help_menu_button, url_pattern", - [ - pytest.param("help_beginners_guide", "docs.checkmk.com", id="beginners_guide"), - pytest.param("help_user_manual", "docs.checkmk.com", id="user_manual"), - pytest.param("help_video_tutorials", "consent.youtube.com", id="video_tutorials"), - pytest.param("help_community_forum", "forum.checkmk.com", id="community_forum"), - pytest.param("help_plugin_api_intro", "docs.checkmk.com", id="plugin_api_intro"), - pytest.param("help_plugin_api_docs", "/check_mk/plugin-api/", id="plugin_api_docs"), - pytest.param("help_rest_api_intro", "docs.checkmk.com", id="rest_api_intro"), - pytest.param("help_rest_api_docs", "/check_mk/api/doc/", id="rest_api_docs"), - pytest.param("help_rest_api_gui", "/check_mk/api/.*/ui/", id="rest_api_gui"), +@dataclass +class HelpMenuButton: + def __init__(self, name: str, url_pattern: str) -> None: + self.name = name + self.url_pattern = url_pattern + + +@pytest.fixture( + name="help_menu_button", + params=[ + pytest.param( + HelpMenuButton("help_beginners_guide", "docs.checkmk.com"), + id="beginners_guide", + ), + pytest.param( + HelpMenuButton("help_user_manual", "docs.checkmk.com"), + id="user_manual", + ), + pytest.param( + HelpMenuButton("help_video_tutorials", "consent.youtube.com"), + id="video_tutorials", + ), + pytest.param( + HelpMenuButton("help_community_forum", "forum.checkmk.com"), + id="community_forum", + ), + pytest.param( + HelpMenuButton("help_plugin_api_intro", "docs.checkmk.com"), + id="plugin_api_intro", + ), pytest.param( - "help_saas_status_page", - "status.checkmk.com", + HelpMenuButton("help_plugin_api_docs", "/check_mk/plugin-api/"), + id="plugin_api_docs", + ), + pytest.param( + HelpMenuButton("help_rest_api_intro", "docs.checkmk.com"), + id="rest_api_intro", + ), + pytest.param( + HelpMenuButton("help_rest_api_docs", "/check_mk/api/doc/"), + id="rest_api_docs", + ), + pytest.param( + HelpMenuButton("help_rest_api_gui", "/check_mk/api/.*/ui/"), + id="rest_api_gui", + ), + pytest.param( + HelpMenuButton("help_saas_status_page", "status.checkmk.com"), id="saas_status_page", marks=skip_if_not_saas_edition, ), pytest.param( - "help_suggest_product_improvement", - "ideas.checkmk.com", + HelpMenuButton("help_suggest_product_improvement", "ideas.checkmk.com"), id="suggest_product_improvement", ), ], ) +def fixture_help_menu_button( + request: pytest.FixtureRequest, page: Page +) -> Iterator[HelpMenuButton]: + def update_user_agent(route: Route, request: Request) -> None: + headers = request.headers + headers["user-agent"] = ( + "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0" + ) + route.continue_(headers=headers) + + help_menu_button: HelpMenuButton = request.param + browser_context = page.context + if "ideas" in help_menu_button.url_pattern: + # "ideas.checkmk.com" requires a reliable user_agent to be initialized, + # not an automated one. + browser_context.route(re.compile(help_menu_button.url_pattern), update_user_agent) + yield help_menu_button + browser_context.unroute_all() + + def test_help_menu( dashboard_page: Dashboard, - help_menu_button: str, - url_pattern: str, + help_menu_button: HelpMenuButton, ) -> None: browser_context = dashboard_page.page.context pw_timeout_msg = ( - f"Expected a response to URL with pattern: `{url_pattern}`.\n" + f"Expected a response to URL with pattern: `{help_menu_button.url_pattern}`.\n" "None intercepted within the validation period!" ) with browser_context.expect_page() as new_tab_info: with browser_context.expect_event( - "response", lambda response: bool(re.findall(url_pattern, response.url)) + "response", + lambda response: bool(re.findall(help_menu_button.url_pattern, response.url)), ) as matched_response: - getattr(dashboard_page.main_menu, help_menu_button).click() + getattr(dashboard_page.main_menu, help_menu_button.name).click() with handle_playwright_timeouterror(pw_timeout_msg): assert matched_response.value.status in (200, 301), ( f"Unexpected response status code: {matched_response.value.status}, " f"response url: {matched_response.value.url}" ) new_page = new_tab_info.value - new_page.wait_for_url(url=re.compile(url_pattern), wait_until="load") + new_page.wait_for_url(url=re.compile(help_menu_button.url_pattern), wait_until="load") new_page.close() diff --git a/tests/testlib/playwright/plugin.py b/tests/testlib/playwright/plugin.py index 88db21665c5..2695b5c316f 100644 --- a/tests/testlib/playwright/plugin.py +++ b/tests/testlib/playwright/plugin.py @@ -100,11 +100,7 @@ def fixture_context_launch_kwargs(pytestconfig: pytest.Config) -> dict[str, t.An .. _BrowserContext: https://playwright.dev/python/docs/api/class-browsercontext """ - kwargs = { - "locale": pytestconfig.getoption("--locale"), - "user_agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:133.0) Gecko/20100101 " - "Firefox/133.0", # Test test_help_menu.suggest_product_improvement needs it - } + kwargs = {"locale": pytestconfig.getoption("--locale")} if pytestconfig.getoption("--video"): kwargs["record_video_dir"] = str(pytestconfig.getoption("--output")) kwargs["record_video_size"] = {"width": 1280, "height": 960}