diff options
Diffstat (limited to 'remote/test/puppeteer/test/src/cdp')
4 files changed, 160 insertions, 6 deletions
diff --git a/remote/test/puppeteer/test/src/cdp/CDPSession.spec.ts b/remote/test/puppeteer/test/src/cdp/CDPSession.spec.ts index 2000c0e435..887152f097 100644 --- a/remote/test/puppeteer/test/src/cdp/CDPSession.spec.ts +++ b/remote/test/puppeteer/test/src/cdp/CDPSession.spec.ts @@ -30,7 +30,7 @@ describe('Target.createCDPSession', function () { }); it('should not report created targets for custom CDP sessions', async () => { - const {browser} = await getTestState(); + const {context} = await getTestState(); let called = 0; const handler = async (target: Target) => { called++; @@ -39,9 +39,9 @@ describe('Target.createCDPSession', function () { } await target.createCDPSession(); }; - browser.browserContexts()[0]!.on('targetcreated', handler); - await browser.newPage(); - browser.browserContexts()[0]!.off('targetcreated', handler); + context.on('targetcreated', handler); + await context.newPage(); + context.off('targetcreated', handler); }); it('should send events', async () => { diff --git a/remote/test/puppeteer/test/src/cdp/devtools.spec.ts b/remote/test/puppeteer/test/src/cdp/devtools.spec.ts index c158481af2..c48b4c353b 100644 --- a/remote/test/puppeteer/test/src/cdp/devtools.spec.ts +++ b/remote/test/puppeteer/test/src/cdp/devtools.spec.ts @@ -93,7 +93,7 @@ describe('DevTools', function () { const browser = await launchBrowser( Object.assign({devtools: true}, launchOptions) ); - const context = await browser.createIncognitoBrowserContext(); + const context = await browser.createBrowserContext(); await Promise.all([ context.newPage(), browser.waitForTarget((target: {url: () => string | string[]}) => { @@ -106,7 +106,7 @@ describe('DevTools', function () { const browser = await launchBrowser( Object.assign({devtools: true}, launchOptions) ); - const context = await browser.createIncognitoBrowserContext(); + const context = await browser.createBrowserContext(); const [target] = await Promise.all([ browser.waitForTarget((target: {url: () => string | string[]}) => { return target.url().includes('devtools://'); diff --git a/remote/test/puppeteer/test/src/cdp/pdf.spec.ts b/remote/test/puppeteer/test/src/cdp/pdf.spec.ts new file mode 100644 index 0000000000..06a41de36f --- /dev/null +++ b/remote/test/puppeteer/test/src/cdp/pdf.spec.ts @@ -0,0 +1,55 @@ +/** + * @license + * Copyright 2017 Google Inc. + * SPDX-License-Identifier: Apache-2.0 + */ + +import {readFile, unlink} from 'fs/promises'; + +import expect from 'expect'; + +import {getTestState, setupTestBrowserHooks} from '../mocha-utils.js'; + +describe('Page.pdf', () => { + setupTestBrowserHooks(); + + it('can print to PDF with accessible', async () => { + const {page, server} = await getTestState(); + + const outputFile = __dirname + '/../../assets/output.pdf'; + const outputFileAccessible = + __dirname + '/../../assets/output-accessible.pdf'; + await page.goto(server.PREFIX + '/pdf.html'); + await page.pdf({path: outputFile, tagged: false}); + await page.pdf({path: outputFileAccessible, tagged: true}); + try { + const [base, tagged] = await Promise.all([ + readFile(outputFile), + readFile(outputFileAccessible), + ]); + expect(tagged.byteLength).toBeGreaterThan(base.byteLength); + } finally { + await Promise.all([unlink(outputFile), unlink(outputFileAccessible)]); + } + }); + + it('can print to PDF with outline', async () => { + const {page, server} = await getTestState(); + + const outputFile = __dirname + '/../../assets/output.pdf'; + const outputFileOutlined = __dirname + '/../../assets/output-outlined.pdf'; + await page.goto(server.PREFIX + '/pdf.html'); + await page.pdf({path: outputFile, tagged: true}); + await page.pdf({path: outputFileOutlined, tagged: true, outline: true}); + try { + const [base, outlined] = await Promise.all([ + readFile(outputFile), + readFile(outputFileOutlined), + ]); + + expect(outlined.byteLength).toBeGreaterThan(base.byteLength); + } finally { + await Promise.all([unlink(outputFile), unlink(outputFileOutlined)]); + } + }); +}); diff --git a/remote/test/puppeteer/test/src/cdp/screencast.spec.ts b/remote/test/puppeteer/test/src/cdp/screencast.spec.ts new file mode 100644 index 0000000000..2833ff4d67 --- /dev/null +++ b/remote/test/puppeteer/test/src/cdp/screencast.spec.ts @@ -0,0 +1,99 @@ +/** + * @license + * Copyright 2023 Google Inc. + * SPDX-License-Identifier: Apache-2.0 + */ + +import {statSync} from 'fs'; + +import expect from 'expect'; + +import {getTestState, setupTestBrowserHooks} from '../mocha-utils.js'; +import {getUniqueVideoFilePlaceholder} from '../utils.js'; + +describe('Screencasts', function () { + setupTestBrowserHooks(); + + describe('Page.screencast', function () { + it('should work', async () => { + using file = getUniqueVideoFilePlaceholder(); + + const {page} = await getTestState(); + + const recorder = await page.screencast({ + path: file.filename, + scale: 0.5, + crop: {width: 100, height: 100, x: 0, y: 0}, + speed: 0.5, + }); + + await page.goto('data:text/html,<input>'); + using input = await page.locator('input').waitHandle(); + await input.type('ab', {delay: 100}); + + await recorder.stop(); + + expect(statSync(file.filename).size).toBeGreaterThan(0); + }); + it('should work concurrently', async () => { + using file1 = getUniqueVideoFilePlaceholder(); + using file2 = getUniqueVideoFilePlaceholder(); + + const {page} = await getTestState(); + + const recorder = await page.screencast({path: file1.filename}); + const recorder2 = await page.screencast({path: file2.filename}); + + await page.goto('data:text/html,<input>'); + using input = await page.locator('input').waitHandle(); + + await input.type('ab', {delay: 100}); + await recorder.stop(); + + await input.type('ab', {delay: 100}); + await recorder2.stop(); + + // Since file2 spent about double the time of file1 recording, so file2 + // should be around double the size of file1. + const ratio = + statSync(file2.filename).size / statSync(file1.filename).size; + + // We use a range because we cannot be precise. + const DELTA = 1.3; + expect(ratio).toBeGreaterThan(2 - DELTA); + expect(ratio).toBeLessThan(2 + DELTA); + }); + it('should validate options', async () => { + const {page} = await getTestState(); + + await expect(page.screencast({scale: 0})).rejects.toBeDefined(); + await expect(page.screencast({scale: -1})).rejects.toBeDefined(); + + await expect(page.screencast({speed: 0})).rejects.toBeDefined(); + await expect(page.screencast({speed: -1})).rejects.toBeDefined(); + + await expect( + page.screencast({crop: {x: 0, y: 0, height: 1, width: 0}}) + ).rejects.toBeDefined(); + await expect( + page.screencast({crop: {x: 0, y: 0, height: 0, width: 1}}) + ).rejects.toBeDefined(); + await expect( + page.screencast({crop: {x: -1, y: 0, height: 1, width: 1}}) + ).rejects.toBeDefined(); + await expect( + page.screencast({crop: {x: 0, y: -1, height: 1, width: 1}}) + ).rejects.toBeDefined(); + await expect( + page.screencast({crop: {x: 0, y: 0, height: 10000, width: 1}}) + ).rejects.toBeDefined(); + await expect( + page.screencast({crop: {x: 0, y: 0, height: 1, width: 10000}}) + ).rejects.toBeDefined(); + + await expect( + page.screencast({ffmpegPath: 'non-existent-path'}) + ).rejects.toBeDefined(); + }); + }); +}); |