diff --git a/conftest.py b/conftest.py index fd08205..ebba3fc 100644 --- a/conftest.py +++ b/conftest.py @@ -1,4 +1,6 @@ from typing import Generator +from urllib.parse import urlparse, parse_qs + from dotenv import load_dotenv import os @@ -31,3 +33,47 @@ def request_context( request_context = playwright.request.new_context(base_url=base_url) yield request_context request_context.dispose() + + +@pytest.fixture(scope="session") +def browser(playwright): + browser = playwright.chromium.launch(headless=False) + yield browser + browser.close() + + +@pytest.fixture(scope="session") +def context(browser): + context = browser.new_context() + yield context + context.close() + + +@pytest.fixture +def intercept_route(page): + def setup_routes(route_pattern): + intercepted_data = { + 'query_params': {}, + 'headers': {}, + 'full_url': '', + 'resource_type': '' + } + + # Define a function to capture query parameters and handle XHR requests + def handle_route(route): + request = route.request + intercepted_data['headers'].update(request.headers) + intercepted_data['full_url'] = request.url + intercepted_data['resource_type'] = request.resource_type + + # Parse the query parameters + parsed_url = urlparse(request.url) + intercepted_data['query_params'].update(parse_qs(parsed_url.query)) + + route.continue_() + + page.route(route_pattern, handle_route) + + return intercepted_data + + return setup_routes diff --git a/tests/home/test_home_page.py b/tests/home/test_home_page.py index 703596b..380bfea 100644 --- a/tests/home/test_home_page.py +++ b/tests/home/test_home_page.py @@ -1,5 +1,8 @@ +import time +from urllib.parse import urlparse, parse_qs + import pytest -from playwright.sync_api import Page +from playwright.sync_api import Page, expect from lib.pages.home_page import HomePage @@ -8,3 +11,19 @@ def test_demo_qa_home_page_elements_should_be_visible(page: Page, base_url): home_page = HomePage(page) home_page.navigate(base_url) home_page.elements_are_visible() + + +def test_get_request_headers(page: Page, base_url, intercept_route): + route_pattern = "**/Account/v1/GenerateToken" + intercepted_data = intercept_route(route_pattern) + # page.route(, handle_route) + page.goto(f'{base_url}/books') + # Click the button that triggers the request + expect(page.locator('span.text', has_text='Login')).to_be_visible() + page.locator('span.text', has_text='Login').click() + page.locator('input#userName').type('someStuff') + page.locator('input#password').type('someStuff') + page.locator('button#login').click() + # time.sleep(3) + # query_params = setup_routes + print(intercepted_data)