Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Test/Assert/PlaywrightTestAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
36 changes: 22 additions & 14 deletions tests/Test/PlaywrightTestAssertionsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<html><body>Hello World</body></html>');
->willReturn('<html><body><strong>Hello World</strong></body></html>');

$this->assertPageContains('Hello World');
$this->assertPageContains('<strong>Hello World</strong>');
}

public function testAssertPageNotContainsUsesPageContent(): void
public function testAssertPageNotContainsUsesPageHtml(): void
{
$this->page = $this->createMock(PageInterface::class);
$this->page->expects($this->once())
->method('content')
->willReturn('<html><body>Hello World</body></html>');
->willReturn('<html><body><strong>Hello World</strong></body></html>');

$this->assertPageNotContains('Missing Text');
$this->assertPageNotContains('<em>Missing Text</em>');
}

public function testAssertSelectorExistsUsesLocator(): void
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
Loading