diff --git a/docs/src/library-js.md b/docs/src/library-js.md index f861ccd9bc..e4925db6f9 100644 --- a/docs/src/library-js.md +++ b/docs/src/library-js.md @@ -3,11 +3,136 @@ id: library title: "Library" --- -Playwright can either be used as a part of the [Playwright Test](./intro.md), or as a Playwright Library (this guide). If you are working on an application that utilizes Playwright capabilities or you are using Playwright with another test runner, read on. +Playwright Library provides unified APIs for launching and interacting with browsers, while Playwright Test provides all this plus a fully managed end-to-end Test Runner and experience. + +Under most circumstances, for end-to-end testing, you'll want to use `@playwright/test` (Playwright Test), and not `playwright` (Playwright Library) directly. To get started with Playwright Test, follow its [Getting Started Guide](./intro.md). - [Release notes](./release-notes.md) +## When Should Playwright Library Be Used Directly? + +- creating an integration for a third party test runner (e.g. the third-party runner plugins listed [here](./test-runners.md) are built on top of Playwright Library) +- automation and scraping + +## Differences + +### Library Example + +The following is an example of using the Playwright Library directly to launch Chromium, go to a page, and check its title: + + +```js tab=js-ts +import playwright, { devices } from 'playwright'; + +(async () => { + // Setup + const browser = await playwright.chromium.launch(); + const context = await browser.newContext(devices['iPhone 11']); + const page = await context.newPage(); + + // The actual interesting bit + await context.route('**.jpg', route => route.abort()); + await page.goto('https://example.com/'); + + assert(await page.title() === 'Example'); // 👎 not a Web First assertion + + // Teardown + await context.close(); + await browser.close(); +})() +``` + +```js tab=js-js +const playwright = require('playwright'); + +(async () => { + // Setup + const browser = await playwright.chromium.launch(); + const context = await browser.newContext(devices['iPhone 11']); + const page = await context.newPage(); + + // The actual interesting bit + await context.route('**.jpg', route => route.abort()); + await page.goto('https://example.com/'); + + assert(await page.title() === 'Example'); // 👎 not a Web First assertion + + // Teardown + await context.close(); + await browser.close(); +})() +``` + +Run via: + +```bash tab=js-ts +node ./my-script.ts +``` + +```bash tab=js-js +node ./my-script.js +``` + +### Test Example + +A test to achieve similar behavior, would look like: + +```js tab=js-ts +import { expect, test, devices } from '@playwright/test'; + +test.use(devices['iPhone 11']); + +test('should be titled', async ({ page, context }) => { + await context.route('**.jpg', route => route.abort()); + await page.goto('https://example.com/'); + + await expect(page).toHaveTitle('Example'); +}); +``` + +```js tab=js-js +const { expect, test, devices } = require('@playwright/test'); + +test.use(devices['iPhone 11']); + +test('should be titled', async ({ page, context }) => { + await context.route('**.jpg', route => route.abort()); + await page.goto('https://example.com/'); + + await expect(page).toHaveTitle('Example'); +}); +``` + +Run via: + +``` +npx playwright test +``` + +### Key Differences + +The key differences to note are as follows: + +| | Library | Test | +| - | - | - | +| Installation | `npm install playwright` | `npm init playwright@latest` (note `install` vs. `init`) | +| `import`/`require` name | `playwright` | `@playwright/test` | +| Initialization | Explicitly need to: