Skip to content

Commit 1afde56

Browse files
committed
Use a deterministic way of waiting for elements in npm tests
1 parent 623596d commit 1afde56

File tree

1 file changed

+57
-63
lines changed

1 file changed

+57
-63
lines changed

tests/test_reactjs/test_modules_from_npm.py

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from playwright.async_api import expect
23

34
import reactpy
45
from reactpy import html
@@ -24,14 +25,13 @@ def App():
2425

2526
await display.show(App)
2627

27-
button = await display.page.wait_for_selector("#test-button")
28-
assert await button.inner_text() == "Click me"
28+
button = display.page.locator("#test-button")
29+
await expect(button).to_have_text("Click me")
2930

3031
# Check if it has the correct class for primary variant
3132
# React Bootstrap buttons usually have 'btn' and 'btn-primary' classes
32-
classes = await button.get_attribute("class")
33-
assert "btn" in classes
34-
assert "btn-primary" in classes
33+
await expect(button).to_contain_class("btn")
34+
await expect(button).to_contain_class("btn-primary")
3535

3636

3737
@pytest.mark.flaky(reruns=3)
@@ -48,14 +48,13 @@ def App():
4848
async with DisplayFixture(backend=backend, browser=browser) as display:
4949
await display.show(App)
5050

51-
button = await display.page.wait_for_selector("#test-button")
52-
assert await button.inner_text() == "Click me"
51+
button = display.page.locator("#test-button")
52+
await expect(button).to_have_text("Click me")
5353

5454
# Check if it has the correct class for primary variant
5555
# React Bootstrap buttons usually have 'btn' and 'btn-primary' classes
56-
classes = await button.get_attribute("class")
57-
assert "btn" in classes
58-
assert "btn-primary" in classes
56+
await expect(button).to_contain_class("btn")
57+
await expect(button).to_contain_class("btn-primary")
5958

6059

6160
@pytest.mark.flaky(reruns=3)
@@ -67,11 +66,11 @@ def App():
6766
return Button({"variant": "contained", "id": "test-button"}, "Click me")
6867

6968
await display.show(App)
70-
button = await display.page.wait_for_selector("#test-button")
69+
button = display.page.locator("#test-button")
7170
# Material UI transforms text to uppercase by default
72-
assert await button.inner_text() == "CLICK ME"
73-
classes = await button.get_attribute("class")
74-
assert "MuiButton-root" in classes
71+
await expect(button).to_have_text("Click me")
72+
await expect(button).to_have_css("text-transform", "uppercase")
73+
await expect(button).to_contain_class("MuiButton-root")
7574

7675

7776
@pytest.mark.flaky(reruns=3)
@@ -83,10 +82,9 @@ def App():
8382
return Button({"type": "primary", "id": "test-button"}, "Click me")
8483

8584
await display.show(App)
86-
button = await display.page.wait_for_selector("#test-button")
87-
assert "Click me" in await button.inner_text()
88-
classes = await button.get_attribute("class")
89-
assert "ant-btn" in classes
85+
button = display.page.locator("#test-button")
86+
await expect(button).to_have_text("Click me")
87+
await expect(button).to_contain_class("ant-btn")
9088

9189

9290
@pytest.mark.flaky(reruns=3)
@@ -100,10 +98,9 @@ def App():
10098
)
10199

102100
await display.show(App)
103-
button = await display.page.wait_for_selector("#test-button")
104-
assert await button.inner_text() == "Click me"
105-
classes = await button.get_attribute("class")
106-
assert "chakra-button" in classes
101+
button = display.page.locator("#test-button")
102+
await expect(button).to_have_text("Click me")
103+
await expect(button).to_contain_class("chakra-button")
107104

108105

109106
@pytest.mark.flaky(reruns=3)
@@ -121,11 +118,10 @@ def App():
121118
async with BackendFixture(html_head=html.head(import_reactjs("preact"))) as backend:
122119
async with DisplayFixture(backend=backend, browser=browser) as display:
123120
await display.show(App)
124-
button = await display.page.wait_for_selector("#test-button")
125-
assert await button.inner_text() == "Click me"
126-
classes = await button.get_attribute("class")
127-
assert "ui" in classes
128-
assert "button" in classes
121+
button = display.page.locator("#test-button")
122+
await expect(button).to_have_text("Click me")
123+
await expect(button).to_contain_class("ui")
124+
await expect(button).to_contain_class("button")
129125

130126

131127
@pytest.mark.flaky(reruns=3)
@@ -139,10 +135,9 @@ def App():
139135
return MantineProvider(Button({"id": "test-button"}, "Click me"))
140136

141137
await display.show(App)
142-
button = await display.page.wait_for_selector("#test-button")
143-
assert await button.inner_text() == "Click me"
144-
classes = await button.get_attribute("class")
145-
assert "mantine-Button-root" in classes
138+
button = display.page.locator("#test-button")
139+
await expect(button).to_have_text("Click me")
140+
await expect(button).to_contain_class("mantine-Button-root")
146141

147142

148143
@pytest.mark.flaky(reruns=3)
@@ -154,10 +149,9 @@ def App():
154149
return PrimaryButton({"id": "test-button"}, "Click me")
155150

