Skip to content

Commit b181ccf

Browse files
committed
Matched to_contain_class tests
1 parent 994af53 commit b181ccf

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

playwright/_impl/_assertions.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,20 @@ async def to_contain_class(
313313
expected, str
314314
):
315315
expected_text = to_expected_text_values(expected)
316+
await self._expect_impl(
317+
"to.contain.class.array",
318+
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
319+
expected,
320+
"Locator expected to contain class names",
321+
)
316322
else:
317323
expected_text = to_expected_text_values([expected])
318-
await self._expect_impl(
319-
"to.contain.class",
320-
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
321-
expected,
322-
"Locator expected to contain class",
323-
)
324+
await self._expect_impl(
325+
"to.contain.class",
326+
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
327+
expected,
328+
"Locator expected to contain class",
329+
)
324330

325331
async def not_to_contain_class(
326332
self,

tests/async/test_assertions.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,28 @@ async def test_assertions_locator_to_have_class(page: Page, server: Server) -> N
147147

148148
async def test_assertions_locator_to_contain_class(page: Page, server: Server) -> None:
149149
await page.goto(server.EMPTY_PAGE)
150-
await page.set_content("<div class='foo bar another foobar'>kek</div>")
151-
await expect(page.locator("div.foobar")).to_contain_class("foobar")
152-
await expect(page.locator("div.foobar")).to_contain_class(["another foobar"])
153-
await expect(page.locator("div.foobar")).not_to_contain_class(
154-
"kekstar", timeout=100
150+
await page.set_content("<div class='foo bar baz'></div>")
151+
locator = page.locator("div")
152+
await expect(locator).to_contain_class("")
153+
await expect(locator).to_contain_class("bar")
154+
await expect(locator).to_contain_class("baz bar")
155+
await expect(locator).to_contain_class(" bar foo ")
156+
await expect(locator).not_to_contain_class(
157+
" baz not-matching "
158+
) # Strip whitespace and match individual classes
159+
with pytest.raises(AssertionError) as excinfo:
160+
await expect(locator).to_contain_class("does-not-exist", timeout=100)
161+
162+
assert excinfo.match("Locator expected to contain class 'does-not-exist'")
163+
assert excinfo.match("Actual value: foo bar baz")
164+
assert excinfo.match("LocatorAssertions.to_contain_class with timeout 100ms")
165+
166+
await page.set_content(
167+
'<div class="foo"></div><div class="hello bar"></div><div class="baz"></div>'
155168
)
156-
with pytest.raises(AssertionError):
157-
await expect(page.locator("div.foobar")).to_contain_class("oh-no", timeout=100)
169+
await expect(locator).to_contain_class(["foo", "hello", "baz"])
170+
await expect(locator).not_to_contain_class(["not-there", "hello", "baz"])
171+
await expect(locator).not_to_contain_class(["foo", "hello"])
158172

159173

160174
async def test_assertions_locator_to_have_count(page: Page, server: Server) -> None:

0 commit comments

Comments
 (0)