Skip to content

[Bug]: codegen is not able to keep track of tabs and popups opened and will refer non-existing variables #36274

Open
@kurtcodebain

Description

@kurtcodebain

Version

1.52.0

Steps to reproduce

  1. Create bug.html with this HTML:
<!DOCTYPE html>
<html>
<head>
  <title>Tab and Popup</title>
</head>
<body>
  <button onclick="openTabAndPopup()">Open Tab and Popup</button>
  <script>
    function openTabAndPopup() {
      const newTab = window.open('', '_blank');
      if (newTab) {
        newTab.document.write(`
          <html>
            <head><title>New Tab</title></head>
            <body>
              <h1>New Tab Content</h1>
              <script>
                window.onload = function() {
                  const popup = window.open('', '', 'width=300,height=200');
                  if (popup) {
                    popup.document.write('<p>This is a popup from the new tab.</p>');
                    popup.document.close();
                    window.close(); // Close the tab
                  } else {
                    alert('Popup was blocked.');
                  }
                };
              <\/script>
            </body>
          </html>
        `);
        newTab.document.close();
      } else {
        alert('Tab was blocked.');
      }
    }
  </script>
</body>
</html>
  1. Run codegen playwright codegen ~/<path>/bug.html
  2. Click button to open popup
  3. You should now see the following recorded steps:
using Microsoft.Playwright;
using System;
using System.Threading.Tasks;

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
    Headless = false,
});
var context = await browser.NewContextAsync();

var page = await context.NewPageAsync();
await page.GotoAsync("file:///Users/<user>/Desktop/bug.html");
var page2 = await page.RunAndWaitForPopupAsync(async () =>
{
    await page.GetByRole(AriaRole.Button, new() { Name = "Open Tab and Popup" }).ClickAsync();
});
await page1.CloseAsync();

Expected behavior

Playwright to be able to keep track of tabs and popups opened and closed referring real variable names.

Actual behavior

Playwright refers a non-existing variable page1, specifically the last line:

await page1.CloseAsync();

page1 never existed.

Additional context

No response

Environment

- Operating System: [macOS Sequoia 15.5]
- CPU: [arm64]
- Browser: [All]
- .NET Version (TFM): [net8.0]
- Other info:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions