fix: add data-testid attributes for contact page elements

This commit is contained in:
张翔
2026-03-05 21:49:12 +08:00
parent 2f6f6a6aca
commit 318081a62a
2 changed files with 34 additions and 31 deletions
+24 -21
View File
@@ -16,13 +16,17 @@ export class ContactPage extends BasePage {
readonly contactInfoCard: Locator;
readonly workHoursCard: Locator;
readonly emailInfo: Locator;
readonly phoneInfo: Locator;
readonly addressInfo: Locator;
readonly emailLink: Locator;
readonly phoneLink: Locator;
readonly addressText: Locator;
readonly pageBadge: Locator;
readonly pageDescription: Locator;
readonly successMessage: Locator;
readonly addressInfo: Locator;
readonly phoneInfo: Locator;
readonly emailInfo: Locator;
readonly nameError: Locator;
readonly emailError: Locator;
readonly phoneError: Locator;
@@ -32,7 +36,7 @@ export class ContactPage extends BasePage {
super(page);
this.url = '/contact';
this.pageHeader = page.locator('h1:has-text("与我们取得联系")');
this.pageHeader = page.locator('h1');
this.contactForm = page.locator('form');
this.nameInput = page.locator('input[name="name"]');
this.phoneInput = page.locator('input[name="phone"]');
@@ -41,15 +45,19 @@ export class ContactPage extends BasePage {
this.messageInput = page.locator('textarea[name="message"]');
this.submitButton = page.locator('button[type="submit"]');
this.contactInfoCard = page.locator('[data-slot="card"]').filter({ hasText: '联系方式' }).first();
this.workHoursCard = page.locator('[data-slot="card"]').filter({ hasText: '工作时间' }).first();
this.contactInfoCard = page.locator('[data-testid="contact-info"]');
this.workHoursCard = page.locator('[data-testid="work-hours-card"]');
this.emailInfo = page.locator('[data-testid="email-info"]');
this.phoneInfo = page.locator('[data-testid="phone-info"]');
this.addressInfo = page.locator('[data-testid="address-info"]');
this.emailLink = page.locator('[data-testid="email-link"]');
this.phoneLink = page.locator('[data-testid="phone-link"]');
this.addressText = page.locator('[data-testid="address-text"]');
this.pageBadge = page.locator('[data-testid="page-badge"]');
this.pageDescription = page.locator('[data-testid="page-description"]');
this.successMessage = page.locator('text=消息已发送');
this.addressInfo = this.contactInfoCard.locator('text=公司地址');
this.phoneInfo = this.contactInfoCard.locator('text=联系电话');
this.emailInfo = this.contactInfoCard.locator('text=电子邮箱');
this.nameError = page.locator('input[name="name"] + .error-message, input[name="name"] ~ .text-destructive').first();
this.emailError = page.locator('input[name="email"] + .error-message, input[name="email"] ~ .text-destructive').first();
this.phoneError = page.locator('input[name="phone"] + .error-message, input[name="phone"] ~ .text-destructive').first();
@@ -202,18 +210,15 @@ export class ContactPage extends BasePage {
}
async getAddress(): Promise<string> {
const addressElement = this.contactInfoCard.locator('div').nth(3).locator('div').nth(1);
return await addressElement.textContent() || '';
return await this.addressText.textContent() || '';
}
async getPhone(): Promise<string> {
const phoneElement = this.contactInfoCard.locator('div').nth(6).locator('div').nth(1);
return await phoneElement.textContent() || '';
return await this.phoneLink.textContent() || '';
}
async getEmail(): Promise<string> {
const emailElement = this.contactInfoCard.locator('div').nth(9).locator('div').nth(1);
return await emailElement.textContent() || '';
return await this.emailLink.textContent() || '';
}
async getPageTitle(): Promise<string> {
@@ -221,13 +226,11 @@ export class ContactPage extends BasePage {
}
async getPageDescription(): Promise<string> {
const description = this.pageHeader.locator('..').locator('p');
return await description.textContent() || '';
return await this.pageDescription.textContent() || '';
}
async getBadgeText(): Promise<string> {
const badge = this.page.locator('[data-slot="badge"]').first();
return await badge.textContent() || '';
return await this.pageBadge.textContent() || '';
}
async isRequiredFieldVisible(fieldName: string): Promise<boolean> {