microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.6k stars 3.57k forks source link

Not able to detect browser event: disconnected event when close browser is invoked #2946

Closed SharikSaigal87 closed 3 years ago

SharikSaigal87 commented 4 years ago

As seen in below code snippet I am Not able to detect browser event: disconnected event when close browser is invoked.Butwhen node process is killed i get the event "browser is closed" Can you please help correct way of detectng browser disconnected event at correct time when browser is closed?

Context: Playwright Version: [playwright-v1.2.0] Operating System: [Windows (Pro 10 - 1809) & Linux (Ubuntu 20.04)] Node version: [v14.4.0 on Windows, v12.18.0 on Linux (Ubuntu 20.04)] Browser: [Chromium] Code Snippet

const {chromium} = require('playwright');
const url1 = 'https://google.com';
(async () => {
    const browser = await chromium.launch({
        headless: false,
        executablePath: 'c:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
        ignoreDefaultArgs: ['--enable-automation'],
        args: [`--app=${url1}`]
    });
    browser.on('disconnected', () => { console.log('browser is closed') });
})();
arjunattam commented 4 years ago

Thanks @SharikSaigal87. I'm able to repro this with Chromium, but it works with WebKit and Firefox. Does that mirror your experience? Also, any particular reason for you to use Chrome stable (instead of Chromium)?

Modified snippet used for testing:

const {chromium, webkit, firefox} = require('playwright');
(async () => {
    const browser = await chromium.launch({ headless: false });
    await browser.newPage();
    // Disconnected event should be fired when the browser is killed manually
    browser.on('disconnected', () => { console.log('browser is closed') });
})();
ArtemGr commented 4 years ago

Noticed that Chromium stays around in the process list even if the browser window is closed. Watching for the page closing event helps to workaround this, e.g.

import {chromium} from 'playwright';
(async () => {
  const browser = await chromium.launch ({headless: false});
  const context = await browser.newContext();
  const page = await context.newPage();
  page.on ('close', () => {
    console.log ('page closed');
    browser.close();});
})();
aslushnikov commented 4 years ago

I'll rephrase this: in Chromium Headful, it's impossible to terminate chromium manually: closing last windows doesn't close browser, and Cmd-Q doesn't close browser either.

dgozman commented 4 years ago

Hmm... We keep the browser open, because Playwright script could open a new context at any moment.

Specifically on MacOS, where we have a separate "Quit application" command, we could theoretically make it quit anyway.

dgozman commented 3 years ago

Seems like working as intended - I don't think we'll address this. When browser is controlled by Playwright, either call browser.close() or just terminate the script.