claude-playwright
Version:
Seamless integration between Claude Code and Playwright MCP for efficient browser automation and testing
183 lines • 5.08 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasePage = void 0;
const test_1 = require("@playwright/test");
/**
* Base Page Object Model class
* All page objects should extend this class for consistent functionality
*/
class BasePage {
constructor(page) {
this.page = page;
this.baseUrl = process.env.BASE_URL || 'http://localhost:3000';
}
/**
* Navigate to a specific path
*/
async navigateTo(path = '') {
const url = `${this.baseUrl}${path}`;
await this.page.goto(url);
await this.page.waitForLoadState('networkidle');
}
/**
* Navigate to the page
*/
async goto() {
await this.navigateTo('/');
}
/**
* Wait for page to be loaded
*/
async waitForLoad() {
await this.page.waitForLoadState('networkidle');
}
/**
* Take screenshot with custom name
*/
async takeScreenshot(name) {
await this.page.screenshot({
path: `screenshots/${name}-${Date.now()}.png`,
fullPage: true
});
}
/**
* Wait for element and click
*/
async clickAndWait(selector) {
await this.page.click(selector);
await this.page.waitForLoadState('networkidle');
}
/**
* Fill multiple form fields
*/
async fillForm(fields) {
for (const [selector, value] of Object.entries(fields)) {
await this.page.fill(selector, value);
}
}
/**
* Wait for element to be visible
*/
async waitForElement(selector, timeout = 10000) {
const element = this.page.locator(selector);
await element.waitFor({ state: 'visible', timeout });
return element;
}
/**
* Check if element exists and is visible
*/
async isElementVisible(selector) {
try {
const element = this.page.locator(selector);
await element.waitFor({ state: 'visible', timeout: 5000 });
return true;
}
catch {
return false;
}
}
/**
* Get text content from element
*/
async getElementText(selector) {
const element = await this.waitForElement(selector);
return await element.textContent() || '';
}
/**
* Expect page to have specific title
*/
async expectPageTitle(title) {
await (0, test_1.expect)(this.page).toHaveTitle(title);
}
/**
* Expect page to have specific URL
*/
async expectURL(url) {
await (0, test_1.expect)(this.page).toHaveURL(url);
}
/**
* Expect element to be visible
*/
async expectElementVisible(selector) {
await (0, test_1.expect)(this.page.locator(selector)).toBeVisible();
}
/**
* Expect element to contain text
*/
async expectElementText(selector, text) {
await (0, test_1.expect)(this.page.locator(selector)).toContainText(text);
}
/**
* Wait for network to be idle
*/
async waitForNetworkIdle() {
await this.page.waitForLoadState('networkidle');
}
/**
* Reload current page
*/
async reload() {
await this.page.reload();
await this.waitForNetworkIdle();
}
/**
* Get current page URL
*/
getCurrentUrl() {
return this.page.url();
}
/**
* Check if page is loaded (abstract method for subclasses to override)
*/
async isLoaded() {
// Default implementation - subclasses should override
return this.page.url().includes(this.baseUrl);
}
/**
* Safe click with retry logic
*/
async safeClick(locator, retries = 3) {
const element = typeof locator === 'string' ? this.page.locator(locator) : locator;
for (let i = 0; i < retries; i++) {
try {
await element.click({ timeout: 5000 });
return;
}
catch (error) {
if (i === retries - 1)
throw error;
await this.page.waitForTimeout(1000);
}
}
}
/**
* Safe fill with clearing
*/
async safeFill(locator, value) {
const element = typeof locator === 'string' ? this.page.locator(locator) : locator;
await element.clear();
await element.fill(value);
}
/**
* Get page title
*/
async getTitle() {
return await this.page.title();
}
/**
* Wait for element to be hidden
*/
async waitForHidden(locator, timeout) {
const element = typeof locator === 'string' ? this.page.locator(locator) : locator;
await element.waitFor({ state: 'hidden', timeout });
}
/**
* Wait for element to be visible
*/
async waitForVisible(locator, timeout) {
const element = typeof locator === 'string' ? this.page.locator(locator) : locator;
await element.waitFor({ state: 'visible', timeout });
}
}
exports.BasePage = BasePage;
//# sourceMappingURL=BasePage.js.map
;