Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 111 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,238 +1,236 @@
# LocatorPro for Playwright 🎯
# LocatorPro for Playwright 🎯 | Self-Healing Test Automation Locators

> **Revolutionary Self-Healing Locator System for Playwright**
> Transform fragile selectors into intelligent, self-healing locators with automatic fallback strategies.
> **Self-Healing Locator System for Playwright Test Automation**
> Transform fragile CSS selectors into intelligent, self-healing locators with automatic fallback strategies for robust web testing.

[![npm version](https://img.shields.io/npm/v/locatorpro-playwright.svg)](https://www.npmjs.com/package/locatorpro-playwright)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Tests](https://github.com/debasisj/locatorpro-playwright/actions/workflows/ci.yml/badge.svg)](https://github.com/debasisj/locatorpro-playwright/actions/workflows/ci.yml)

**Keywords**: `playwright locators`, `test automation`, `self-healing tests`, `web testing`, `css selectors`, `xpath locators`, `test automation framework`, `playwright testing`, `robust locators`, `automated testing tools`

## 🚀 **Why LocatorPro?**

**Playwright already has excellent built-in locators** like `getByRole()`, `getByText()`, and `getByTestId()` that are robust and reliable. These should be your first choice for most scenarios.
## 🚀 **Why Choose LocatorPro for Playwright Test Automation?**

**However, you need LocatorPro when:**
- Working with **legacy applications** where you can't control the markup
- Dealing with **dynamically generated IDs** or **changing class names**
- Elements lack proper **test IDs** or **semantic roles**
- You need to find elements by **contextual relationships** (e.g., "Add to Cart" button for a specific product)
- **Migrating existing tests** with fragile selectors without rewriting everything
- Building tests for **third-party applications** where you can't modify the HTML
**Microsoft Playwright already has excellent built-in locators** like `getByRole()`, `getByText()`, and `getByTestId()` that are robust and reliable for modern web testing. These should be your first choice for most test automation scenarios.

LocatorPro **enhances** Playwright's locators with intelligent fallbacks and contextual awareness, making your tests resilient in scenarios where standard locators fall short.
**However, you need LocatorPro when building resilient test automation for:**
- **Legacy web applications** where you can't control the HTML markup
- **Dynamic CSS selectors** and **changing element IDs** that break test automation
- Web elements lacking proper **test IDs**, **ARIA roles**, or **semantic markup**
- **Contextual element selection** in complex UI components (e.g., "Add to Cart" button for specific products)
- **Migrating existing test suites** with fragile CSS selectors without complete rewrites
- **Third-party web applications** and embedded widgets where HTML modification isn't possible
- **Cross-browser testing** scenarios requiring more resilient element location strategies

**Traditional Playwright tests break when:**
- IDs change: `#submit-btn-123` → `#submit-btn-456` ❌
- Classes are renamed: `.old-button` → `.new-button` ❌
LocatorPro **enhances** Playwright's locator capabilities with intelligent fallbacks and contextual awareness, making your test automation resilient in scenarios where standard web testing approaches fall short.

**Traditional Playwright test automation breaks when:**
- Element IDs change: `#submit-btn-123` → `#submit-btn-456` ❌
- CSS classes are renamed: `.old-button` → `.new-button` ❌
- DOM structure changes: Elements move or get nested differently ❌
- Dynamic content updates modify element attributes ❌

**LocatorPro creates self-healing tests that:**
- ✅ **Automatically find alternatives** when selectors break
- ✅ **Use intelligent fallback strategies** with priority-based selection
- ✅ **Analyze DOM patterns** to understand your intent
- ✅ **Work across different page layouts** without modification
**LocatorPro creates self-healing test automation that:**
- ✅ **Automatically find alternative selectors** when CSS selectors break
- ✅ **Use intelligent fallback strategies** with priority-based element selection
- ✅ **Analyze DOM patterns** to understand test automation intent
- ✅ **Work across different responsive layouts** without test modification
- ✅ **Improve test automation reliability** and reduce maintenance overhead

---

## 🎯 **Core Features**
## 🎯 **Core Test Automation Features**

### 🔍 **Smart Element Discovery**
- **Intelligent DOM scanning** with comprehensive element analysis
- **Text-based location** with fuzzy matching and variations
- **Related element selection** - find buttons by nearby text
- **Attribute-aware strategies** covering all HTML attributes
### 🔍 **Smart Element Discovery for Web Testing**
- **Intelligent DOM scanning** with comprehensive HTML element analysis
- **Text-based element location** with fuzzy matching and text variations
- **Related element selection** - find interactive elements by nearby text content
- **HTML attribute-aware strategies** covering all standard and custom attributes

### 🛡️ **Self-Healing Capabilities**
- **Multiple fallback strategies** with reliability scoring
- **Automatic enhancement** of existing fragile locators
- **Pattern recognition** for broken selectors
- **Cross-layout compatibility** for responsive designs
### 🛡️ **Self-Healing Test Automation Capabilities**
- **Multiple CSS selector fallbacks** with reliability scoring algorithms
- **Automatic enhancement** of existing fragile test locators
- **Pattern recognition** for broken CSS selectors and XPath expressions
- **Cross-layout compatibility** for responsive web design testing

### ⚡ **Developer Experience**
- **Drop-in replacement** for existing Playwright locators
- **One-line solutions** for complex scenarios
- **Comprehensive debugging** with detailed strategy logging
- **Zero configuration** - works out of the box
### ⚡ **Developer Experience for Test Automation**
- **Drop-in replacement** for existing Playwright locator methods
- **One-line solutions** for complex web testing scenarios
- **Comprehensive debugging** with detailed locator strategy logging
- **Zero configuration** - works out of the box with any Playwright test setup

---

## 📦 **Installation**
## 📦 **Installation for Playwright Test Automation**

```bash
# Install LocatorPro for your Playwright test automation project
npm install locatorpro-playwright
# or
# or using yarn for test automation setup
yarn add locatorpro-playwright
```
yarn add locatorpro-playwright
```

---

## 🚀 **Quick Start**
## 🚀 **Quick Start Guide for Playwright Testing**

```typescript
import { test, expect } from '@playwright/test';
import { SmartLocator } from 'locatorpro-playwright';

test('Smart locator example', async ({ page }) => {
test('Smart locator test automation example', async ({ page }) => {
const smartLocator = new SmartLocator(page);

await page.goto('https://example.com');

// 🎯 Find button by text with automatic fallbacks
// 🎯 Find button by text with automatic CSS selector fallbacks
const loginButton = await smartLocator.findByVisibleText('Login');
await loginButton.click();

// 🎯 Smart related element selection (revolutionary!)
// 🎯 Smart contextual element selection (great for test automation!)
const addToCartBtn = await smartLocator.findByRelatedText(
'Add to Cart', // Target element
'iPhone 15 Pro' // Related text in same container
'Add to Cart', // Target interactive element
'iPhone 15 Pro' // Related text content in same container
);
await addToCartBtn.click();
});
```

---

## 🎯 **Core Methods**
## 🎯 **Core Methods for Test Automation**

### 🔍 **Smart Discovery Methods**
### 🔍 **Smart Discovery Methods for Web Testing**

#### `findByVisibleText(text, options?)`
Find elements by visible text with intelligent fallback strategies.
#### `findByVisibleText(text, options?)` - Enhanced Text-Based Element Location
Find web elements by visible text with intelligent CSS selector fallbacks for robust test automation.

```typescript
// Basic usage
// Basic text-based element location for test automation
const button = await smartLocator.findByVisibleText('Submit');

// With fallbacks and options
// Advanced text search with fallbacks for resilient testing
const element = await smartLocator.findByVisibleText('Submit', {
fallbacks: ['Save', 'Send', 'Continue'],
elementTypes: ['button', 'input', 'a'],
maxResults: 5
});
```

#### `findByRelatedText(targetText, relatedText, options?)` 🌟
**Revolutionary feature** - Find elements by their relationship to nearby text.
#### `findByRelatedText(targetText, relatedText, options?)` 🌟 - Contextual Element Selection

**Great feature for test automation** - Find web elements by their relationship to nearby text content, perfect for complex UI testing.

```typescript
// Find "Add to Cart" button for specific product
// Find "Add to Cart" button for specific product in e-commerce testing
const addBtn = await smartLocator.findByRelatedText(
'Add to Cart', // What you want to click
'Product Name' // Text that identifies the context
'Add to Cart', // Interactive element to click
'Product Name' // Context text for element identification
);

// Real-world examples
// Real-world test automation examples
const sizeS = await smartLocator.findByRelatedText('S', 'Argus All-Weather Tank');
const chooseBtn = await smartLocator.findByRelatedText('Choose This Flight', 'Aer Lingus');
const repoLink = await smartLocator.findByRelatedText('my-repo', 'Smart locator library');
```

#### `findByText(text)` | `findByRole(role, options?)` | `findByTestId(testId)` | `findBySelector(selector)`
Additional core finder methods with enhanced capabilities.
#### Enhanced Element Location Methods for Test Automation
Additional core finder methods with enhanced capabilities for robust web testing.

```typescript
// Enhanced text search with fuzzy matching
// Enhanced text search with fuzzy matching for test automation
const element = await smartLocator.findByText('Login');

// Role-based with name filtering
// ARIA role-based selection with name filtering for accessibility testing
const nav = await smartLocator.findByRole('navigation');
const button = await smartLocator.findByRole('button', { name: 'Submit' });

// Tries data-testid, data-test, data-qa, and more
// Comprehensive test ID detection (data-testid, data-test, data-qa, and more)
const field = await smartLocator.findByTestId('submit-button');

// Enhanced CSS selector with fallbacks
// Enhanced CSS selector with automatic fallbacks for test automation
const element = await smartLocator.findBySelector('.my-component');
```

### 🛡️ **Self-Healing Enhancement**
### 🛡️ **Self-Healing Enhancement for Test Automation**

#### `enhanceLocator(locator)`
Transform any existing Playwright locator into a self-healing smart locator.
#### `enhanceLocator(locator)` - Transform Fragile Selectors into Self-Healing Test Automation
Transform any existing Playwright locator into a self-healing smart locator for robust test automation.

```typescript
// Transform fragile locators
// Transform fragile CSS selectors into self-healing test automation
const fragileLocator = page.locator('#submit-btn-1234');
const enhanced = await smartLocator.enhanceLocator(fragileLocator);

// Even if #submit-btn-1234 doesn't exist, enhanced locator will:
// 1. Analyze the pattern (ID contains "submit")
// 2. Generate smart alternatives:
// 1. Analyze the CSS selector pattern (ID contains "submit")
// 2. Generate smart alternatives for test automation:
// - page.getByText('Submit')
// - page.locator('button[type="submit"]')
// - page.locator('[id^="submit-btn"]')
await enhanced.click(); // ✅ Works even when original ID is broken!
await enhanced.click(); // ✅ Works even when original CSS selector breaks!
```

#### `autoEnhance(locator, options?)`
Try original locator first, enhance automatically if it fails.
#### Advanced Enhancement Methods for Test Automation
Specialized enhancement methods for different test automation scenarios.

```typescript
// Try original locator first, enhance automatically if it fails
const smartButton = await smartLocator.autoEnhance(
page.locator('#might-not-exist')
);
await smartButton.click(); // Always works!
```
await smartButton.click(); // Always works in test automation!

#### `enhanceWorkingLocator(locator)` | `enhanceBrokenLocator(locator)`
Specialized enhancement methods for different scenarios.

```typescript
// Enhance already working locators with additional strategies
// Enhance working locators with additional strategies for future-proofing
const enhanced = await smartLocator.enhanceWorkingLocator(page.locator('#working-btn'));

// Specifically handle broken selectors with pattern analysis
// Handle broken CSS selectors with intelligent pattern analysis
const fixed = await smartLocator.enhanceBrokenLocator(page.locator('#broken-123'));
```

### ⚡ **Smart Actions (Drop-in Replacements)**
### ⚡ **Smart Actions for Legacy Test Automation Migration**

#### `smartClick(locator)` | `smartFill(locator, text)`
Click and fill with automatic enhancement - perfect for legacy test migration.
#### Drop-in Replacements for Existing Test Automation Code
Perfect for migrating legacy test automation suites without complete rewrites.

```typescript
// Instead of: await page.locator('#fragile-btn').click();
await smartLocator.smartClick(page.locator('#fragile-btn'));

// Instead of: await page.locator('#fragile-input').fill('text');
await smartLocator.smartFill(page.locator('#fragile-input'), 'Hello World');
```

#### `smartExpected(locator)`
Expectations with automatic enhancement.

```typescript
// Instead of: await expect(page.locator('#fragile')).toBeVisible();
await smartLocator.smartExpected(page.locator('#fragile')).toBeVisible();
```

---

## 🎯 **Real-World Examples**
## 🎯 **Real-World Test Automation Examples**

### 🛍️ **E-commerce: Product Selection**
### 🛍️ **E-commerce Test Automation: Product Selection**
```typescript
// Traditional approach (brittle)
// Traditional approach (brittle for test automation)
const productRow = page.locator('tr:has-text("iPhone 15 Pro")');
const addButton = productRow.locator('button:has-text("Add to Cart")');
await addButton.click();

// ✨ Smart approach (one line, layout-independent)
// ✨ Smart approach (robust test automation, layout-independent)
const addBtn = await smartLocator.findByRelatedText('Add to Cart', 'iPhone 15 Pro');
await addBtn.click();
```

### ✈️ **Travel: Flight Selection**
### ✈️ **Travel Website Test Automation: Flight Selection**
```typescript
// Traditional approach (complex XPath)
// Traditional approach (complex XPath for test automation)
const flightRow = page.locator('//tr[contains(., "Aer Lingus")]');
const chooseBtn = flightRow.locator('.//input[@value="Choose This Flight"]');
await chooseBtn.click();

// ✨ Smart approach (simple and reliable)
// ✨ Smart approach (simple and reliable test automation)
const chooseBtn = await smartLocator.findByRelatedText('Choose This Flight', 'Aer Lingus');
await chooseBtn.click();
```
Expand Down Expand Up @@ -281,7 +279,7 @@ LocatorPro automatically generates multiple strategies with priority-based fallb
11. **XPath strategies** (0.8-0.85) - Complex path-based selectors
12. **CSS class combinations** (0.6-0.8) - `.btn.primary`

### **Intelligent Pattern Recognition**
### ** Pattern Recognition**
```typescript
// Original selector: #submit-btn-1234 (broken)
// Smart alternatives generated:
Expand Down Expand Up @@ -438,7 +436,7 @@ npm run test
## � **Changelog**

### **v1.0.1** - Latest Release 🎉
- ✅ **Revolutionary `findByRelatedText()`** - Container-based element selection
- ✅ **Great `findByRelatedText()`** - Container-based element selection
- ✅ **Enhanced `enhanceLocator()`** - Handles broken selectors with pattern analysis
- ✅ **Smart Actions API** - `smartClick()`, `smartFill()`, `smartExpected()`
- ✅ **Comprehensive attribute coverage** - All HTML attributes supported
Expand Down Expand Up @@ -475,6 +473,20 @@ import { SmartLocator } from 'locatorpro-playwright';

---

**Built with ❤️ for the Playwright community**
## 🎯 **SEO-Optimized Features for Test Automation**

**LocatorPro for Playwright** is the ultimate solution for:
- **Robust test automation** with self-healing CSS selectors
- **Cross-browser testing** with intelligent element location
- **Legacy application testing** without modifying source code
- **E-commerce test automation** with complex product interactions
- **Responsive web design testing** across multiple device viewports
- **Accessibility testing** with ARIA role and semantic element support
- **Performance testing** with efficient element discovery algorithms
- **CI/CD integration** for automated test pipeline reliability

---

**Built with ❤️ for the Playwright and test automation community**

*Making web automation more reliable, one locator at a time.*
*Making web test automation more reliable, one intelligent locator at a time.*
Loading
Loading