Repository navigation
Expand file tree
/
Copy pathtest_example.py
More file actions
117 lines (87 loc) · 4.26 KB
/
Copy pathtest_example.py
File metadata and controls
117 lines (87 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from pathlib import Path
import pytest
from django.core.management import call_command
from django.urls import reverse
from playwright.sync_api import Page, expect
def test_counter(live_server, page: Page):
page.set_default_timeout(5_000)
page.goto(str(live_server))
message_content = page.get_by_test_id("message-content")
initialized_from = page.get_by_test_id("message-initialized-from")
expect(initialized_from).to_have_text("127.0.0.1")
expect(message_content).to_have_text("( no message )")
# Click on the first "+" button.
page.get_by_role("button", name="+").first.click()
expect(message_content).to_have_text(
"Counter 'First counter' incremented by 1. Its new value is now 1001."
)
expect(page.get_by_role("textbox").first).to_have_value("€ 1,001")
# Click on the first "-" button.
page.get_by_role("button", name="-").first.click()
expect(message_content).to_have_text(
"Counter 'First counter' incremented by -1. Its new value is now 1000."
)
expect(page.get_by_role("textbox").first).to_have_value("€ 1,000")
# Click on the second "+" button.
page.get_by_role("button", name="+").nth(1).click()
expect(message_content).to_have_text(
"Counter 'Second counter' incremented by 1. Its new value is now 1001."
)
expect(page.get_by_role("textbox").nth(1)).to_have_value("€ 1,001")
@pytest.mark.django_db
def test_coffee(live_server, page: Page):
call_command("load_coffee_beans")
coffee_url = str(live_server) + reverse("coffee")
page.set_default_timeout(5_000)
page.goto(coffee_url)
# Search by arabica and assert that filter works
page.get_by_role("textbox").click()
page.get_by_role("textbox").press_sequentially("arabica")
expect(page.get_by_test_id("coffee-row")).to_have_count(1)
# Click on the "Edit" button and assert that the inline form is shown
page.get_by_test_id("coffee-edit-button").click()
expect(page.get_by_test_id("coffee-edit-form")).to_be_visible()
# Set the stock quantity to 101, save the form, and assert the form is closed
# and the value is updated
page.locator("#id_stock_quantity").fill("101")
page.get_by_test_id("coffee-edit-form-save").click()
expect(page.get_by_test_id("coffee-edit-form")).to_be_hidden()
expect(page.get_by_test_id("coffee-stock-quantity")).to_have_text("101")
# Click on the "Delete" button and assert the record is deleted.
page.on("dialog", lambda dialog: dialog.accept())
page.get_by_test_id("coffee-delete-button").click()
expect(page.get_by_test_id("coffee-row")).to_have_count(0)
@pytest.mark.django_db
def test_modals(live_server, page: Page):
modals_url = str(live_server) + reverse("modals")
page.set_default_timeout(5_000)
page.goto(modals_url)
# Click on the "Send email" link and see that the modal is shown
# and has text populated by the context.
page.get_by_text("Send email").first.click()
expect(page.get_by_test_id("modal").first).to_be_visible()
expect(page.get_by_test_id("modal-title").first).to_have_text("Alice")
expect(page.get_by_test_id("modal-body").first).to_have_text(
"Send email to alice@example.com?"
)
# Click the "Close" button and see that the modal is hidden.
page.get_by_test_id("modal-close").first.click()
expect(page.get_by_test_id("modal").first).to_be_hidden()
def test_stateless_counter(live_server, page: Page):
statelesscounter_url = str(live_server) + reverse("statelesscounter")
page.set_default_timeout(5_000)
page.goto(statelesscounter_url)
count = page.locator("div", has_text="Count:").last
expect(count).to_contain_text("Count: 0")
page.get_by_role("button", name="+1").click()
expect(count).to_contain_text("Count: 1")
page.get_by_role("button", name="-1").click()
expect(count).to_contain_text("Count: 0")
def test_uploads(live_server, page: Page):
uploads_url = str(live_server) + reverse("uploads")
page.set_default_timeout(5000)
page.goto(uploads_url)
coffee_csv = Path(__file__).parents[1] / "example" / "coffee.csv"
page.set_input_files("input[name=csv_file]", coffee_csv)
page.get_by_role("button", name="Upload").click()
expect(page.get_by_test_id("filename")).to_have_text("coffee.csv")