playwright-automation-library
v1.0.0
Published
Reusable Playwright library for automation testing
Maintainers
Readme
playwright-automation-library
Reusable Playwright npm library for automation testing — companion to cypress-automation-library and infy-mcp-playwright.
Features
- Action helpers —
safeClick,fillField,login,getByTestId, and more - Fixtures —
autofixture with all helpers bound topage - Utilities — selectors, wait helpers, API helpers, test data generators
- Config helper — easy Playwright config setup via
registerConfig - TypeScript — full type definitions included
Installation
npm install playwright-automation-library @playwright/test
npx playwright installQuick Start
Option 1: Use the auto fixture (recommended)
// tests/login.spec.ts
import { test, expect } from 'playwright-automation-library';
test('logs in successfully', async ({ auto }) => {
await auto.login('[email protected]', 'password123');
await expect(auto.getByTestId('dashboard')).toBeVisible();
});Option 2: Import helpers directly
import { test, expect } from '@playwright/test';
import { safeClick, fillField, login, getByTestId } from 'playwright-automation-library';
test('fills a form safely', async ({ page }) => {
await page.goto('/contact');
await fillField(page, '[name="email"]', '[email protected]');
await safeClick(page, '[type="submit"]');
await expect(getByTestId(page, 'success-message')).toBeVisible();
});Configure Playwright (optional)
// playwright.config.ts
import { defineConfig } from '@playwright/test';
import { registerConfig } from 'playwright-automation-library';
export default defineConfig(
registerConfig(
{
testDir: './tests',
fullyParallel: true,
},
{
baseURL: 'https://your-app.com',
viewport: { width: 1280, height: 720 },
actionTimeout: 10000,
}
)
);Action Helpers
| Helper | Description |
|--------|-------------|
| safeClick(page, selector) | Click only after element is visible |
| fillField(page, selector, value) | Clear and fill an input |
| clearAndType(page, selector, text) | Clear field then type |
| waitForVisible(page, selector) | Wait until element is visible |
| waitForHidden(page, selector) | Wait until element is hidden |
| getByTestId(page, testId) | Select by data-testid |
| getByRole(page, role, options?) | Select by ARIA role |
| getByLabel(page, label) | Find input by label text |
| getByPlaceholder(page, placeholder) | Find input by placeholder |
| login(page, user, pass, options?) | Reusable login flow |
| uploadFile(page, selector, filePath) | Upload a file |
| selectDropdown(page, selector, value) | Select dropdown value |
| scrollToElement(page, selector) | Scroll element into view |
| getTableRow(page, row) | Get table row by index |
| getTableCell(page, row, col) | Get table cell by row/column |
| withinIframe(page, selector, callback) | Run actions inside iframe |
| hoverElement(page, selector) | Hover over element |
| checkCheckbox(page, selector) | Check a checkbox |
| uncheckCheckbox(page, selector) | Uncheck a checkbox |
| assertText(page, selector, text) | Assert element contains text |
| assertUrl(page, path) | Assert URL contains path |
| screenshotNamed(page, name) | Take named screenshot |
| mockApiResponse(page, method, url, response) | Mock API response |
| waitForApi(page, url) | Wait for API response |
| dragAndDrop(page, source, target) | Drag and drop elements |
| closeModal(page, selector?) | Close modal/dialog |
| typeSlow(page, selector, text, delay?) | Slow typing for animations |
| doubleClickElement(page, selector) | Double click element |
| forceClick(page, selector) | Force click element |
Utilities
import {
byTestId,
byRole,
byPlaceholder,
buildSelector,
waitForStable,
generateEmail,
generatePhone,
generatePassword,
formatDate,
setAuthToken,
apiRequest,
mockApiResponse,
} from 'playwright-automation-library';
// Selector helpers
const btn = byTestId('submit-btn');
const emailInput = byPlaceholder('Enter email');
// Test data
const email = generateEmail('user');
const phone = generatePhone();
const date = formatDate(7); // 7 days from now
// API request with auth token
setAuthToken('your-jwt-token');
const response = await apiRequest(request, 'GET', '/api/users');Development
npm install
npm run build
npm run lintPublish to npm
npm login
npm pack --dry-run
npm publishPush to GitHub
git init
git add .
git commit -m "Initial commit: Playwright automation library"
git remote add origin https://github.com/sujeet0414/playwright-automation-library.git
git push -u origin mainLicense
MIT
