@@ -147,14 +147,28 @@ async def test_assertions_locator_to_have_class(page: Page, server: Server) -> N
147147
148148async 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
160174async def test_assertions_locator_to_have_count (page : Page , server : Server ) -> None :
0 commit comments