import { Page, Locator } from '@playwright/test' export class FormHelper { private page: Page constructor(page: Page) { this.page = page } async fillInput(selector: string, value: string, options?: { clear?: boolean; delay?: number }) { const input = this.page.locator(selector) if (options?.clear) { await input.clear() } await input.fill(value, { delay: options?.delay }) } async selectOption(selector: string, value: string) { const select = this.page.locator(selector) await select.selectOption(value) } async checkCheckbox(selector: string) { const checkbox = this.page.locator(selector) await checkbox.check() } async uncheckCheckbox(selector: string) { const checkbox = this.page.locator(selector) await checkbox.uncheck() } async selectRadio(selector: string, value: string) { const radio = this.page.locator(`${selector}[value="${value}"]`) await radio.check() } async uploadFile(selector: string, filePath: string) { const input = this.page.locator(selector) await input.setInputFiles(filePath) } async submitForm(selector: string) { const form = this.page.locator(selector) await form.evaluate((form: HTMLFormElement) => form.submit()) } async resetForm(selector: string) { const form = this.page.locator(selector) await form.evaluate((form: HTMLFormElement) => form.reset()) } async getFieldValue(selector: string): Promise { const field = this.page.locator(selector) return await field.inputValue() } async isFieldValid(selector: string): Promise { const field = this.page.locator(selector) const isValid = await field.evaluate((el: HTMLInputElement) => el.checkValidity()) return isValid } async getValidationMessage(selector: string): Promise { const field = this.page.locator(selector) return await field.evaluate((el: HTMLInputElement) => el.validationMessage) } async waitForFormToBeReady(selector: string, timeout: number = 5000) { await this.page.waitForSelector(selector, { state: 'visible', timeout }) await this.page.waitForLoadState('networkidle', { timeout }) } async fillForm(fields: Array<{ selector: string; value: string; type?: 'input' | 'select' | 'checkbox' }>) { for (const field of fields) { switch (field.type) { case 'select': await this.selectOption(field.selector, field.value) break case 'checkbox': if (field.value === 'true' || field.value === 'checked') { await this.checkCheckbox(field.selector) } else { await this.uncheckCheckbox(field.selector) } break default: await this.fillInput(field.selector, field.value) } } } } export default FormHelper