GitXplorerGitXplorer
e

playwright-expect

public
5 stars
1 forks
0 issues

Commits

List of commits on branch main.
Verified
449413906c6416e07a97fbed898fdf0b54e410d0

update(readme): deprecate repo

eelaichenkov committed 3 years ago
Unverified
40ddc6a0ad088d20c5a3eec1beb39775ce7a48c9

update(docs): change month to year commits label

eelaichenkov committed 4 years ago
Unverified
daf38acbd63521c7b0a91660793de4273950eca9

update(docs): add labels in readme

eelaichenkov committed 4 years ago
Unverified
dbed52bf922a01fb8b04a557a7c966262f2324c0

Release 0.1.2

eelaichenkov committed 4 years ago
Unverified
69e604f50253cdc62d5ebe681b9273ac4d78665f

update(docs): add article in the readme

eelaichenkov committed 4 years ago
Unverified
47ef606f32ebe643370cf7fb71bcb23ae516b5d1

update(types): add examples to the types

eelaichenkov committed 4 years ago

README

The README file for this repository.

Deprecated!

Please, use built-in Playwright assertions.

playwright-expect

Test Total npm downloads NPM version Commits MIT licensed

The playwright-expect is an assertion library for TypeScript and JavaScript intended for use with a test runner such as Jest or Playwright Test. It lets you write better assertions for end-to-end testing.

Motivation

TL;DR:

expect-playwright is a great library, but it contains a few methods.

playwright-expect is a great library too, with all major methods and extra features such as waits, ignore case sensitive, trim. All in all, It has everything that you demand to accomplish end-to-end testing needs.

Please, read more about motivation and main features.

Key Features

  • rich and easy to use;
  • exhaustive messages and diff highlights;
  • can ignore case sensitive and trim values before asserting;
  • waits for expectation to succeed;
  • works in Jest and Playwright Test;
  • built-in types for TypeScript and JavaScript autocompletion.

Usage

Install

npm i -D playwright-expect

Playwright Test - TypeScript

// playwright.config.ts
import { expect } from '@playwright/test';
import { matchers } from 'playwright-expect';

// add custom matchers
expect.extend(matchers);

Playwright Test - JavaScript

// playwright.config.js
const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');

// add custom matchers
expect.extend(matchers);

Please, read API documentation

Examples

All methods, which expects element can accept element in three ways:

  1. [page, selector] (recommended)
  2. ElementHandle
  3. Promise<ElementHandle>

Use toHaveText to check that element's text equals to the expected

// Using ElementHandle
const title = await page.$('h1');

await expect(title).toHaveText('Home');

// Using Promise<ElementHandle>
await expect(page.$('h1')).toHaveText('Home');

// Using an array of page and selector. Furthermore, you can pass options such as ignoreCase and trim
await expect([page, 'h1']).toHaveText('home', { ignoreCase: true });

// Even more, you can wait for the element before asserting
await expect([page, '.notification']).toHaveText('Success', { timeout: 15000 });

// Also, it could be useful to fail fast, by default it waits for the 10 seconds
await expect([page, '.notification']).toHaveText('success', { timeout: 1000, trim: true });

NOTE: You can wait for the element only using the [page, selector] approach

Use toBeVisible to check that element is visible

// Using ElementHandle
const button = await page.$('#next');

await expect(title).toBeVisible();

// Using Promise<ElementHandle>
await expect(page.$('#next')).toBeVisible(true); // true here is optional

// Using an array of page and selector
await expect([page, '#next']).toBeVisible(false);

Use toBeEnabled and toBeDisabled to check that element is enabled/disabled

// Using ElementHandle
const button = await page.$('#next');

await expect(title).toBeEnabled();

// Using Promise<ElementHandle>
await expect(page.$('#next')).toBeEnabled();

// Using an array of page and selector
await expect([page, '#next']).toBeEnabled(false);

// Also, you can use `not` to verify opposite
await expect([page, '#next']).not.toBeEnabled();

// Even more, you can check that element is disabled
await expect(page.$('#next')).toBeDisabled();

Use toHaveUrl and toContainUrl to check that page's url equals or contains the expected url

await expect(page).toHaveUrl('https://duckduckgo.com/');

// Also, you can wait for the url
await expect(page).toHaveUrl('https://duckduckgo.com/', { timeout: 5000 });

await expect(page).toContainUrl('duck');

Use toHaveTitle or toContainTitle to check that page's title equals or contains the expected title

await expect(page).toHaveTitle('DuckDuckGo — Privacy, simplified.');

await expect(page).toContainTitle('Privacy');

// ignore case sensitive
await expect(page).toContainTitle('privacy', {ignoreCase: true});

Author

Yevhen Laichenkov elaichenkov@gmail.com

Inspired by

expect-playwright

expect-webdriverio