2021-04-29 19:28:19 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-05-06 17:08:22 +03:00
|
|
|
import { test, expect } from './pageTest';
|
2021-04-29 19:28:19 +03:00
|
|
|
|
2022-08-01 00:31:17 +03:00
|
|
|
test('should work', async ({ page, server, toImpl }) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const result = await toImpl(page.mainFrame()).nonStallingRawEvaluateInExistingMainContext('2+2');
|
|
|
|
expect(result).toBe(4);
|
|
|
|
});
|
2021-04-29 19:28:19 +03:00
|
|
|
|
2022-08-01 00:31:17 +03:00
|
|
|
test('should throw while pending navigation', async ({ page, server, toImpl }) => {
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => document.body.textContent = 'HELLO WORLD');
|
|
|
|
let error;
|
|
|
|
await page.route('**/empty.html', async (route, request) => {
|
|
|
|
error = await toImpl(page.mainFrame()).nonStallingRawEvaluateInExistingMainContext('2+2').catch(e => e);
|
2023-06-02 22:59:12 +03:00
|
|
|
await route.abort();
|
2021-04-29 19:28:19 +03:00
|
|
|
});
|
2022-08-01 00:31:17 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE).catch(() => {});
|
|
|
|
expect(error.message).toContain('Frame is currently attempting a navigation');
|
|
|
|
});
|
2021-04-29 19:28:19 +03:00
|
|
|
|
2022-08-01 00:31:17 +03:00
|
|
|
test('should throw when no main execution context', async ({ page, toImpl }) => {
|
|
|
|
let errorPromise;
|
|
|
|
page.on('frameattached', frame => {
|
|
|
|
errorPromise = toImpl(frame).nonStallingRawEvaluateInExistingMainContext('2+2').catch(e => e);
|
2021-04-29 19:28:19 +03:00
|
|
|
});
|
2022-08-01 00:31:17 +03:00
|
|
|
await page.setContent('<iframe></iframe>');
|
|
|
|
const error = await errorPromise;
|
|
|
|
// bail out if we accidentally succeeded
|
|
|
|
if (error === 4)
|
|
|
|
return;
|
|
|
|
// Testing this as a race.
|
|
|
|
expect([
|
|
|
|
'Frame does not yet have a main execution context',
|
|
|
|
'Frame is currently attempting a navigation',
|
|
|
|
'Navigation interrupted the evaluation',
|
|
|
|
]).toContain(error.message);
|
2021-04-29 19:28:19 +03:00
|
|
|
});
|