Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

6.9 KB
Raw Blame
/**
 * 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 fs from 'fs';
import { test, expect, daemonFolder } from './cli-fixtures';

test.describe.configure({
  retries: 1,
});

test('connect by channel name error', async ({ cli }) => {
  const { error } = await cli('attach', '--cdp=chrome-dev');
  expect(error).toContain('Could not connect to chrome-dev');
  expect(error).toContain('chrome://inspect/#remote-debugging');
});

test('attach --cdp=<channel> defaults session name to the channel', async ({ cli }) => {
  // Connection fails, but the daemon writes its err log under the resolved session name.
  await cli('attach', '--cdp=chrome-dev');
  const folder = await daemonFolder();
  expect(folder).toBeTruthy();
  const files = await fs.promises.readdir(folder!);
  expect(files).toContain('chrome-dev.err');
  expect(files).not.toContain('default.err');
});

test('explicit --session wins over --cdp=<channel>', async ({ cli }) => {
  await cli('attach', '--cdp=chrome-dev', '-s=explicit');
  const folder = await daemonFolder();
  expect(folder).toBeTruthy();
  const files = await fs.promises.readdir(folder!);
  expect(files).toContain('explicit.err');
  expect(files).not.toContain('chrome-dev.err');
});

test('attach via cdp URL keeps the default session', async ({ cdpServer, cli, server }) => {
  const browserContext = await cdpServer.start();
  const [page] = browserContext.pages();
  await page.goto(server.HELLO_WORLD);

  await cli('attach', `--cdp=${cdpServer.endpoint}`);
  const { output: listOutput } = await cli('list');
  expect(listOutput).toContain('- default:');
  expect(listOutput).toContain('(attached)');
});

test('attach via cdp URL honors PLAYWRIGHT_CLI_SESSION as session name', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright-cli/issues/414' } }, async ({ cdpServer, cli }) => {
  await cdpServer.start();
  const { exitCode } = await cli('attach', `--cdp=${cdpServer.endpoint}`, { env: { PLAYWRIGHT_CLI_SESSION: 'myname' } });
  expect(exitCode).toBe(0);
});

test('attach rejects combining --cdp, --endpoint, or --extension', async ({ cli }) => {
  const { error, exitCode } = await cli('attach', '--cdp=chrome-dev', '--endpoint=/tmp/foo');
  expect(exitCode).toBe(1);
  expect(error).toContain('only one of [name], --cdp, --endpoint, or --extension can be specified');
});

test('detach tears down an attached session', async ({ cdpServer, cli }) => {
  await cdpServer.start();

  await cli('attach', `--cdp=${cdpServer.endpoint}`, '-s=mine');
  const { output: listBefore } = await cli('list');
  expect(listBefore).toContain('- mine:');
  expect(listBefore).toContain('(attached)');

  const { output: detachOutput } = await cli('-s=mine', 'detach');
  expect(detachOutput).toContain(`Browser 'mine' detached`);

  const { output: listAfter } = await cli('list');
  expect(listAfter).not.toContain('- mine:');
});

test('detach rejects sessions opened via open', async ({ cli, server }) => {
  await cli('open', server.HELLO_WORLD);

  const { error, exitCode } = await cli('detach');
  expect(exitCode).toBe(1);
  expect(error).toContain(`session 'default' was not attached`);
  expect(error).toContain('close');
});

test('detach on unknown session reports not attached', async ({ cli }) => {
  const { output } = await cli('-s=nobody', 'detach');
  expect(output).toContain(`Browser 'nobody' is not attached.`);
});

test('cdp server', async ({ cdpServer, cli, server }) => {
  const browserContext = await cdpServer.start();
  const [page] = browserContext.pages();
  await page.goto(server.HELLO_WORLD);

  const configPath = test.info().outputPath('config.ini');
  await fs.promises.writeFile(configPath, `
browser.isolated=false
`);
  await cli('attach', `--cdp=${cdpServer.endpoint}`, `--config=${configPath}`);
  const { inlineSnapshot } = await cli('snapshot');
  expect(inlineSnapshot).toContain(`- generic [active] [ref=e1]: Hello, world!`);
});

test('attach via cdp', async ({ cdpServer, cli, server }) => {
  const browserContext = await cdpServer.start();
  const [page] = browserContext.pages();
  await page.goto(server.HELLO_WORLD);

  await cli('attach', `--cdp=${cdpServer.endpoint}`);
  const { inlineSnapshot } = await cli('snapshot');
  expect(inlineSnapshot).toContain(`- generic [active] [ref=e1]: Hello, world!`);
});

test('tracing-start-stop over cdp', async ({ cdpServer, cli, server }, testInfo) => {
  const browserContext = await cdpServer.start();
  const [page] = browserContext.pages();
  await page.goto(server.HELLO_WORLD);

  await cli('attach', `--cdp=${cdpServer.endpoint}`);

  const { output } = await cli('tracing-start');
  expect(output).toContain('Trace recording started');
  await cli('eval', '() => fetch("/hello-world")');

  const { output: tracingStopOutput } = await cli('tracing-stop');
  expect(tracingStopOutput).toContain('Trace recording stopped');
  const [, timestamp] = tracingStopOutput.match(/trace-(\d+)\.trace/);

  expect(fs.existsSync(testInfo.outputPath('.playwright-cli', 'traces', 'resources'))).toBeTruthy();
  expect(fs.existsSync(testInfo.outputPath('.playwright-cli', 'traces', `trace-${timestamp}.trace`))).toBeTruthy();
  expect(fs.existsSync(testInfo.outputPath('.playwright-cli', 'traces', `trace-${timestamp}.network`))).toBeTruthy();
});

test('attach via cdp with a crashed page', {
  annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/41714' },
}, async ({ cdpServer, cli, server }) => {
  const browserContext = await cdpServer.start();
  const [page] = browserContext.pages();
  await page.goto(server.HELLO_WORLD);
  const crashedPage = await browserContext.newPage();
  await crashedPage.goto(server.PREFIX + '/title.html');
  crashedPage.goto('chrome://crash').catch(() => {});
  await crashedPage.waitForEvent('crash');

  // Attaching should not hang on the crashed page, and should not list it.
  const { output: attachOutput, exitCode } = await cli('attach', `--cdp=${cdpServer.endpoint}`);
  expect(exitCode).toBe(0);
  expect(attachOutput).toContain(`### Page
- Page URL: ${server.HELLO_WORLD}
- Page Title: Title`);
  expect(attachOutput).not.toContain('### Open tabs');

  const { output: listOutput } = await cli('tab-list');
  expect(listOutput).toBe(`### Result
- 0: (current) [Title](${server.HELLO_WORLD})`);

  const { inlineSnapshot } = await cli('snapshot');
  expect(inlineSnapshot).toContain(`- generic [active] [ref=e1]: Hello, world!`);
});