Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Conversation

jrolfs
Copy link
Member

@jrolfs jrolfs commented Jun 1, 2022

New fixture for @playwright/test that is based on, and built with, more modern Playwright APIs. Queries return Locator instead of ElementHandle and configuration is integrated into @playwright/test's configuration (global andtest.use.

Internally, @testing-library/dom is integrated via a custom selector engine. The integration is loaded via a [worker fixture]https://playwright.dev/docs/test-fixtures#worker-scoped-fixtures), ensuring everything works well with @playwright/test's parallelism.

This API will replace the legacy ElementHandle-based APIs in the next major release. However, before that happens, we will also make it compatible with vanilla playwright (without @playwright/test).

This is comprised of the following pull requests, which were released as 4.x-beta.n:

@jrolfs jrolfs self-assigned this Jun 1, 2022
@jrolfs jrolfs closed this Jun 7, 2022
@jrolfs jrolfs reopened this Jun 7, 2022
@jrolfs jrolfs changed the title 🔖 5.0 Beta 📦 Beta Aug 26, 2022
@jrolfs jrolfs force-pushed the beta branch 2 times, most recently from 43a2716 to bd7fb5c Compare September 1, 2022 20:34
@jrolfs
Copy link
Member Author

jrolfs commented Sep 2, 2022

Uggggh, Semantic Release got out of sync somehow and it's trying to release an already released version 😩

@jrolfs jrolfs force-pushed the beta branch 3 times, most recently from c01975d to 05021a7 Compare September 8, 2022 17:41
@jrolfs jrolfs requested review from gajus and sebinsua September 17, 2022 20:12
@jrolfs jrolfs marked this pull request as ready for review September 17, 2022 20:12
…ries

This will likely replace the fixtures that provided `ElementHandle`-based
queries in a future major release, but for now the `Locator` queries are
exported as `locatorFixtures`:

```ts
import { test as baseTest } from '@playwright/test'
import {
  locatorFixtures as fixtures,
  LocatorFixtures as TestingLibraryFixtures,
  within
} from '@playwright-testing-library/test/fixture';

const test = baseTest.extend<TestingLibraryFixtures>(fixtures);

const {expect} = test;

test('my form', async ({queries: {getByTestId}}) => {
  // Queries now return `Locator`
  const formLocator = getByTestId('my-form');

  // Locator-based `within` support
  const {getByLabelText} = within(formLocator);

  const emailInputLocator = getByLabelText('Email');

  // Interact via `Locator` API 🥳
  await emailInputLocator.fill('email@playwright.dev');

  // Assert via `Locator` APIs 🎉
  await expect(emailInputLocator).toHaveValue('email@playwright.dev');
})
```
### Global

```ts
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  use: {
    testIdAttribute: 'data-custom-test-id',
    asyncUtilsTimeout: 5000,
  },
};

export default config;
```

### Local

```ts
import { test as baseTest } from '@playwright/test'
import {
  locatorFixtures as fixtures,
  LocatorFixtures as TestingLibraryFixtures,
  within
} from '@playwright-testing-library/test/fixture';

const test = baseTest.extend<TestingLibraryFixtures>(fixtures);

const {expect} = test;

// Entire test suite
test.use({ testIdAttribute: 'data-custom-test-id' });

test.describe(() => {
  // Specific block
  test.use({
    testIdAttribute: 'some-other-test-id',
    asyncUtilsTimeout: 5000,
  });

  test('my form', async ({queries: {getByTestId}}) => {
    // ...
  });
});
```
(until we have an official API for \`Locator\` queries that doesn't require
a **@playwright/test** fixture)
This will likely replace the `queries` fixture when the `Locator` fixture
stuff is officially released
Support `Page` in addition to `Locator` in `within()` fixture. When
a `Page` instance is passed to `within()`, a `Screen` instance will be
returned which includes the entire `Page` API in addition to the query
methods.

```ts
test('within page', async ({ page }) => {
  // Passing `page` to `within` returns a `Screen` instance
  const screen = within(page)

  await screen.goto(/* ... */)

  const form = screen.queryByRole('form', {name: 'User'})

  // Passing a `Locator` returns scoped queries
  const {queryByLabelText} = within(form)

  // ...
})
```
@jrolfs jrolfs changed the title 📦 Beta ✨ New locator based fixtures (4.4.0-beta.6) Sep 18, 2022
@jrolfs jrolfs merged commit 1b41bd5 into main Sep 18, 2022
@jrolfs jrolfs deleted the beta branch September 18, 2022 11:52
@github-actions
Copy link

🎉 This PR is included in version 4.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant