Skip to content

Commit

Permalink
Added route interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokhilenko committed Jun 23, 2024
1 parent ef17d42 commit 07bab49
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
46 changes: 46 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Generator
from urllib.parse import urlparse, parse_qs

from dotenv import load_dotenv

import os
Expand Down Expand Up @@ -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
21 changes: 20 additions & 1 deletion tests/home/test_home_page.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)

0 comments on commit 07bab49

Please sign in to comment.