Skip to content

Commit

Permalink
feat: add assertVisible and assertMissing assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-vasileuski committed Feb 16, 2025
1 parent 2bc935f commit 27bcdea
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Operations/AssertMissing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Pest\Browser\Operations;

use Pest\Browser\Contracts\Operation;

final class AssertMissing implements Operation
{
/**
* Creates an operation instance.
*/
public function __construct(
private string $selector,
) {
//
}

/**
* Compile the operation.
*/
public function compile(): string
{
$escapedSelector = str_replace("'", "\'", $this->selector);

return "await expect(page.locator('{$escapedSelector}')).toBeHidden();";
}
}
29 changes: 29 additions & 0 deletions src/Operations/AssertVisible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Pest\Browser\Operations;

use Pest\Browser\Contracts\Operation;

final class AssertVisible implements Operation
{
/**
* Creates an operation instance.
*/
public function __construct(
private string $selector,
) {
//
}

/**
* Compile the operation.
*/
public function compile(): string
{
$escapedSelector = str_replace("'", "\'", $this->selector);

return "await expect(page.locator('{$escapedSelector}')).toBeVisible();";
}
}
20 changes: 20 additions & 0 deletions src/PendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ public function clickLink(string $text, string $selector = 'a'): self
return $this;
}

/**
* Checks if the given element is visible.
*/
public function assertVisible(string $selector): self
{
$this->operations[] = new Operations\AssertVisible($selector);

return $this;
}

/**
* Checks if the given element is not visible.
*/
public function assertMissing(string $selector): self
{
$this->operations[] = new Operations\AssertMissing($selector);

return $this;
}

/**
* Compile the JavaScript test file.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Browser/Operations/AssertMissingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Symfony\Component\Process\Exception\ProcessFailedException;

describe('assert missing', function () {
it('passes when the given element is not visible', function () {
$this->visit('https://laravel.com')
->assertMissing('.DocSearch-Button-Keys');
});

it('fails when the given element is visible', function () {
$this->visit('https://laravel.com')
->assertMissing('body');
})->throws(ProcessFailedException::class);
});
17 changes: 17 additions & 0 deletions tests/Browser/Operations/AssertVisibleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Symfony\Component\Process\Exception\ProcessFailedException;

describe('assert visible', function () {
it('passes when the given element is visible', function () {
$this->visit('https://laravel.com')
->assertVisible('body');
});

it('fails when the given element is not visible', function () {
$this->visit('https://laravel.com')
->assertVisible('.DocSearch-Button-Keys');
})->throws(ProcessFailedException::class);
});

0 comments on commit 27bcdea

Please sign in to comment.