Skip to content

jaktestowac/playwright-tools

Repository files navigation

🎭 playwright-tools

A comprehensive collection of utilities for Playwright testing that simplify common testing patterns and enhance your automation workflow.

TypeScript NPM Version License


✨ Features

  • πŸ›‘οΈ Safe Element Interactions - Built-in waits and error handling for reliable interactions
  • πŸ”„ Retry Mechanisms - Exponential backoff retry logic for flaky operations
  • πŸ“Έ Enhanced Screenshots - Timestamped screenshots with debugging annotations
  • πŸ” Element Queries - Existence checks and bulk operations without throwing errors
  • 🌐 Navigation Utilities - Safe navigation with loading state checks
  • ⌨️ Advanced Interactions - File uploads, drag & drop, keyboard shortcuts
  • πŸ“Š Table Utilities - Extract and validate table data efficiently
  • 🎯 Accessibility Helpers - Check ARIA attributes and accessibility properties
  • πŸ’Ύ Storage Management - Handle localStorage and sessionStorage operations
  • 🌍 Network Helpers - Wait for specific requests and monitor network activity
  • πŸ”§ Page Objects - Reusable page object patterns and utilities
  • πŸ“ Test Data Factories - Generate consistent test data across tests
  • πŸ“‹ Assertions - Enhanced assertion helpers for multiple elements
  • ⚑ Test Utilities - Filter and dispatch tests with skip/runOnly flags
  • πŸ§ͺ TypeScript Support - Full type safety with comprehensive TypeScript definitions
  • πŸͺΆ Zero Dependencies - No additional dependencies beyond Playwright

πŸ“¦ Installation

npm install playwright-tools

Peer Dependency: This package requires @playwright/test to be installed in your project.

npm install @playwright/test

πŸš€ Quick Start

Basic Usage

import { safeClick, safeFill, elementExists } from "playwright-tools";

// Safe element interactions
await safeClick(page.locator("#submit-button"));
await safeFill(page.locator("#username"), "john.doe@example.com");

// Check element existence without throwing
const exists = await elementExists(page.locator("#optional-element"));

Modular Imports

Import specific modules for better tree-shaking:

// Import specific modules
import { safeClick, safeFill } from "playwright-tools/interactions";
import { waitForElements, elementExists } from "playwright-tools/element-queries";
import { takeTimestampedScreenshot } from "playwright-tools/screenshots";
import { retryAction } from "playwright-tools/retry";

// Or import everything
import * as pwUtils from "playwright-tools";

πŸ“š Documentation

πŸ“– Complete documentation is available in the docs directory:

Guides & Examples


πŸ’‘ Quick Examples

Form Handling

import { fillForm, safeClick } from "playwright-tools";

await fillForm([
  { locator: page.locator("#username"), value: "john.doe" },
  { locator: page.locator("#password"), value: "secure123" },
  { locator: page.locator("#email"), value: "john@example.com" },
]);

await safeClick(page.locator("#submit"));

Element Queries

import { elementExists, waitForElements } from "playwright-tools";

// Check if element exists without throwing
const exists = await elementExists(page.locator("#optional-element"));

// Wait for multiple elements concurrently
await waitForElements([
  page.locator("#header"),
  page.locator("#navigation"),
  page.locator("#content"),
]);

Network Monitoring

import { waitForNetworkRequest } from "playwright-tools";

// Wait for specific API call
const response = await waitForNetworkRequest(page, "/api/users", {
  method: "GET",
  status: 200,
  timeout: 10000,
});

Retry Logic

import { retryAction } from "playwright-tools";

// Retry flaky operations with exponential backoff
const result = await retryAction(
  async () => {
    await page.click("#sometimes-slow-button");
    return await page.locator("#result").textContent();
  },
  { maxRetries: 3, baseDelay: 1000 }
);

Locator Decorator

The Locator Decorator lets you extend Playwright locators with custom methods, enhanced safe interactions, and even method overrides. This makes your locators more powerful, reusable, and tailored to your app.

Key Features

  • Add your own methods to any locator
  • Use enhanced methods like safeClick, safeFill, extractData, and more
  • Override existing locator methods (e.g., custom click)
  • Factory and page extension patterns for consistent usage

Quick Example

import { createEnhancedLocator, extendPage } from "playwright-tools";

// Enhance a single locator
const enhanced = createEnhancedLocator(page.locator("#my-btn"), {
  customMethods: {
    async highlight() {
      await this.evaluate(el => el.style.border = "2px solid red");
    }
  }
});
await enhanced.safeClick();
await enhanced.highlight();

// Or extend the whole page for consistent usage
const enhancedPage = extendPage(page, {
  customMethods: {
    async waitForAnimation() {
      await this.waitForFunction(() => !document.querySelector('[style*="animation"]'));
    }
  }
});
const btn = enhancedPage.locator("#my-btn");
await btn.safeClick();
await btn.waitForAnimation();

See the full documentation:
πŸ“– Locator Decorator Guide


πŸ”— Quick Links


πŸ“– Resources & Learning Materials

🦎 Practice Application

πŸ‡΅πŸ‡± Polish Resources

πŸ‡¬πŸ‡§ English Resources


πŸ“ž Contact & Support

Feel free to reach out to us:


🌟 Show Your Support

If you found this package helpful:

  • ⭐ Star this repository to show your support
  • πŸ”„ Share with your team to help spread knowledge about advanced Playwright testing patterns
  • πŸ—£οΈ Tell the community about your experience with playwright-tools
  • πŸ’ Contribute by submitting issues or pull requests

Happy testing and automating tests! πŸš€

jaktestowac.pl Team πŸ’šβ€οΈ

PS. For more resources and updates, follow us on our website and GitHub.


Built with πŸ’šβ€οΈ for the Playwright and testing automation community

GitHub stars GitHub forks GitHub issues

Releases

No releases published

Packages

No packages published