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 support for interacting with dialog boxes #655

Open
Tracked by #1460
ankur22 opened this issue Nov 18, 2022 · 1 comment · Fixed by #663
Open
Tracked by #1460

Add support for interacting with dialog boxes #655

ankur22 opened this issue Nov 18, 2022 · 1 comment · Fixed by #663
Labels
feature A new feature

Comments

@ankur22
Copy link
Collaborator

ankur22 commented Nov 18, 2022

Feature Description

Given a website which pop ups a dialog box (alert, prompt, confirm or beforeunload) such as:

<!DOCTYPE html>
<html>
    <head>
        <script>
            window.alert("sometext");
        </script>
    </head>
    <body>
        Hello World!
    </body>
</html>

We want to have the functionality to interact with the dialog box in the same way as Playwright's page.on method.

Suggested Solution (optional)

We need to implement the CDP listener handleJavaScriptDialog. If a page.on("dialog") listener has not been called prior to navigating to a page with an alert window, the dialog should automatically be dismissed.

This listener needs to be set prior to the navigation. There are two methods that can be called on the dialog object, one is accept(promptText) and the other is dismiss. promptText is only really needed when the dialog box is of type prompt, if it is added then it doesn't change the behaviour in any way.

Listener must either accept([promptText]) or dialog.dismiss() the dialog - otherwise the page will freeze waiting for the dialog, and actions like click will never finish.

Playwright script:

const { test, chromium } = require('@playwright/test');

test.describe('lifecycle', () => {
  test('lifecycle', async () => {
    const browser = await chromium.launch({ headless: false });
    const ctx = await browser.newContext();
    const page = await ctx.newPage();

    // This listener will be called when we navigate to the page.
    page.on("dialog", (dialog) => {
      dialog.accept();
      console.log("accept 1");
    })

    await page.goto("http://localhost:8080/dialogbox", { waitUntil: 'networkidle' });

    // Listener set, but nothing will call it.
    page.on("dialog", (dialog) => {
      dialog.accept();
      console.log("accept 2");
    })

    // Another listener set, but nothing will call it.
    page.on("dialog", (dialog) => {
      dialog.accept();
      console.log("accept 3");
    })

    await browser.close();
  });
});

Already existing or connected issues / PRs (optional)

No response

@ankur22 ankur22 added the feature A new feature label Nov 18, 2022
@ankur22 ankur22 changed the title Implement a method of interacting with dialog box (alerts) Implement a method of interacting with dialog boxes Nov 18, 2022
@ankur22
Copy link
Collaborator Author

ankur22 commented Nov 30, 2022

This has been partially completed with #663. The change will dismiss the dialog box automatically.

@ankur22 ankur22 reopened this Nov 30, 2022
@inancgumus inancgumus added this to the v0.7.0 milestone Dec 5, 2022
@inancgumus inancgumus changed the title Implement a method of interacting with dialog boxes Add support for interacting with dialog boxes Dec 5, 2022
@inancgumus inancgumus removed this from the v0.7.0 milestone Feb 7, 2023
@ankur22 ankur22 removed their assignment Jun 12, 2023
@inancgumus inancgumus mentioned this issue Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature
Projects
Development

Successfully merging a pull request may close this issue.

2 participants