playwright/tests/page/page-event-network.spec.ts

117 lines
4.7 KiB
TypeScript
Raw Normal View History

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.
*/
import { test as it, expect } from './pageTest';
2020-08-04 02:30:37 +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);
});
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();
});
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');
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');
if (browserName === 'chromium') {
2020-08-04 02:30:37 +03:00
expect(failedRequests[0].failure().errorText).toBe('net::ERR_EMPTY_RESPONSE');
} else if (browserName === 'webkit') {
if (isMac)
2020-08-04 02:30:37 +03:00
expect(failedRequests[0].failure().errorText).toBe('The network connection was lost.');
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();
});
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);
});
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);
events.push('requestfinished');
2020-08-04 02:30:37 +03:00
expect(events).toEqual(['request', 'response', 'requestfinished']);
});
it('should support redirects', async ({ page, server }) => {
2020-08-04 02:30:37 +03:00
const FOO_URL = server.PREFIX + '/foo.html';
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();
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());
});