2018-09-13 19:08:51 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const puppeteer = require('../..');
|
2018-09-24 19:46:39 +00:00
|
|
|
const {TestServer} = require('../testserver/');
|
2020-04-03 11:22:55 +00:00
|
|
|
const expect = require('expect');
|
2018-09-13 19:08:51 +00:00
|
|
|
|
|
|
|
const puppeteerWebPath = path.join(__dirname, 'puppeteer-web.js');
|
|
|
|
if (!fs.existsSync(puppeteerWebPath))
|
|
|
|
throw new Error(`puppeteer-web is not built; run "npm run bundle"`);
|
|
|
|
const puppeteerWeb = fs.readFileSync(puppeteerWebPath, 'utf8');
|
|
|
|
|
2020-04-09 18:12:32 +00:00
|
|
|
const state = {};
|
2018-09-13 19:08:51 +00:00
|
|
|
|
2020-04-09 18:12:32 +00:00
|
|
|
before(async() => {
|
2018-09-13 19:08:51 +00:00
|
|
|
const assetsPath = path.join(__dirname, '..', '..', 'test', 'assets');
|
|
|
|
const port = 8998;
|
2018-09-24 19:46:39 +00:00
|
|
|
state.server = await TestServer.create(assetsPath, port);
|
2018-09-13 19:08:51 +00:00
|
|
|
state.serverConfig = {
|
|
|
|
PREFIX: `http://localhost:${port}`,
|
|
|
|
EMPTY_PAGE: `http://localhost:${port}/empty.html`,
|
|
|
|
};
|
2018-11-13 07:26:16 +00:00
|
|
|
state.browser = await puppeteer.launch();
|
2018-09-13 19:08:51 +00:00
|
|
|
});
|
|
|
|
|
2020-04-09 18:12:32 +00:00
|
|
|
after(async() => {
|
2018-09-13 19:08:51 +00:00
|
|
|
await Promise.all([
|
|
|
|
state.server.stop(),
|
|
|
|
state.browser.close()
|
|
|
|
]);
|
|
|
|
state.browser = null;
|
|
|
|
state.server = null;
|
|
|
|
});
|
|
|
|
|
2020-04-09 18:12:32 +00:00
|
|
|
beforeEach(async() => {
|
2018-09-13 19:08:51 +00:00
|
|
|
state.page = await state.browser.newPage();
|
|
|
|
await state.page.evaluateOnNewDocument(puppeteerWeb);
|
|
|
|
await state.page.addScriptTag({
|
|
|
|
content: puppeteerWeb + '\n//# sourceURL=puppeteer-web.js'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-09 18:12:32 +00:00
|
|
|
afterEach(async() => {
|
2018-09-13 19:08:51 +00:00
|
|
|
await state.page.close();
|
|
|
|
state.page = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Puppeteer-Web', () => {
|
2020-04-09 18:12:32 +00:00
|
|
|
it('should work over web socket', async() => {
|
|
|
|
const {page, serverConfig} = state;
|
2018-11-13 07:26:16 +00:00
|
|
|
const browser2 = await puppeteer.launch();
|
2018-09-13 19:08:51 +00:00
|
|
|
// Use in-page puppeteer to create a new page and navigate it to the EMPTY_PAGE
|
|
|
|
await page.evaluate(async(browserWSEndpoint, serverConfig) => {
|
|
|
|
const puppeteer = require('puppeteer');
|
|
|
|
const browser = await puppeteer.connect({browserWSEndpoint});
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(serverConfig.EMPTY_PAGE);
|
|
|
|
}, browser2.wsEndpoint(), serverConfig);
|
|
|
|
const pageURLs = (await browser2.pages()).map(page => page.url()).sort();
|
|
|
|
expect(pageURLs).toEqual([
|
|
|
|
'about:blank',
|
|
|
|
serverConfig.EMPTY_PAGE
|
|
|
|
]);
|
|
|
|
await browser2.close();
|
|
|
|
});
|
2020-04-09 18:12:32 +00:00
|
|
|
it('should work over exposed DevTools protocol', async() => {
|
|
|
|
const {browser, page, serverConfig} = state;
|
2018-09-20 18:55:23 +00:00
|
|
|
// Expose devtools protocol binding into page.
|
|
|
|
const session = await browser.target().createCDPSession();
|
|
|
|
const pageInfo = (await session.send('Target.getTargets')).targetInfos.find(info => info.attached);
|
|
|
|
await session.send('Target.exposeDevToolsProtocol', {targetId: pageInfo.targetId});
|
|
|
|
await session.detach();
|
|
|
|
|
2020-04-08 10:18:29 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
|
2018-09-20 18:55:23 +00:00
|
|
|
// Use in-page puppeteer to create a new page and navigate it to the EMPTY_PAGE
|
|
|
|
await page.evaluate(async serverConfig => {
|
|
|
|
const puppeteer = require('puppeteer');
|
|
|
|
window.cdp.close = () => {};
|
|
|
|
const browser = await puppeteer.connect({transport: window.cdp});
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(serverConfig.EMPTY_PAGE);
|
|
|
|
}, serverConfig);
|
|
|
|
const pageURLs = (await browser.pages()).map(page => page.url()).sort();
|
|
|
|
expect(pageURLs).toEqual([
|
|
|
|
'about:blank',
|
|
|
|
'about:blank',
|
|
|
|
serverConfig.EMPTY_PAGE
|
|
|
|
]);
|
|
|
|
});
|
2018-09-13 19:08:51 +00:00
|
|
|
});
|