156151
await display.show(App)
157-
button = await display.page.wait_for_selector("#test-button")
158-
assert await button.inner_text() == "Click me"
159-
classes = await button.get_attribute("class")
160-
assert "ms-Button" in classes
152+
button = display.page.locator("#test-button")
153+
await expect(button).to_have_text("Click me")
154+
await expect(button).to_contain_class("ms-Button")
161155

162156

163157
@pytest.mark.flaky(reruns=3)
@@ -169,10 +163,9 @@ def App():
169163
return Button({"intent": "primary", "id": "test-button"}, "Click me")
170164

171165
await display.show(App)
172-
button = await display.page.wait_for_selector("#test-button")
173-
assert await button.inner_text() == "Click me"
174-
classes = await button.get_attribute("class")
175-
assert any(c.startswith("bp") and "button" in c for c in classes.split())
166+
button = display.page.locator("#test-button")
167+
await expect(button).to_have_text("Click me")
168+
await expect(button).to_contain_class("bp6-button")
176169

177170

178171
@pytest.mark.flaky(reruns=3)
@@ -186,8 +179,8 @@ def App():
186179
)
187180

188181
await display.show(App)
189-
button = await display.page.wait_for_selector("#test-button")
190-
assert await button.inner_text() == "Click me"
182+
button = display.page.locator("#test-button")
183+
await expect(button).to_have_text("Click me")
191184

192185

193186
@pytest.mark.flaky(reruns=3)
@@ -199,8 +192,8 @@ def App():
199192
return Button({"appearance": "primary", "id": "test-button"}, "Click me")
200193

201194
await display.show(App)
202-
button = await display.page.wait_for_selector("#test-button")
203-
assert await button.inner_text() == "Click me"
195+
button = display.page.locator("#test-button")
196+
await expect(button).to_have_text("Click me")
204197

205198

206199
@pytest.mark.flaky(reruns=3)
@@ -222,8 +215,8 @@ def App():
222215
# react-spinners renders a span with the loader
223216
# We can check if it exists. It might not have an ID we can easily set on the root if it doesn't forward props well,
224217
# but let's try wrapping it.
225-
loader = await display.page.wait_for_selector("span[data-testid='loader']")
226-
assert await loader.is_visible()
218+
loader = display.page.locator("span[data-testid='loader']")
219+
await expect(loader).to_be_visible()
227220

228221

229222
@pytest.mark.flaky(reruns=3)
@@ -250,13 +243,12 @@ def App():
250243

251244
await display.show(App)
252245

253-
box = await display.page.wait_for_selector("#chakra-box")
254-
assert await box.is_visible()
246+
box = display.page.locator("#chakra-box")
247+
await expect(box).to_be_visible()
255248

256-
button = await display.page.wait_for_selector("#bootstrap-button")
257-
assert await button.inner_text() == "Nested Button"
258-
classes = await button.get_attribute("class")
259-
assert "btn" in classes
249+
button = display.page.locator("#bootstrap-button")
250+
await expect(button).to_have_text("Nested Button")
251+
await expect(button).to_contain_class("btn")
260252

261253

262254
@pytest.mark.flaky(reruns=3)
@@ -280,15 +272,16 @@ def App():
280272

281273
await display.show(App)
282274

283-
card = await display.page.wait_for_selector("#antd-card")
284-
assert await card.is_visible()
275+
card = display.page.locator("#antd-card")
276+
await expect(card).to_be_visible()
285277

286-
server_div = await display.page.wait_for_selector("#server-div")
287-
assert await server_div.is_visible()
288-
assert "Server Side Div" in await server_div.inner_text()
278+
server_div = display.page.locator("#server-div")
279+
await expect(server_div).to_be_visible()
280+
await expect(server_div).to_contain_text("Server Side Div")
289281

290-
button = await display.page.wait_for_selector("#mui-button")
291-
assert "MUI BUTTON" in await button.inner_text() # MUI capitalizes
282+
button = display.page.locator("#mui-button")
283+
await expect(button).to_contain_text("MUI Button") # MUI capitalizes
284+
await expect(button).to_have_css("text-transform", "uppercase")
292285

293286

294287
@pytest.mark.flaky(reruns=3)
@@ -361,16 +354,17 @@ def App():
361354
await display.show(App)
362355

363356
# Check if the button is visible and has correct text
364-
btn = await display.page.wait_for_selector("#learn-more-btn")
365-
assert await btn.is_visible()
357+
btn = display.page.locator("#learn-more-btn")
358+
await expect(btn).to_be_visible()
366359
# Material UI transforms text to uppercase by default
367-
assert "LEARN MORE" in await btn.inner_text()
360+
await expect(btn).to_contain_text("Learn More")
361+
await expect(btn).to_have_css("text-transform", "uppercase")
368362

369363
# Check if Card is rendered (it usually has MuiCard-root class)
370364
# We can't easily select by ID as we didn't put one on Card, but we can check structure if needed.
371365
# But let's just check if the text "be-nev-o-lent" is visible
372-
text = await display.page.wait_for_selector("text=be-nev-o-lent")
373-
assert await text.is_visible()
366+
text = display.page.locator("text=be-nev-o-lent")
367+
await expect(text).to_be_visible()
374368

375369

376370
def _get_chakra_components():

0 commit comments

Comments
 (0)