-
-
Notifications
You must be signed in to change notification settings - Fork 85
feat: add ability to interact with dialogs #163
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
base: 4.x
Are you sure you want to change the base?
feat: add ability to interact with dialogs #163
Conversation
|
@jasonmccreary Would love to get your feedback on this, if you have time. |
|
This is really useful in testing an app that has many confirm dialogs. There's just a little thing that has been tripping me up. When an expectation in the dialog handler function fails, it can cause a hang where the dialog remains open (with no information on which test case is running). This happens if the interaction that opened the dialog uses an automatic selector. Demonstration with a test case: it('does not hang when dialog expectation fails', function (): void {
Route::get('/', fn (): string => '
<button id="confirm-btn" onclick="confirm(\'Are you sure?\');">Show Confirm</button>
');
$page = visit('/');
$page->onDialog(function (Dialog $dialog): void {
expect($dialog->message())->toBe('Wrong text');
$dialog->accept();
});
// $page->click('#confirm-btn'); // times out as desired and fails with "Timeout 1000ms exceeded"
$page->click('Show Confirm'); // makes test hang with dialog still open
})->throws(ExpectationFailedException::class);This can be worked around by a try / catch in the dialog handler: $page->onDialog(function (Dialog $dialog): void {
try {
expect($dialog->message())->toBe('Wrong text');
} catch (ExpectationFailedException $e) {
$dialog->dismiss();
throw $e;
}
$dialog->accept();
});That also gives a nicer result of making the test fail with the original expectation failure rather than a timeout for the click, i.e. "Failed asserting that two strings are identical." (with a diff). Does this try / catch inside the handler feel too long-winded for inclusion in example usage? Would it be appropriate to catch this exception in |
|
@biinari I will look into this. |
320bc6b to
70e03bd
Compare
|
@biinari Added the try catch to properly avoid test freezing issue. |
|
Any updates on when this will be merged? |
This pull request adds the ability to test and interact with browser dialogs. Playwright automatically dismisses the dialogs if they are not explicitly interrupted in tests. However, sometimes you might want to make sure if certain dialogs appear, under certain conditions and maybe with some dynamic content or alert.
Example: