diff --git a/src/Test/Assert/PlaywrightTestAssertionsTrait.php b/src/Test/Assert/PlaywrightTestAssertionsTrait.php index c0b4adf..1948f7b 100644 --- a/src/Test/Assert/PlaywrightTestAssertionsTrait.php +++ b/src/Test/Assert/PlaywrightTestAssertionsTrait.php @@ -14,7 +14,11 @@ namespace Playwright\Symfony\Test\Assert; +use Playwright\Locator\LocatorInterface; use Playwright\Page\PageInterface; +use Playwright\Testing\Expect; +use Playwright\Testing\ExpectDecorator; +use Playwright\Testing\ExpectInterface; use Symfony\Component\HttpFoundation\Response; /** @@ -42,23 +46,22 @@ protected function assertSelectorExists(string $selector): void protected function assertSelectorNotExists(string $selector): void { - $count = $this->getPage()->locator($selector)->count(); - $this->assertSame(0, $count, "Selector '$selector' should not exist"); + $this->expectLocator($this->getPage()->locator($selector))->toHaveCount(0); } protected function assertSelectorVisible(string $selector): void { - $this->assertTrue($this->getPage()->locator($selector)->isVisible(), "Selector '$selector' is not visible"); + $this->expectLocator($this->getPage()->locator($selector))->toBeVisible(); } protected function assertSelectorHidden(string $selector): void { - $this->assertTrue($this->getPage()->locator($selector)->isHidden(), "Selector '$selector' is not hidden"); + $this->expectLocator($this->getPage()->locator($selector))->toBeHidden(); } protected function assertSelectorTextContains(string $selector, string $text): void { - $this->assertStringContainsString($text, $this->getPage()->locator($selector)->textContent() ?? '', "Selector '$selector' does not contain text '$text'"); + $this->expectLocator($this->getPage()->locator($selector))->toContainText($text); } protected function assertResponseStatusCode(int $expectedCode): void @@ -120,6 +123,11 @@ protected function screenshot(string $path): void $this->getPage()->screenshot($path); } + private function expectLocator(LocatorInterface $locator): ExpectInterface + { + return new ExpectDecorator(new Expect($locator), $this); + } + /** * Must be implemented by the class using this trait. */ diff --git a/tests/Test/PlaywrightTestAssertionsTraitTest.php b/tests/Test/PlaywrightTestAssertionsTraitTest.php index d57c825..72b4793 100644 --- a/tests/Test/PlaywrightTestAssertionsTraitTest.php +++ b/tests/Test/PlaywrightTestAssertionsTraitTest.php @@ -35,24 +35,24 @@ protected function setUp(): void $this->response = null; } - public function testAssertPageContainsUsesPageContent(): void + public function testAssertPageContainsUsesPageHtml(): void { $this->page = $this->createMock(PageInterface::class); $this->page->expects($this->once()) ->method('content') - ->willReturn('
Hello World'); + ->willReturn('Hello World'); - $this->assertPageContains('Hello World'); + $this->assertPageContains('Hello World'); } - public function testAssertPageNotContainsUsesPageContent(): void + public function testAssertPageNotContainsUsesPageHtml(): void { $this->page = $this->createMock(PageInterface::class); $this->page->expects($this->once()) ->method('content') - ->willReturn('Hello World'); + ->willReturn('Hello World'); - $this->assertPageNotContains('Missing Text'); + $this->assertPageNotContains('Missing Text'); } public function testAssertSelectorExistsUsesLocator(): void @@ -69,11 +69,13 @@ public function testAssertSelectorExistsUsesLocator(): void $this->assertSelectorExists('#main'); } - public function testAssertSelectorNotExistsUsesLocator(): void + public function testAssertSelectorNotExistsWaitsUntilCountIsZero(): void { $this->page = $this->createMock(PageInterface::class); $locator = $this->createMock(LocatorInterface::class); - $locator->expects($this->once())->method('count')->willReturn(0); + $locator->expects($this->exactly(2)) + ->method('count') + ->willReturn(1, 0); $this->page->expects($this->once()) ->method('locator') @@ -83,11 +85,13 @@ public function testAssertSelectorNotExistsUsesLocator(): void $this->assertSelectorNotExists('.missing'); } - public function testAssertSelectorVisible(): void + public function testAssertSelectorVisibleWaitsUntilVisible(): void { $this->page = $this->createMock(PageInterface::class); $locator = $this->createMock(LocatorInterface::class); - $locator->expects($this->once())->method('isVisible')->willReturn(true); + $locator->expects($this->exactly(2)) + ->method('isVisible') + ->willReturn(false, true); $this->page->expects($this->once()) ->method('locator') @@ -97,11 +101,13 @@ public function testAssertSelectorVisible(): void $this->assertSelectorVisible('.visible'); } - public function testAssertSelectorHidden(): void + public function testAssertSelectorHiddenWaitsUntilHidden(): void { $this->page = $this->createMock(PageInterface::class); $locator = $this->createMock(LocatorInterface::class); - $locator->expects($this->once())->method('isHidden')->willReturn(true); + $locator->expects($this->exactly(2)) + ->method('isVisible') + ->willReturn(true, false); $this->page->expects($this->once()) ->method('locator') @@ -111,11 +117,13 @@ public function testAssertSelectorHidden(): void $this->assertSelectorHidden('.hidden'); } - public function testAssertSelectorTextContains(): void + public function testAssertSelectorTextContainsWaitsForText(): void { $this->page = $this->createMock(PageInterface::class); $locator = $this->createMock(LocatorInterface::class); - $locator->expects($this->once())->method('textContent')->willReturn('The Quick Brown Fox'); + $locator->expects($this->exactly(2)) + ->method('textContent') + ->willReturn('Loading', 'The Quick Brown Fox'); $this->page->expects($this->once()) ->method('locator')