Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertVisible and assertMissing assertions #35

Merged
merged 4 commits into from
Feb 17, 2025
Merged
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Pause the test for the specified number of milliseconds.
- [assertPresent](#assertpresent)
- [assertNotPresent](#assertnotpresent)
- [assertScript](#assertscript)
- [assertVisible](#assertvisible)
- [assertMissing](#assertmissing)

#### assertAttribute

Expand Down Expand Up @@ -107,6 +109,31 @@ $this->visit($url)
->assertDontSee('we are a streaming service');
```

#### assertVisible

Assert that an element with the given selector is visible:

```php
test('assert visible', function () {
$url = 'https://laravel.com';

$this->visit($url)
->assertVisible('h1:visible');
});
```

#### assertMissing

Assert that an element with the given selector is hidden:

```php
test('assert missing', function () {
$url = 'https://laravel.com';

$this->visit($url)
->assertMissing('a.hidden');
```

#### assertQueryStringHas

Assert that the given query string is present in the url:
Expand Down
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 @@ -241,7 +241,7 @@
/**
* Checks if the given script returns the expected value.
*/
public function assertScript(string $expression, array|bool|float|int|null|string $expected): self

Check failure on line 244 in src/PendingTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - Node 20.x - ubuntu-latest - prefer-stable -

Method Pest\Browser\PendingTest::assertScript() has parameter $expected with no value type specified in iterable type array.

Check failure on line 244 in src/PendingTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - Node 22.x - ubuntu-latest - prefer-stable -

Method Pest\Browser\PendingTest::assertScript() has parameter $expected with no value type specified in iterable type array.
{
$this->operations[] = new Operations\AssertScript($expression, $expected);

Expand Down Expand Up @@ -274,6 +274,26 @@
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);
});
Loading