Skip to content

Commit f63574a

Browse files
committed
chore: adjusts existing tests
1 parent cc41076 commit f63574a

22 files changed

Lines changed: 36 additions & 357 deletions

src/Playwright/Locator.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Generator;
88
use Pest\Browser\Playwright\Concerns\InteractsWithPlaywright;
99
use Pest\Browser\Support\Selector;
10+
use PHPUnit\Framework\ExpectationFailedException;
1011
use RuntimeException;
12+
use WebSocket\TimeoutException;
1113

1214
/**
1315
* @internal
@@ -31,6 +33,8 @@ public function __construct(
3133
*/
3234
public function isVisible(): bool
3335
{
36+
$this->waitFor();
37+
3438
$response = $this->sendMessage('isVisible');
3539

3640
return $this->processBooleanResponse($response);
@@ -41,6 +45,8 @@ public function isVisible(): bool
4145
*/
4246
public function isChecked(): bool
4347
{
48+
$this->waitFor();
49+
4450
$response = $this->sendMessage('isChecked');
4551

4652
return $this->processBooleanResponse($response);
@@ -51,6 +57,8 @@ public function isChecked(): bool
5157
*/
5258
public function isEnabled(): bool
5359
{
60+
$this->waitFor();
61+
5462
$response = $this->sendMessage('isEnabled');
5563

5664
return $this->processBooleanResponse($response);
@@ -61,22 +69,32 @@ public function isEnabled(): bool
6169
*/
6270
public function isDisabled(): bool
6371
{
64-
return ! $this->isEnabled();
72+
$this->waitFor();
73+
74+
$response = $this->sendMessage('isDisabled');
75+
76+
return $this->processBooleanResponse($response);
6577
}
6678

6779
/**
6880
* Check if element matching the locator is hidden.
6981
*/
7082
public function isHidden(): bool
7183
{
72-
return ! $this->isVisible();
84+
$this->waitFor(['state' => 'hidden']);
85+
86+
$response = $this->sendMessage('isHidden');
87+
88+
return $this->processBooleanResponse($response);
7389
}
7490

7591
/**
7692
* Check if element matching the locator is editable.
7793
*/
7894
public function isEditable(): bool
7995
{
96+
$this->waitFor();
97+
8098
$response = $this->sendMessage('isEditable');
8199

82100
return $this->processBooleanResponse($response);
@@ -87,7 +105,10 @@ public function isEditable(): bool
87105
*/
88106
public function check(): void
89107
{
108+
$this->waitFor();
109+
90110
$response = $this->sendMessage('check');
111+
91112
$this->processVoidResponse($response);
92113
}
93114

@@ -263,8 +284,13 @@ public function getAttribute(string $name): ?string
263284
*/
264285
public function waitFor(?array $options = null): void
265286
{
266-
$response = $this->sendMessage('waitForSelector', $options ?? []);
267-
$this->processVoidResponse($response);
287+
try {
288+
$response = $this->sendMessage('waitForSelector', $options ?? []);
289+
290+
$this->processVoidResponse($response);
291+
} catch (TimeoutException) {
292+
throw new ExpectationFailedException('Element not found.');
293+
}
268294
}
269295

270296
/**

src/Playwright/Page.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function goto(string $url): self
5353
}
5454

5555
$response = $this->sendMessage('goto', ['url' => $url, 'waitUntil' => 'load']);
56+
5657
$this->processNavigationResponse($response);
5758

5859
return $this;

tests/Browser/Expectations/ToBeCheckedTest.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
7-
it('passes when checkbox is checked', function (): void {
5+
it('passes if checkbox is checked', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('input[name="checked-checkbox"]'))->toBeChecked();
119
});
12-
13-
it('fails when checkbox is checked', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('input[name="checked-checkbox"]'))->not->toBeChecked();
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when checkbox is not checked', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('input[name="unchecked-checkbox"]'))->not->toBeChecked();
23-
});
24-
25-
it('fails when checkbox is not checked', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('input[name="unchecked-checkbox"]'))->toBeChecked();
29-
})->throws(ExpectationFailedException::class);

tests/Browser/Expectations/ToBeDisabledTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,12 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
75
it('passes when input is disabled', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('input[name="disabled-input"]'))->toBeDisabled();
119
});
1210

13-
it('fails when input is disabled', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('input[name="disabled-input"]'))->not->toBeDisabled();
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when input is not disabled', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('input[name="enabled-input"]'))->not->toBeDisabled();
23-
});
24-
25-
it('fails when input is not disabled', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('input[name="enabled-input"]'))->toBeDisabled();
29-
})->throws(ExpectationFailedException::class);
30-
3111
it('passes when button is disabled', function (): void {
3212
$page = page()->goto('/test/form-inputs');
3313

tests/Browser/Expectations/ToBeEditableTest.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
75
it('passes when input is editable', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('input[name="enabled-input"]'))->toBeEditable();
119
});
12-
13-
it('fails when input is editable', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('input[name="enabled-input"]'))->not->toBeEditable();
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when input is not editable (readonly)', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('input[name="readonly-input"]'))->not->toBeEditable();
23-
});
24-
25-
it('fails when input is not editable (readonly)', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('input[name="readonly-input"]'))->toBeEditable();
29-
})->throws(ExpectationFailedException::class);
30-
31-
it('passes when input is not editable (disabled)', function (): void {
32-
$page = page()->goto('/test/form-inputs');
33-
34-
expect($page->locator('input[name="disabled-input"]'))->not->toBeEditable();
35-
});
36-
37-
it('fails when input is not editable (disabled)', function (): void {
38-
$page = page()->goto('/test/form-inputs');
39-
40-
expect($page->locator('input[name="disabled-input"]'))->toBeEditable();
41-
})->throws(ExpectationFailedException::class);

tests/Browser/Expectations/ToBeEmptyTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
75
it('passes when element is empty', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('#empty-element'))->toBeEmpty();
119
});
12-
13-
it('fails when element is not empty', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('label[for="email"]'))->toBeEmpty();
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when element is not empty and we expect it not to be', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('label[for="email"]'))->not->toBeEmpty();
23-
});
24-
25-
it('fails when element is empty but we expect it not to be', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('#empty-element'))->not->toBeEmpty();
29-
})->throws(ExpectationFailedException::class);

tests/Browser/Expectations/ToBeEnabledTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,14 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
75
it('passes when input is enabled', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('input[name="enabled-input"]'))->toBeEnabled();
119
});
1210

13-
it('fails when input is enabled', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('input[name="enabled-input"]'))->not->toBeEnabled();
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when input is not enabled', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('input[name="disabled-input"]'))->not->toBeEnabled();
23-
});
24-
25-
it('fails when input is not enabled', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('input[name="disabled-input"]'))->toBeEnabled();
29-
})->throws(ExpectationFailedException::class);
30-
3111
it('passes when button is enabled', function (): void {
3212
$page = page()->goto('/test/form-inputs');
3313

3414
expect($page->locator('button[name="enabled-button"]'))->toBeEnabled();
3515
});
36-
37-
it('passes when button is not enabled', function (): void {
38-
$page = page()->goto('/test/form-inputs');
39-
40-
expect($page->locator('button[name="disabled-button"]'))->not->toBeEnabled();
41-
});

tests/Browser/Expectations/ToBeHiddenTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
75
it('passes when element is hidden', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('input[name="hidden"]'))->toBeHidden();
119
});
12-
13-
it('fails when element is hidden', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('input[name="hidden"]'))->not->toBeHidden();
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when element is not hidden', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('input[name="visible"]'))->not->toBeHidden();
23-
});
24-
25-
it('fails when element is not hidden', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('input[name="visible"]'))->toBeHidden();
29-
})->throws(ExpectationFailedException::class);

tests/Browser/Expectations/ToBeVisibleTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
75
it('passes when element is visible', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('input[name="visible"]'))->toBeVisible();
119
});
12-
13-
it('fails when element is visible', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('input[name="visible"]'))->not->toBeVisible();
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when element is not visible', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('input[name="hidden"]'))->not->toBeVisible();
23-
});
24-
25-
it('fails when element is not visible', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('input[name="hidden"]'))->toBeVisible();
29-
})->throws(ExpectationFailedException::class);

tests/Browser/Expectations/ToHaveClassTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use PHPUnit\Framework\ExpectationFailedException;
6-
75
it('passes when element has the expected class', function (): void {
86
$page = page()->goto('/test/form-inputs');
97

108
expect($page->locator('input[name="default-checkbox"]'))->toHaveClass('check-class');
119
});
12-
13-
it('fails when element does not have the expected class', function (): void {
14-
$page = page()->goto('/test/form-inputs');
15-
16-
expect($page->locator('input[name="default-checkbox"]'))->toHaveClass('some-other-class');
17-
})->throws(ExpectationFailedException::class);
18-
19-
it('passes when element does not have the class and we expect it not to', function (): void {
20-
$page = page()->goto('/test/form-inputs');
21-
22-
expect($page->locator('input[name="default-checkbox"]'))->not->toHaveClass('some-other-class');
23-
});
24-
25-
it('fails when element has the class but we expect it not to', function (): void {
26-
$page = page()->goto('/test/form-inputs');
27-
28-
expect($page->locator('input[name="default-checkbox"]'))->not->toHaveClass('check-class');
29-
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)