同步完整源码 - 2026-05-24

This commit is contained in:
xiaoxue
2026-05-24 23:05:48 +08:00
commit 21ef9025a7
425 changed files with 109915 additions and 0 deletions
@@ -0,0 +1,51 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import TODO_ITEMS, create_default_todos
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
create_default_todos(page)
# run the actual test
yield
# run any cleanup code
def test_should_display_the_correct_text(page: Page) -> None:
page.locator(".todo-list li .toggle").first.check()
expect(page.locator(".clear-completed")).to_have_text("Clear completed")
def test_should_clear_completed_items_when_clicked(page: Page) -> None:
todo_items = page.locator(".todo-list li")
todo_items.nth(1).locator(".toggle").check()
page.locator(".clear-completed").click()
expect(todo_items).to_have_count(2)
expect(todo_items).to_have_text([TODO_ITEMS[0], TODO_ITEMS[2]])
def test_should_be_hidden_when_there_are_no_items_that_are_completed(
page: Page,
) -> None:
page.locator(".todo-list li .toggle").first.check()
page.locator(".clear-completed").click()
expect(page.locator(".clear-completed")).to_be_hidden()
+41
View File
@@ -0,0 +1,41 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import TODO_ITEMS, assert_number_of_todos_in_local_storage
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
# run the actual test
yield
# run any cleanup code
def test_should_display_the_current_number_of_todo_items(page: Page) -> None:
page.locator(".new-todo").fill(TODO_ITEMS[0])
page.locator(".new-todo").press("Enter")
expect(page.locator(".todo-count")).to_contain_text("1")
page.locator(".new-todo").fill(TODO_ITEMS[1])
page.locator(".new-todo").press("Enter")
expect(page.locator(".todo-count")).to_contain_text("2")
assert_number_of_todos_in_local_storage(page, 2)
+97
View File
@@ -0,0 +1,97 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import (
TODO_ITEMS,
assert_number_of_todos_in_local_storage,
check_todos_in_local_storage,
create_default_todos,
)
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
create_default_todos(page)
assert_number_of_todos_in_local_storage(page, 3)
# run the actual test
yield
# run any cleanup code
def test_should_hide_other_controls_when_editing(page: Page) -> None:
todo_item = page.locator(".todo-list li").nth(1)
todo_item.dblclick()
expect(todo_item.locator(".toggle")).not_to_be_visible()
expect(todo_item.locator("label")).not_to_be_visible()
assert_number_of_todos_in_local_storage(page, 3)
def test_should_save_edits_on_blur(page: Page) -> None:
todo_items = page.locator(".todo-list li")
todo_items.nth(1).dblclick()
todo_items.nth(1).locator(".edit").fill("buy some sausages")
todo_items.nth(1).locator(".edit").dispatch_event("blur")
expect(todo_items).to_have_text(
[
TODO_ITEMS[0],
"buy some sausages",
TODO_ITEMS[2],
]
)
check_todos_in_local_storage(page, "buy some sausages")
def test_should_trim_entered_text(page: Page) -> None:
todo_items = page.locator(".todo-list li")
todo_items.nth(1).dblclick()
todo_items.nth(1).locator(".edit").fill(" buy some sausages ")
todo_items.nth(1).locator(".edit").press("Enter")
expect(todo_items).to_have_text(
[
TODO_ITEMS[0],
"buy some sausages",
TODO_ITEMS[2],
]
)
check_todos_in_local_storage(page, "buy some sausages")
def test_should_remove_the_item_if_an_empty_text_string_was_entered(page: Page) -> None:
todo_items = page.locator(".todo-list li")
todo_items.nth(1).dblclick()
todo_items.nth(1).locator(".edit").fill("")
todo_items.nth(1).locator(".edit").press("Enter")
expect(todo_items).to_have_text(
[
TODO_ITEMS[0],
TODO_ITEMS[2],
]
)
def test_should_cancel_edits_on_escape(page: Page) -> None:
todo_items = page.locator(".todo-list li")
todo_items.nth(1).dblclick()
todo_items.nth(1).locator(".edit").press("Escape")
expect(todo_items).to_have_text(TODO_ITEMS)
+89
View File
@@ -0,0 +1,89 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import (
TODO_ITEMS,
check_number_of_completed_todos_in_local_storage,
check_todos_in_local_storage,
create_default_todos,
)
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
# run the actual test
yield
# run any cleanup code
def test_should_allow_me_to_mark_items_as_completed(page: Page) -> None:
# Create two items.
for item in TODO_ITEMS[:2]:
page.locator(".new-todo").fill(item)
page.locator(".new-todo").press("Enter")
# Check first item.
firstTodo = page.locator(".todo-list li").nth(0)
firstTodo.locator(".toggle").check()
expect(firstTodo).to_have_class("completed")
# Check second item.
secondTodo = page.locator(".todo-list li").nth(1)
expect(secondTodo).not_to_have_class("completed")
secondTodo.locator(".toggle").check()
# Assert completed class.
expect(firstTodo).to_have_class("completed")
expect(secondTodo).to_have_class("completed")
def test_should_allow_me_to_un_mark_items_as_completed(page: Page) -> None:
# Create two items.
for item in TODO_ITEMS[:2]:
page.locator(".new-todo").fill(item)
page.locator(".new-todo").press("Enter")
firstTodo = page.locator(".todo-list li").nth(0)
secondTodo = page.locator(".todo-list li").nth(1)
firstTodo.locator(".toggle").check()
expect(firstTodo).to_have_class("completed")
expect(secondTodo).not_to_have_class("completed")
check_number_of_completed_todos_in_local_storage(page, 1)
firstTodo.locator(".toggle").uncheck()
expect(firstTodo).not_to_have_class("completed")
expect(secondTodo).not_to_have_class("completed")
check_number_of_completed_todos_in_local_storage(page, 0)
def test_should_allow_me_to_edit_an_item(page: Page) -> None:
create_default_todos(page)
todo_items = page.locator(".todo-list li")
secondTodo = todo_items.nth(1)
secondTodo.dblclick()
expect(secondTodo.locator(".edit")).to_have_value(TODO_ITEMS[1])
secondTodo.locator(".edit").fill("buy some sausages")
secondTodo.locator(".edit").press("Enter")
# Explicitly assert the new text value.
expect(todo_items).to_have_text([TODO_ITEMS[0], "buy some sausages", TODO_ITEMS[2]])
check_todos_in_local_storage(page, "buy some sausages")
@@ -0,0 +1,84 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import (
assert_number_of_todos_in_local_storage,
check_number_of_completed_todos_in_local_storage,
create_default_todos,
)
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
# run the actual test
yield
# run any cleanup code
def test_should_allow_me_to_mark_all_items_as_completed(page: Page) -> None:
create_default_todos(page)
assert_number_of_todos_in_local_storage(page, 3)
# Complete all todos.
page.locator(".toggle-all").check()
# Ensure all todos have 'completed' class.
expect(page.locator(".todo-list li")).to_have_class(
["completed", "completed", "completed"]
)
check_number_of_completed_todos_in_local_storage(page, 3)
assert_number_of_todos_in_local_storage(page, 3)
def test_should_allow_me_to_clear_the_complete_state_of_all_items(page: Page) -> None:
create_default_todos(page)
assert_number_of_todos_in_local_storage(page, 3)
# Check and then immediately uncheck.
page.locator(".toggle-all").check()
page.locator(".toggle-all").uncheck()
# Should be no completed classes.
expect(page.locator(".todo-list li")).to_have_class(["", "", ""])
assert_number_of_todos_in_local_storage(page, 3)
def test_complete_all_checkbox_should_update_state_when_items_are_completed_or_cleared(
page: Page,
) -> None:
create_default_todos(page)
assert_number_of_todos_in_local_storage(page, 3)
toggleAll = page.locator(".toggle-all")
toggleAll.check()
expect(toggleAll).to_be_checked()
check_number_of_completed_todos_in_local_storage(page, 3)
# Uncheck first todo.
firstTodo = page.locator(".todo-list li").nth(0)
firstTodo.locator(".toggle").uncheck()
# Reuse toggleAll locator and make sure its not checked.
expect(toggleAll).not_to_be_checked()
firstTodo.locator(".toggle").check()
check_number_of_completed_todos_in_local_storage(page, 3)
# Assert the toggle all is checked again.
expect(toggleAll).to_be_checked()
assert_number_of_todos_in_local_storage(page, 3)
@@ -0,0 +1,89 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import (
TODO_ITEMS,
assert_number_of_todos_in_local_storage,
create_default_todos,
)
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
# run the actual test
yield
# run any cleanup code
def test_new_todo_test_should_allow_me_to_add_todo_items(page: Page) -> None:
# Create 1st todo.
page.locator(".new-todo").fill(TODO_ITEMS[0])
page.locator(".new-todo").press("Enter")
# Make sure the list only has one todo item.
expect(page.locator(".view label")).to_have_text([TODO_ITEMS[0]])
# Create 2nd todo.
page.locator(".new-todo").fill(TODO_ITEMS[1])
page.locator(".new-todo").press("Enter")
# Make sure the list now has two todo items.
expect(page.locator(".view label")).to_have_text([TODO_ITEMS[0], TODO_ITEMS[1]])
assert_number_of_todos_in_local_storage(page, 2)
def test_new_todo_test_should_clear_text_input_field_when_an_item_is_added(
page: Page,
) -> None:
# Create one todo item.
page.locator(".new-todo").fill(TODO_ITEMS[0])
page.locator(".new-todo").press("Enter")
# Check that input is empty.
expect(page.locator(".new-todo")).to_be_empty()
assert_number_of_todos_in_local_storage(page, 1)
def test_new_todo_test_should_append_new_items_to_the_bottom_of_the_list(
page: Page,
) -> None:
# Create 3 items.
create_default_todos(page)
# Check test using different methods.
expect(page.locator(".todo-count")).to_have_text("3 items left")
expect(page.locator(".todo-count")).to_contain_text("3")
expect(page.locator(".todo-count")).to_have_text(re.compile("3"))
# Check all items in one call.
expect(page.locator(".view label")).to_have_text(TODO_ITEMS)
assert_number_of_todos_in_local_storage(page, 3)
def test_new_todo_should_show_main_and_foter_when_items_added(page: Page) -> None:
page.locator(".new-todo").fill(TODO_ITEMS[0])
page.locator(".new-todo").press("Enter")
expect(page.locator(".main")).to_be_visible()
expect(page.locator(".footer")).to_be_visible()
assert_number_of_todos_in_local_storage(page, 1)
@@ -0,0 +1,48 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import TODO_ITEMS, check_number_of_completed_todos_in_local_storage
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
# run the actual test
yield
# run any cleanup code
def test_should_persist_its_data(page: Page) -> None:
for item in TODO_ITEMS[:2]:
page.locator(".new-todo").fill(item)
page.locator(".new-todo").press("Enter")
todo_items = page.locator(".todo-list li")
todo_items.nth(0).locator(".toggle").check()
expect(todo_items).to_have_text([TODO_ITEMS[0], TODO_ITEMS[1]])
expect(todo_items).to_have_class(["completed", ""])
# Ensure there is 1 completed item.
check_number_of_completed_todos_in_local_storage(page, 1)
# Now reload.
page.reload()
expect(todo_items).to_have_text([TODO_ITEMS[0], TODO_ITEMS[1]])
expect(todo_items).to_have_class(["completed", ""])
+94
View File
@@ -0,0 +1,94 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generator
import pytest
from playwright.sync_api import Page, expect
from .utils import (
TODO_ITEMS,
check_number_of_completed_todos_in_local_storage,
check_todos_in_local_storage,
create_default_todos,
)
@pytest.fixture(autouse=True)
def run_around_tests(page: Page) -> Generator[None, None, None]:
# setup before a test
page.goto("https://demo.playwright.dev/todomvc")
create_default_todos(page)
# make sure the app had a chance to save updated todos in storage
# before navigating to a new view, otherwise the items can get lost :(
# in some frameworks like Durandal
check_todos_in_local_storage(page, TODO_ITEMS[0])
# run the actual test
yield
# run any cleanup code
def test_should_allow_me_to_display_active_item(page: Page) -> None:
page.locator(".todo-list li .toggle").nth(1).check()
check_number_of_completed_todos_in_local_storage(page, 1)
page.locator(".filters >> text=Active").click()
expect(page.locator(".todo-list li")).to_have_count(2)
expect(page.locator(".todo-list li")).to_have_text([TODO_ITEMS[0], TODO_ITEMS[2]])
def test_should_respect_the_back_button(page: Page) -> None:
page.locator(".todo-list li .toggle").nth(1).check()
check_number_of_completed_todos_in_local_storage(page, 1)
# Showing all items
page.locator(".filters >> text=All").click()
expect(page.locator(".todo-list li")).to_have_count(3)
# Showing active items
page.locator(".filters >> text=Active").click()
# Showing completed items
page.locator(".filters >> text=Completed").click()
expect(page.locator(".todo-list li")).to_have_count(1)
page.go_back()
expect(page.locator(".todo-list li")).to_have_count(2)
page.go_back()
expect(page.locator(".todo-list li")).to_have_count(3)
def test_should_allow_me_to_display_completed_items(page: Page) -> None:
page.locator(".todo-list li .toggle").nth(1).check()
check_number_of_completed_todos_in_local_storage(page, 1)
page.locator(".filters >> text=Completed").click()
expect(page.locator(".todo-list li")).to_have_count(1)
def test_should_allow_me_to_display_all_items(page: Page) -> None:
page.locator(".todo-list li .toggle").nth(1).check()
check_number_of_completed_todos_in_local_storage(page, 1)
page.locator(".filters >> text=Active").click()
page.locator(".filters >> text=Completed").click()
page.locator(".filters >> text=All").click()
expect(page.locator(".todo-list li")).to_have_count(3)
def test_should_highlight_the_current_applied_filter(page: Page) -> None:
expect(page.locator(".filters >> text=All")).to_have_class("selected")
page.locator(".filters >> text=Active").click()
# Page change - active items.
expect(page.locator(".filters >> text=Active")).to_have_class("selected")
page.locator(".filters >> text=Completed").click()
# Page change - completed items.
expect(page.locator(".filters >> text=Completed")).to_have_class("selected")
+41
View File
@@ -0,0 +1,41 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from playwright.sync_api import Page
TODO_ITEMS = ["buy some cheese", "feed the cat", "book a doctors appointment"]
def create_default_todos(page: Page) -> None:
for item in TODO_ITEMS:
page.locator(".new-todo").fill(item)
page.locator(".new-todo").press("Enter")
def check_number_of_completed_todos_in_local_storage(page: Page, expected: int) -> None:
assert (
page.evaluate(
"JSON.parse(localStorage['react-todos']).filter(i => i.completed).length"
)
== expected
)
def assert_number_of_todos_in_local_storage(page: Page, expected: int) -> None:
assert len(page.evaluate("JSON.parse(localStorage['react-todos'])")) == expected
def check_todos_in_local_storage(page: Page, title: str) -> None:
assert title in page.evaluate(
"JSON.parse(localStorage['react-todos']).map(i => i.title)"
)
+1
View File
@@ -0,0 +1 @@
pytest-playwright