Repository: puppeteer/puppeteer
File: test/src/page.spec.ts (TypeScript)

Code before:
      for (let i = 0; i < 2; i++) {
        const message = results[i].message;
        expect(message).atLeastOneToContain([
          'Target closed',
          'Page closed!',
          'Frame detached',
        ]);
        expect(message).not.toContain('Timeout');
      }
    });
    it('closing last tab should not close the browser', async () => {
      // This test asserts the behavior is consistent between protocols and browsers.
      // WebDriver Classic requires the session to be closed after the last page is
      // closed: https://w3c.github.io/webdriver/#dfn-close-window.
      // WebDriver BiDi does not specify behavior for closing the last page:
      // https://github.com/w3c/webdriver-bidi/issues/187
      const {browser, close} = await launch({}, {createContext: false});
      try {
        const pages = await browser.pages();
        await Promise.all(
          pages.map(page => {
            return page.close();
          })
        );
        // Verify the browser is still connected.
        expect(browser.isConnected()).toBe(true);
        // Verify the browser can open a new page.
        await browser.newPage();
      } finally {
        await close();
      }
    });
  });

  describe('Page.Events.Load', function () {
    it('should fire when expected', async () => {
      const {page} = await getTestState();

      await Promise.all([waitEvent(page, 'load'), page.goto('about:blank')]);
    });
  });

  describe('removing and adding event handlers', () => {
    it('should correctly fire event handlers as they are added and then removed', async () => {
      const {page, server} = await getTestState();

      const handler = sinon.spy();
      const onResponse = (response: {url: () => string}) => {
        // Ignore default favicon requests.
        if (!isFavicon(response)) {
          handler();

Review comment (suggestion):
```suggestion
        expect(browser.connected).toBe(true);
```

Code after:
      for (let i = 0; i < 2; i++) {
        const message = results[i].message;
        expect(message).atLeastOneToContain([
          'Target closed',
          'Page closed!',
          'Frame detached',
        ]);
        expect(message).not.toContain('Timeout');
      }
    });
  });

  describe('Page.Events.Load', function () {
    it('should fire when expected', async () => {
      const {page} = await getTestState();

      await Promise.all([waitEvent(page, 'load'), page.goto('about:blank')]);
    });
  });

  describe('removing and adding event handlers', () => {
    it('should correctly fire event handlers as they are added and then removed', async () => {
      const {page, server} = await getTestState();

      const handler = sinon.spy();
      const onResponse = (response: {url: () => string}) => {
        // Ignore default favicon requests.
        if (!isFavicon(response)) {
          handler();
        }
      };
      page.on('response', onResponse);
      await page.goto(server.EMPTY_PAGE);
      expect(handler.callCount).toBe(1);
      page.off('response', onResponse);
      await page.goto(server.EMPTY_PAGE);
      // Still one because we removed the handler.
      expect(handler.callCount).toBe(1);
      page.on('response', onResponse);
      await page.goto(server.EMPTY_PAGE);
      // Two now because we added the handler back.
      expect(handler.callCount).toBe(2);
    });

    it('should correctly added and removed request events', async () => {
      const {page, server} = await getTestState();

      const handler = sinon.spy();
      const onResponse = (response: {url: () => string}) => {
        // Ignore default favicon requests.
        if (!isFavicon(response)) {