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.
|
|
|
|
*/
|
2020-08-20 07:32:12 +03:00
|
|
|
|
2021-05-06 17:08:22 +03:00
|
|
|
import { test as it, expect } from './pageTest';
|
2020-08-04 02:30:37 +03:00
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('Page.Events.Request', 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);
|
|
|
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(requests[0].resourceType()).toBe('document');
|
|
|
|
expect(requests[0].method()).toBe('GET');
|
|
|
|
expect(await requests[0].response()).toBeTruthy();
|
|
|
|
expect(requests[0].frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('Page.Events.Response', async ({ page, server }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const responses = [];
|
|
|
|
page.on('response', response => responses.push(response));
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(responses.length).toBe(1);
|
|
|
|
expect(responses[0].url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(responses[0].status()).toBe(200);
|
|
|
|
expect(responses[0].ok()).toBe(true);
|
|
|
|
expect(responses[0].request()).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('Page.Events.RequestFailed', async ({ page, server, browserName, isMac, isWindows }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
server.setRoute('/one-style.css', (req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'text/css');
|
2020-08-12 01:50:53 +03:00
|
|
|
res.connection.destroy();
|
2020-08-04 02:30:37 +03:00
|
|
|
});
|
|
|
|
const failedRequests = [];
|
|
|
|
page.on('requestfailed', request => failedRequests.push(request));
|
|
|
|
await page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
expect(failedRequests.length).toBe(1);
|
|
|
|
expect(failedRequests[0].url()).toContain('one-style.css');
|
|
|
|
expect(await failedRequests[0].response()).toBe(null);
|
|
|
|
expect(failedRequests[0].resourceType()).toBe('stylesheet');
|
2021-05-13 20:22:23 +03:00
|
|
|
if (browserName === 'chromium') {
|
2020-08-04 02:30:37 +03:00
|
|
|
expect(failedRequests[0].failure().errorText).toBe('net::ERR_EMPTY_RESPONSE');
|
2021-05-13 20:22:23 +03:00
|
|
|
} else if (browserName === 'webkit') {
|
2020-09-17 07:36:36 +03:00
|
|
|
if (isMac)
|
2020-08-04 02:30:37 +03:00
|
|
|
expect(failedRequests[0].failure().errorText).toBe('The network connection was lost.');
|
2020-09-17 07:36:36 +03:00
|
|
|
else if (isWindows)
|
2020-08-04 02:30:37 +03:00
|
|
|
expect(failedRequests[0].failure().errorText).toBe('Server returned nothing (no headers, no data)');
|
|
|
|
else
|
|
|
|
expect(failedRequests[0].failure().errorText).toBe('Message Corrupt');
|
|
|
|
} else {
|
|
|
|
expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_NET_RESET');
|
|
|
|
}
|
|
|
|
expect(failedRequests[0].frame()).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('Page.Events.RequestFinished', async ({ page, server }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const [response] = await Promise.all([
|
|
|
|
page.goto(server.EMPTY_PAGE),
|
|
|
|
page.waitForEvent('requestfinished')
|
|
|
|
]);
|
|
|
|
const request = response.request();
|
|
|
|
expect(request.url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(await request.response()).toBeTruthy();
|
|
|
|
expect(request.frame() === page.mainFrame()).toBe(true);
|
|
|
|
expect(request.frame().url()).toBe(server.EMPTY_PAGE);
|
|
|
|
expect(request.failure()).toBe(null);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should fire events in proper order', async ({ page, server }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const events = [];
|
|
|
|
page.on('request', request => events.push('request'));
|
|
|
|
page.on('response', response => events.push('response'));
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(await response.finished()).toBe(null);
|
2020-08-28 14:20:29 +03:00
|
|
|
events.push('requestfinished');
|
2020-08-04 02:30:37 +03:00
|
|
|
expect(events).toEqual(['request', 'response', 'requestfinished']);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should support redirects', async ({ page, server }) => {
|
2020-08-04 02:30:37 +03:00
|
|
|
const FOO_URL = server.PREFIX + '/foo.html';
|
2021-09-01 03:48:39 +03:00
|
|
|
const events = {};
|
|
|
|
events[server.EMPTY_PAGE] = [];
|
|
|
|
events[FOO_URL] = [];
|
|
|
|
page.on('request', request => events[request.url()].push(request.method()));
|
|
|
|
page.on('response', response => events[response.url()].push(response.status()));
|
|
|
|
page.on('requestfinished', request => events[request.url()].push('DONE'));
|
|
|
|
page.on('requestfailed', request => events[request.url()].push('FAIL'));
|
|
|
|
|
|
|
|
server.setRedirect('/foo.html', '/empty.html');
|
2020-08-04 02:30:37 +03:00
|
|
|
const response = await page.goto(FOO_URL);
|
|
|
|
await response.finished();
|
2021-09-01 03:48:39 +03:00
|
|
|
|
|
|
|
const expected = {};
|
|
|
|
expected[FOO_URL] = ['GET', 302, 'DONE'];
|
|
|
|
expected[server.EMPTY_PAGE] = ['GET', 200, 'DONE'];
|
|
|
|
expect(events).toEqual(expected);
|
2020-08-04 02:30:37 +03:00
|
|
|
const redirectedFrom = response.request().redirectedFrom();
|
|
|
|
expect(redirectedFrom.url()).toContain('/foo.html');
|
|
|
|
expect(redirectedFrom.redirectedFrom()).toBe(null);
|
|
|
|
expect(redirectedFrom.redirectedTo()).toBe(response.request());
|
|
|
|
});
|