<commit_before>/**
 * Copyright 2017 Google Inc. All rights reserved.
 * Modifications copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { test as it, expect } from './pageTest';
import * as os from 'os';

function crash({ page, toImpl, browserName, platform, mode }: any) {
  if (browserName === 'chromium') {
    page.goto('chrome://crash').catch(e => {});
  } else if (browserName === 'webkit') {
    it.skip(mode !== 'default');
    it.fixme(platform === 'darwin' && parseInt(os.release(), 10) >= 20, 'Timing out after roll on BigSur');
    toImpl(page)._delegate._session.send('Page.crash', {}).catch(e => {});
  } else if (browserName === 'firefox') {
    it.skip(mode !== 'default');
    toImpl(page)._delegate._session.send('Page.crash', {}).catch(e => {});
  }
}

it.describe('', () => {
  it('should emit crash event when page crashes', async ({ page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    crash({ page, toImpl, browserName, platform, mode });
    const crashedPage = await new Promise(f => page.on('crash', f));
    expect(crashedPage).toBe(page);
  });

  it('should throw on any action after page crashes', async ({ page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    crash({ page, toImpl, browserName, platform, mode });
    await page.waitForEvent('crash');
    const err = await page.evaluate(() => {}).then(() => null, e => e);
    expect(err).toBeTruthy();
    expect(err.message).toContain('Target crashed');
  });

  it('should cancel waitForEvent when page crashes', async ({ page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    const promise = page.waitForEvent('response').catch(e => e);
    crash({ page, toImpl, browserName, platform, mode });
    const error = await promise;
    expect(error.message).toContain('Page crashed');
  });

  it('should cancel navigation when page crashes', async ({ server, page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    server.setRoute('/one-style.css', () => {});
    const promise = page.goto(server.PREFIX + '/one-style.html').catch(e => e);
    await page.waitForNavigation({ waitUntil: 'domcontentloaded' });
    crash({ page, toImpl, browserName, platform, mode });
    const error = await promise;
    expect(error.message).toContain('Navigation failed because page crashed');
  });

  it('should be able to close context when page crashes', async ({ isAndroid, isElectron, page, toImpl, browserName, platform, mode }) => {
    it.skip(isAndroid);
    it.skip(isElectron);

    await page.setContent(`<div>This page should crash</div>`);
    crash({ page, toImpl, browserName, platform, mode });
    await page.waitForEvent('crash');
    await page.context().close();
  });
});<commit_msg>test: rebaseline page-event-crash test (#15817)<commit_after>/**
 * Copyright 2017 Google Inc. All rights reserved.
 * Modifications copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { test as it, expect } from './pageTest';
import * as os from 'os';

function crash({ page, toImpl, browserName, platform, mode }: any) {
  if (browserName === 'chromium') {
    page.goto('chrome://crash').catch(e => {});
  } else if (browserName === 'webkit') {
    it.skip(mode !== 'default');
    it.fixme(platform === 'darwin' && parseInt(os.release(), 10) >= 20, 'Timing out after roll on BigSur');
    toImpl(page)._delegate._session.send('Page.crash', {}).catch(e => {});
  } else if (browserName === 'firefox') {
    it.skip(mode !== 'default');
    toImpl(page)._delegate._session.send('Page.crash', {}).catch(e => {});
  }
}

it.describe('', () => {
  it('should emit crash event when page crashes', async ({ page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    crash({ page, toImpl, browserName, platform, mode });
    const crashedPage = await new Promise(f => page.on('crash', f));
    expect(crashedPage).toBe(page);
  });

  it('should throw on any action after page crashes', async ({ page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    crash({ page, toImpl, browserName, platform, mode });
    await page.waitForEvent('crash');
    const err = await page.evaluate(() => {}).then(() => null, e => e);
    expect(err).toBeTruthy();
    // In Firefox, crashed page is sometimes "closed".
    if (browserName === 'firefox')
      expect(err.message.includes('Target page, context or browser has been closed') || err.message.includes('Target crashed'), err.message).toBe(true);
    else
      expect(err.message).toContain('Target crashed');
  });

  it('should cancel waitForEvent when page crashes', async ({ page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    const promise = page.waitForEvent('response').catch(e => e);
    crash({ page, toImpl, browserName, platform, mode });
    const error = await promise;
    expect(error.message).toContain('Page crashed');
  });

  it('should cancel navigation when page crashes', async ({ server, page, toImpl, browserName, platform, mode }) => {
    await page.setContent(`<div>This page should crash</div>`);
    server.setRoute('/one-style.css', () => {});
    const promise = page.goto(server.PREFIX + '/one-style.html').catch(e => e);
    await page.waitForNavigation({ waitUntil: 'domcontentloaded' });
    crash({ page, toImpl, browserName, platform, mode });
    const error = await promise;
    expect(error.message).toContain('Navigation failed because page crashed');
  });

  it('should be able to close context when page crashes', async ({ isAndroid, isElectron, page, toImpl, browserName, platform, mode }) => {
    it.skip(isAndroid);
    it.skip(isElectron);

    await page.setContent(`<div>This page should crash</div>`);
    crash({ page, toImpl, browserName, platform, mode });
    await page.waitForEvent('crash');
    await page.context().close();
  });
});
{'fraction_non_alphanumeric': 0.1606060606060606, 'fraction_numerical': 0.003994490358126722, 'mean_word_length': 4.088297126839524, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4920927', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2103, 'n_words': 672}