|
| 1 | +from typing import Generator |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from playwright.sync_api import Page, expect |
| 6 | + |
| 7 | +from .utils import ( |
| 8 | + TODO_ITEMS, |
| 9 | + check_number_of_completed_todos_in_local_storage, |
| 10 | + check_todos_in_local_storage, |
| 11 | + create_default_todos, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(autouse=True) |
| 16 | +def run_around_tests(page: Page) -> Generator[None, None, None]: |
| 17 | + # setup before a test |
| 18 | + page.goto("https://demo.playwright.dev/todomvc") |
| 19 | + # run the actual test |
| 20 | + yield |
| 21 | + # run any cleanup code |
| 22 | + |
| 23 | + |
| 24 | +def test_should_allow_me_to_mark_items_as_completed(page: Page) -> None: |
| 25 | + # Create two items. |
| 26 | + for item in TODO_ITEMS[:2]: |
| 27 | + page.locator(".new-todo").fill(item) |
| 28 | + page.locator(".new-todo").press("Enter") |
| 29 | + |
| 30 | + # Check first item. |
| 31 | + firstTodo = page.locator(".todo-list li").nth(0) |
| 32 | + firstTodo.locator(".toggle").check() |
| 33 | + expect(firstTodo).to_have_class("completed") |
| 34 | + |
| 35 | + # Check second item. |
| 36 | + secondTodo = page.locator(".todo-list li").nth(1) |
| 37 | + expect(secondTodo).not_to_have_class("completed") |
| 38 | + secondTodo.locator(".toggle").check() |
| 39 | + |
| 40 | + # Assert completed class. |
| 41 | + expect(firstTodo).to_have_class("completed") |
| 42 | + expect(secondTodo).to_have_class("completed") |
| 43 | + |
| 44 | + |
| 45 | +def test_should_allow_me_to_un_mark_items_as_completed(page: Page) -> None: |
| 46 | + # Create two items. |
| 47 | + for item in TODO_ITEMS[:2]: |
| 48 | + page.locator(".new-todo").fill(item) |
| 49 | + page.locator(".new-todo").press("Enter") |
| 50 | + |
| 51 | + firstTodo = page.locator(".todo-list li").nth(0) |
| 52 | + secondTodo = page.locator(".todo-list li").nth(1) |
| 53 | + firstTodo.locator(".toggle").check() |
| 54 | + expect(firstTodo).to_have_class("completed") |
| 55 | + expect(secondTodo).not_to_have_class("completed") |
| 56 | + check_number_of_completed_todos_in_local_storage(page, 1) |
| 57 | + |
| 58 | + firstTodo.locator(".toggle").uncheck() |
| 59 | + expect(firstTodo).not_to_have_class("completed") |
| 60 | + expect(secondTodo).not_to_have_class("completed") |
| 61 | + check_number_of_completed_todos_in_local_storage(page, 0) |
| 62 | + |
| 63 | + |
| 64 | +def test_should_allow_me_to_edit_an_item(page: Page) -> None: |
| 65 | + create_default_todos(page) |
| 66 | + |
| 67 | + todo_items = page.locator(".todo-list li") |
| 68 | + secondTodo = todo_items.nth(1) |
| 69 | + secondTodo.dblclick() |
| 70 | + expect(secondTodo.locator(".edit")).to_have_value(TODO_ITEMS[1]) |
| 71 | + secondTodo.locator(".edit").fill("buy some sausages") |
| 72 | + secondTodo.locator(".edit").press("Enter") |
| 73 | + |
| 74 | + # Explicitly assert the new text value. |
| 75 | + expect(todo_items).to_have_text([TODO_ITEMS[0], "buy some sausages", TODO_ITEMS[2]]) |
| 76 | + check_todos_in_local_storage(page, "buy some sausages") |
0 commit comments