/** * 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, expect, stripAnsi } from './playwright-test-fixtures'; import fs from 'fs'; import path from 'path'; import { spawnSync } from 'child_process'; import { registry } from '../../packages/playwright-core/lib/utils/registry'; const ffmpeg = registry.findExecutable('ffmpeg')!.executablePath(); export class VideoPlayer { videoWidth: number; videoHeight: number; constructor(fileName: string) { const output = spawnSync(ffmpeg, ['-i', fileName, '-r', '25', `${fileName}-%03d.png`]).stderr.toString(); const lines = output.split('\n'); const streamLine = lines.find(l => l.trim().startsWith('Stream #0:0')); const resolutionMatch = streamLine.match(/, (\d+)x(\d+),/); this.videoWidth = parseInt(resolutionMatch![1], 10); this.videoHeight = parseInt(resolutionMatch![2], 10); } } test('should respect viewport option', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { viewport: { width: 800, height: 800 } } }; `, 'a.test.ts': ` const { test } = pwt; test('pass', async ({ page }) => { expect(page.viewportSize()).toEqual({ width: 800, height: 800 }); }); `, 'b.test.ts': ` const { test } = pwt; test.use({ viewport: { width: 600, height: 600 } }); test('pass', async ({ page }) => { expect(page.viewportSize()).toEqual({ width: 600, height: 600 }); }); `, }, { workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(2); }); test('should run in three browsers with --browser', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { viewport: { width: 800, height: 800 } } }; `, 'a.test.ts': ` const { test } = pwt; test('pass', async ({ page, browserName }) => { expect(page.viewportSize()).toEqual({ width: 800, height: 800 }); console.log('\\n%%browser=' + browserName); }); `, }, { browser: 'all', workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(3); expect(result.output.split('\n').filter(line => line.startsWith('%%')).sort()).toEqual([ '%%browser=chromium', '%%browser=firefox', '%%browser=webkit', ]); }); test('should run in one browser with --browser', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { viewport: { width: 800, height: 800 } } }; `, 'a.test.ts': ` const { test } = pwt; test('pass', async ({ page, browserName }) => { expect(page.viewportSize()).toEqual({ width: 800, height: 800 }); console.log('\\n%%browser=' + browserName); }); `, }, { browser: 'webkit', workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); expect(result.output.split('\n').filter(line => line.startsWith('%%')).sort()).toEqual([ '%%browser=webkit', ]); }); test('should complain with projects and --browser', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { projects: [ {} ] }; `, 'a.test.ts': ` const { test } = pwt; test('pass', async ({ page }) => { }); `, }, { browser: 'webkit', workers: 1 }); expect(result.exitCode).toBe(1); expect(result.passed).toBe(0); expect(result.output).toContain('Cannot use --browser option when configuration file defines projects'); }); test('should not override use:browserName without projects', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { browserName: 'webkit' } }; `, 'a.test.ts': ` const { test } = pwt; test('pass', async ({ page, browserName }) => { console.log('\\n%%browser=' + browserName); }); `, }, { workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); expect(result.output.split('\n').filter(line => line.startsWith('%%')).sort()).toEqual([ '%%browser=webkit', ]); }); test('should override use:browserName with --browser', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { browserName: 'webkit' } }; `, 'a.test.ts': ` const { test } = pwt; test('pass', async ({ page, browserName }) => { console.log('\\n%%browser=' + browserName); }); `, }, { browser: 'firefox', workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); expect(result.output.split('\n').filter(line => line.startsWith('%%')).sort()).toEqual([ '%%browser=firefox', ]); }); test('should respect context options in various contexts', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { viewport: { width: 500, height: 500 } } }; `, 'a.test.ts': ` import fs from 'fs'; import os from 'os'; import path from 'path'; import rimraf from 'rimraf'; const { test } = pwt; test.use({ locale: 'fr-CH' }); let context; test.beforeAll(async ({ browser }) => { context = await browser.newContext(); }); test.afterAll(async () => { await context.close(); }); test('shared context', async ({}) => { const page = await context.newPage(); expect(page.viewportSize()).toEqual({ width: 500, height: 500 }); expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); }); test('own context', async ({ browser }) => { const page = await browser.newPage(); expect(page.viewportSize()).toEqual({ width: 500, height: 500 }); expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); await page.close(); }); test('default context', async ({ page }) => { expect(page.viewportSize()).toEqual({ width: 500, height: 500 }); expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); }); test('persistent context', async ({ playwright, browserName }) => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'user-data-dir-')); const context = await playwright[browserName].launchPersistentContext(dir); const page = context.pages()[0]; expect(page.viewportSize()).toEqual({ width: 500, height: 500 }); expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); await context.close(); rimraf.sync(dir); }); test('another browser', async ({ playwright, browserName }) => { const browser = await playwright.webkit.launch(); const page = await browser.newPage(); expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); await browser.close(); }); `, }, { workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(5); }); test('should respect headless in launchPersistent', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { headless: false } }; `, 'a.test.ts': ` import fs from 'fs'; import os from 'os'; import path from 'path'; import rimraf from 'rimraf'; const { test } = pwt; test('persistent context', async ({ playwright, browserName }) => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'user-data-dir-')); const context = await playwright[browserName].launchPersistentContext(dir); const page = context.pages()[0]; expect(await page.evaluate(() => navigator.userAgent)).not.toContain('Headless'); await context.close(); rimraf.sync(dir); }); `, }, { workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); }); test('should respect headless in modifiers that run before tests', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': ` module.exports = { use: { headless: false } }; `, 'a.test.ts': ` const { test } = pwt; test.skip(({ browser }) => false); test('should work', async ({ page }) => { expect(await page.evaluate(() => navigator.userAgent)).not.toContain('Headless'); }); `, }, { workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); }); test('should call logger from launchOptions config', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ 'a.test.ts': ` const { test } = pwt; const log = []; test.use({ launchOptions: { logger: { log: (name, severity, message) => log.push({name, severity, message}), isEnabled: (name, severity) => severity !== 'verbose' } } }); test('should support config logger', async ({browser, context}) => { expect(browser.version()).toBeTruthy(); expect(log.length > 0).toBeTruthy(); expect(log.filter(item => item.severity === 'info').length > 0).toBeTruthy(); expect(log.filter(item => item.message.includes('browser.newContext started')).length > 0).toBeTruthy(); expect(log.filter(item => item.message.includes('browser.newContext succeeded')).length > 0).toBeTruthy(); }); `, }, { workers: 1 }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); }); test('should report error and pending operations on timeout', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ 'a.test.ts': ` const { test } = pwt; test('timedout', async ({ page }) => { await page.setContent('