53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
|
|
import pytest
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
from workers.python.ui_tests import execute_ui_test_case
|
|
from workers.python.pb import common_test_pb2 as pb
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_execute_ui_test_case_with_steps():
|
|
# 1. Setup Mocks
|
|
mock_page = AsyncMock()
|
|
mock_browser = AsyncMock()
|
|
mock_browser.new_page.return_value = mock_page
|
|
|
|
# `page.locator` is a sync method returning an object with async methods.
|
|
# So, we use MagicMock to return our AsyncMock element.
|
|
mock_element = AsyncMock()
|
|
mock_page.locator = MagicMock(return_value=mock_element)
|
|
|
|
# Patch the playwright instance and browser sessions
|
|
with patch('workers.python.ui_tests._playwright_instance', new_callable=AsyncMock) as mock_playwright:
|
|
mock_playwright.chromium.launch.return_value = mock_browser
|
|
|
|
with patch('workers.python.ui_tests._browser_sessions', new_callable=dict):
|
|
# 2. Prepare test data
|
|
steps = [
|
|
pb.UiStep(name="Click button", event_type="click", selector="#button1"),
|
|
pb.UiStep(name="Input text", event_type="input", selector="#input1", input_value="test"),
|
|
]
|
|
|
|
# 3. Call the function under test
|
|
success, log_output, _, _, _ = await execute_ui_test_case(
|
|
test_case_id="test1",
|
|
url_path="/",
|
|
browser_type="chromium",
|
|
headless=True,
|
|
steps=steps,
|
|
browser_session_id=None
|
|
)
|
|
|
|
# 4. Assertions
|
|
assert success is True, f"Test failed with logs: {log_output}"
|
|
|
|
# Assert page navigation
|
|
mock_page.goto.assert_awaited_once_with('https://playwright.dev/')
|
|
|
|
# Assert that the locator was called correctly
|
|
mock_page.locator.assert_any_call("#button1")
|
|
mock_page.locator.assert_any_call("#input1")
|
|
|
|
# Assert that the async methods on the element were awaited
|
|
mock_element.click.assert_awaited_once()
|
|
mock_element.fill.assert_awaited_once_with("test")
|