2020-08-04 02:30:37 +03:00
|
|
|
/**
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
|
|
|
|
2021-05-06 17:08:22 +03:00
|
|
|
import { test as it, expect } from './pageTest';
|
2021-05-06 05:10:28 +03:00
|
|
|
import { attachFrame } from '../config/utils';
|
2020-08-04 02:30:37 +03:00
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should fire for navigation requests', async ({ page, server }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const requests = [];
|
|
|
|
page.on('request', request => requests.push(request));
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(requests.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should fire for iframes', async ({ page, server }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const requests = [];
|
|
|
|
page.on('request', request => requests.push(request));
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-09-19 01:52:14 +03:00
|
|
|
await attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
2020-08-04 02:30:37 +03:00
|
|
|
expect(requests.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should fire for fetches', async ({ page, server }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const requests = [];
|
|
|
|
page.on('request', request => requests.push(request));
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => fetch('/empty.html'));
|
|
|
|
expect(requests.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
2021-10-02 05:40:47 +03:00
|
|
|
it('should report requests and responses handled by service worker', async ({ page, server, isAndroid, isElectron }) => {
|
2021-04-09 17:59:09 +03:00
|
|
|
it.fixme(isAndroid);
|
2021-10-02 05:40:47 +03:00
|
|
|
it.fixme(isElectron);
|
2021-04-03 00:23:42 +03:00
|
|
|
|
2020-08-04 02:30:37 +03:00
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/fetchdummy/sw.html');
|
2020-08-28 14:20:29 +03:00
|
|
|
await page.evaluate(() => window['activationPromise']);
|
2020-08-04 02:30:37 +03:00
|
|
|
const [swResponse, request] = await Promise.all([
|
2020-08-28 14:20:29 +03:00
|
|
|
page.evaluate(() => window['fetchDummy']('foo')),
|
2020-08-04 02:30:37 +03:00
|
|
|
page.waitForEvent('request'),
|
|
|
|
]);
|
|
|
|
expect(swResponse).toBe('responseFromServiceWorker:foo');
|
|
|
|
expect(request.url()).toBe(server.PREFIX + '/serviceworkers/fetchdummy/foo');
|
|
|
|
const response = await request.response();
|
|
|
|
expect(response.url()).toBe(server.PREFIX + '/serviceworkers/fetchdummy/foo');
|
|
|
|
expect(await response.text()).toBe('responseFromServiceWorker:foo');
|
|
|
|
});
|
2021-09-28 19:54:05 +03:00
|
|
|
|
|
|
|
it('should return response body when Cross-Origin-Opener-Policy is set', async ({ page, server, browserName }) => {
|
|
|
|
server.setRoute('/empty.html', (req, res) => {
|
|
|
|
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
|
|
|
|
res.end('Hello there!');
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(page.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
await response.finished();
|
|
|
|
expect(response.request().failure()).toBeNull();
|
|
|
|
expect(await response.text()).toBe('Hello there!');
|
|
|
|
});
|
2021-11-13 06:06:53 +03:00
|
|
|
|
|
|
|
it('should fire requestfailed when intercepting race', async ({ page, server, browserName }) => {
|
2021-12-02 18:44:13 +03:00
|
|
|
it.skip(browserName !== 'chromium', 'This test is specifically testing Chromium race');
|
|
|
|
|
2021-11-13 06:06:53 +03:00
|
|
|
const promsie = new Promise<void>(resolve => {
|
|
|
|
let counter = 0;
|
|
|
|
const failures = new Set();
|
|
|
|
const alive = new Set();
|
|
|
|
page.on('request', request => {
|
|
|
|
expect(alive.has(request)).toBe(false);
|
|
|
|
expect(failures.has(request)).toBe(false);
|
|
|
|
alive.add(request);
|
|
|
|
});
|
|
|
|
page.on('requestfailed', request => {
|
|
|
|
expect(failures.has(request)).toBe(false);
|
|
|
|
expect(alive.has(request)).toBe(true);
|
|
|
|
alive.delete(request);
|
|
|
|
failures.add(request);
|
|
|
|
if (++counter === 10)
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Stall requests to make sure we don't get requestfinished.
|
|
|
|
await page.route('**', route => {});
|
|
|
|
|
|
|
|
await page.setContent(`
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<iframe src="${server.EMPTY_PAGE}"></iframe>
|
|
|
|
<script>
|
|
|
|
function abortAll() {
|
|
|
|
const frames = document.querySelectorAll("iframe");
|
|
|
|
for (const frame of frames)
|
|
|
|
frame.src = "about:blank";
|
|
|
|
}
|
2021-12-02 18:44:13 +03:00
|
|
|
abortAll();
|
2021-11-13 06:06:53 +03:00
|
|
|
</script>
|
|
|
|
`);
|
|
|
|
|
|
|
|
await promsie;
|
|
|
|
});
|
2023-05-20 03:59:17 +03:00
|
|
|
|
2023-05-25 19:43:41 +03:00
|
|
|
it('main resource xhr should have type xhr', async ({ page, server }) => {
|
2023-05-20 03:59:17 +03:00
|
|
|
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/22812' });
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [request] = await Promise.all([
|
|
|
|
page.waitForEvent('request'),
|
|
|
|
page.evaluate(() => {
|
|
|
|
const x = new XMLHttpRequest();
|
|
|
|
x.open('GET', location.href, false);
|
|
|
|
x.send();
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
expect(request.isNavigationRequest()).toBe(false);
|
|
|
|
expect(request.resourceType()).toBe('xhr');
|
|
|
|
});
|