2019-12-11 00:21:51 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-12-20 01:51:49 +03:00
|
|
|
|
2019-12-11 00:21:51 +03:00
|
|
|
const utils = require('../utils');
|
|
|
|
|
2019-12-19 04:11:45 +03:00
|
|
|
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, FFOX, CHROME, WEBKIT}) {
|
2019-12-11 00:21:51 +03:00
|
|
|
const {describe, xdescribe, fdescribe} = testRunner;
|
2019-12-20 02:47:35 +03:00
|
|
|
const {it, fit, xit, dit} = testRunner;
|
2019-12-11 00:21:51 +03:00
|
|
|
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
|
|
|
|
|
|
|
describe('Playwright.connect', function() {
|
|
|
|
it('should be able to connect multiple times to the same browser', async({server}) => {
|
2019-12-20 01:51:49 +03:00
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const local = await browserServer.connect();
|
|
|
|
const remote = await playwright.connect({
|
2019-12-11 00:21:51 +03:00
|
|
|
...defaultBrowserOptions,
|
2019-12-20 01:51:49 +03:00
|
|
|
browserWSEndpoint: browserServer.wsEndpoint()
|
2019-12-11 00:21:51 +03:00
|
|
|
});
|
2019-12-20 01:51:49 +03:00
|
|
|
const page = await remote.defaultContext().newPage();
|
2019-12-11 00:21:51 +03:00
|
|
|
expect(await page.evaluate(() => 7 * 8)).toBe(56);
|
2019-12-20 01:51:49 +03:00
|
|
|
remote.disconnect();
|
2019-12-11 00:21:51 +03:00
|
|
|
|
2019-12-20 01:51:49 +03:00
|
|
|
const secondPage = await local.defaultContext().newPage();
|
2019-12-11 00:21:51 +03:00
|
|
|
expect(await secondPage.evaluate(() => 7 * 6)).toBe(42, 'original browser should still work');
|
2019-12-20 01:51:49 +03:00
|
|
|
await browserServer.close();
|
2019-12-11 00:21:51 +03:00
|
|
|
});
|
|
|
|
it('should be able to close remote browser', async({server}) => {
|
2019-12-20 01:51:49 +03:00
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const local = await browserServer.connect();
|
|
|
|
const remote = await playwright.connect({
|
2019-12-11 00:21:51 +03:00
|
|
|
...defaultBrowserOptions,
|
2019-12-20 01:51:49 +03:00
|
|
|
browserWSEndpoint: browserServer.wsEndpoint()
|
2019-12-11 00:21:51 +03:00
|
|
|
});
|
|
|
|
await Promise.all([
|
2019-12-20 01:51:49 +03:00
|
|
|
utils.waitEvent(local, 'disconnected'),
|
|
|
|
remote.close(),
|
2019-12-11 00:21:51 +03:00
|
|
|
]);
|
|
|
|
});
|
2019-12-20 01:51:49 +03:00
|
|
|
it('should be able to reconnect to a browser', async({server}) => {
|
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const browser = await browserServer.connect();
|
|
|
|
const browserWSEndpoint = browserServer.wsEndpoint();
|
|
|
|
const page = await browser.defaultContext().newPage();
|
2019-12-11 00:21:51 +03:00
|
|
|
await page.goto(server.PREFIX + '/frames/nested-frames.html');
|
|
|
|
|
2019-12-20 01:51:49 +03:00
|
|
|
const remote = await playwright.connect({...defaultBrowserOptions, browserWSEndpoint});
|
|
|
|
const pages = await remote.defaultContext().pages();
|
2019-12-11 00:21:51 +03:00
|
|
|
const restoredPage = pages.find(page => page.url() === server.PREFIX + '/frames/nested-frames.html');
|
|
|
|
expect(utils.dumpFrames(restoredPage.mainFrame())).toEqual([
|
|
|
|
'http://localhost:<PORT>/frames/nested-frames.html',
|
|
|
|
' http://localhost:<PORT>/frames/frame.html (aframe)',
|
|
|
|
' http://localhost:<PORT>/frames/two-frames.html (2frames)',
|
|
|
|
' http://localhost:<PORT>/frames/frame.html (dos)',
|
|
|
|
' http://localhost:<PORT>/frames/frame.html (uno)',
|
|
|
|
]);
|
|
|
|
expect(await restoredPage.evaluate(() => 7 * 8)).toBe(56);
|
2019-12-20 01:51:49 +03:00
|
|
|
await remote.close();
|
2019-12-11 00:21:51 +03:00
|
|
|
});
|
|
|
|
// @see https://github.com/GoogleChrome/puppeteer/issues/4197#issuecomment-481793410
|
|
|
|
it('should be able to connect to the same page simultaneously', async({server}) => {
|
2019-12-20 01:51:49 +03:00
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const local = await browserServer.connect();
|
|
|
|
const remote = await playwright.connect({ ...defaultBrowserOptions, browserWSEndpoint: browserServer.wsEndpoint() });
|
2019-12-11 00:21:51 +03:00
|
|
|
const [page1, page2] = await Promise.all([
|
2019-12-20 01:51:49 +03:00
|
|
|
new Promise(x => local.once('targetcreated', target => x(target.page()))),
|
|
|
|
remote.defaultContext().newPage(),
|
2019-12-11 00:21:51 +03:00
|
|
|
]);
|
|
|
|
expect(await page1.evaluate(() => 7 * 8)).toBe(56);
|
|
|
|
expect(await page2.evaluate(() => 7 * 6)).toBe(42);
|
2019-12-20 01:51:49 +03:00
|
|
|
await local.close();
|
2019-12-11 00:21:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Browser.disconnect', function() {
|
|
|
|
it('should reject navigation when browser closes', async({server}) => {
|
|
|
|
server.setRoute('/one-style.css', () => {});
|
2019-12-20 01:51:49 +03:00
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const local = await browserServer.connect();
|
|
|
|
const remote = await playwright.connect({...defaultBrowserOptions, browserWSEndpoint: browserServer.wsEndpoint()});
|
2019-12-19 05:07:11 +03:00
|
|
|
const page = await remote.defaultContext().newPage();
|
2019-12-11 00:21:51 +03:00
|
|
|
const navigationPromise = page.goto(server.PREFIX + '/one-style.html', {timeout: 60000}).catch(e => e);
|
|
|
|
await server.waitForRequest('/one-style.css');
|
|
|
|
remote.disconnect();
|
|
|
|
const error = await navigationPromise;
|
|
|
|
expect(error.message).toBe('Navigation failed because browser has disconnected!');
|
2019-12-20 01:51:49 +03:00
|
|
|
await local.close();
|
2019-12-11 00:21:51 +03:00
|
|
|
});
|
|
|
|
it('should reject waitForSelector when browser closes', async({server}) => {
|
|
|
|
server.setRoute('/empty.html', () => {});
|
2019-12-20 01:51:49 +03:00
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const local = await browserServer.connect();
|
|
|
|
const remote = await playwright.connect({...defaultBrowserOptions, browserWSEndpoint: browserServer.wsEndpoint()});
|
2019-12-19 05:07:11 +03:00
|
|
|
const page = await remote.defaultContext().newPage();
|
2019-12-19 01:28:16 +03:00
|
|
|
const watchdog = page.waitForSelector('div', { timeout: 60000 }).catch(e => e);
|
2019-12-11 00:21:51 +03:00
|
|
|
remote.disconnect();
|
|
|
|
const error = await watchdog;
|
|
|
|
expect(error.message).toContain('Protocol error');
|
2019-12-20 01:51:49 +03:00
|
|
|
await local.close();
|
2019-12-11 00:21:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Browser.close', function() {
|
|
|
|
it('should terminate network waiters', async({context, server}) => {
|
2019-12-20 01:51:49 +03:00
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const local = await browserServer.connect();
|
|
|
|
const remote = await playwright.connect({...defaultBrowserOptions, browserWSEndpoint: browserServer.wsEndpoint()});
|
2019-12-19 05:07:11 +03:00
|
|
|
const newPage = await remote.defaultContext().newPage();
|
2019-12-11 00:21:51 +03:00
|
|
|
const results = await Promise.all([
|
|
|
|
newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e),
|
|
|
|
newPage.waitForResponse(server.EMPTY_PAGE).catch(e => e),
|
2019-12-20 01:51:49 +03:00
|
|
|
local.close()
|
2019-12-11 00:21:51 +03:00
|
|
|
]);
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
const message = results[i].message;
|
|
|
|
expect(message).toContain('Target closed');
|
|
|
|
expect(message).not.toContain('Timeout');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-12 09:52:14 +03:00
|
|
|
describe('Browser.isConnected', () => {
|
|
|
|
it('should set the browser connected state', async () => {
|
2019-12-20 01:51:49 +03:00
|
|
|
const browserServer = await playwright.launchServer(defaultBrowserOptions);
|
|
|
|
const local = await browserServer.connect();
|
|
|
|
const browserWSEndpoint = browserServer.wsEndpoint();
|
2019-12-12 09:52:14 +03:00
|
|
|
const newBrowser = await playwright.connect({browserWSEndpoint});
|
|
|
|
expect(newBrowser.isConnected()).toBe(true);
|
|
|
|
newBrowser.disconnect();
|
|
|
|
expect(newBrowser.isConnected()).toBe(false);
|
2019-12-20 01:51:49 +03:00
|
|
|
await browserServer.close();
|
2019-12-12 09:52:14 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-11 00:21:51 +03:00
|
|
|
};
|