From bac0dbfdbe810a108266d937afcf942735e91a8a Mon Sep 17 00:00:00 2001 From: LingandRX Date: Sun, 16 Mar 2025 19:10:03 +0800 Subject: [PATCH] =?UTF-8?q?ci(test):=20=E6=9B=B4=E6=96=B0=20Playwright=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=B9=B6=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 Playwright 版本从 v1.43.0 升级到 v1.51.0 - 优化了 baseFixture 中的登录流程 - 改进了 customerPage 中的顾客创建逻辑 - 调整了 boss_cashier 和 staff_goal 测试中的操作方式 --- .gitlab-ci.yml | 4 +- tests/fixtures/baseFixture.ts | 4 +- tests/pages/customer/customerPage.ts | 61 +++++++++++++++++++++------- tests/touch/boss_cashier.spec.ts | 26 +++++------- tests/touch/staff_goal.spec.ts | 8 ++-- 5 files changed, 66 insertions(+), 37 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 02637bf..2c191ff 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,6 @@ playwright_tests: stage: test - image: mcr.microsoft.com/playwright:v1.43.0-jammy + image: mcr.microsoft.com/playwright:v1.51.0-noble cache: key: $CI_COMMIT_REF_SLUG paths: @@ -9,7 +9,7 @@ playwright_tests: - npm config set registry https://registry.npmmirror.com script: - npm install - - npx playwright test + - npx playwright test /touch/boss_cashier.spec.ts --grep "挂单" --project '慧来客touch(管理员身份) - Desktop Chrome' artifacts: paths: - test-results/ diff --git a/tests/fixtures/baseFixture.ts b/tests/fixtures/baseFixture.ts index 4203491..b772759 100644 --- a/tests/fixtures/baseFixture.ts +++ b/tests/fixtures/baseFixture.ts @@ -24,7 +24,9 @@ export const test = base.extend({ await page.getByRole('button', { name: /开\s单/ }).click(); // await page.getByRole('button', { name: /开\s单/ }).waitFor(); await expect(page.getByRole('button', { name: '创建会员' })).toBeEnabled(); - + await page.locator('.top > .anticon').first().click(); + await expect(page.getByRole('button', { name: '创建会员' })).not.toBeVisible(); + await expect(page.getByRole('button', { name: /开\s单/ })).toBeInViewport(); await use(page); }, diff --git a/tests/pages/customer/customerPage.ts b/tests/pages/customer/customerPage.ts index f6514b9..e561fd1 100644 --- a/tests/pages/customer/customerPage.ts +++ b/tests/pages/customer/customerPage.ts @@ -339,22 +339,55 @@ export class CustomerPage { * @param {string} phone 手机号 */ private readonly confirmCreation = async (phone: string) => { - const [response] = await Promise.all([ - this.page.waitForResponse(async res => res.url().includes('/invalid_check') && (await res.json()).code === 'SUCCESS'), - this.page.getByRole('button', { name: '确认创建' }).click(), - ]); - const responseBody = await response.json(); - - const phoneStatus = responseBody?.content?.status; - if (phoneStatus === undefined || phoneStatus === null) { - // 创建顾客成功 - return; + try { + // 等待响应并点击按钮 + const [response] = await Promise.all([ + this.page.waitForResponse(async (res) => { + const urlMatch = res.url().includes('/invalid_check'); + if (!urlMatch) return false; + try { + const json = await res.json(); + return json.code === 'SUCCESS'; + } catch (e) { + console.error('解析响应体失败:', e); + return false; + } + }), + this.page.getByRole('button', { name: '确认创建' }).click(), + ]); + + // 解析响应体 + let responseBody: any; + try { + responseBody = await response.json(); + } catch (e) { + throw new Error('无法解析服务器响应体,请检查网络连接或服务器状态'); + } + + // 检查响应体内容 + if (responseBody && typeof responseBody.content === 'object') { + const phoneStatus = responseBody.content?.status; + + if (phoneStatus != null) { + throw new Error(`手机号码 ${phone} 已被使用,无法创建新顾客`); + } + } else { + console.warn('响应体格式不符合预期,content 不是对象'); + } + } catch (error) { + // 捕获并记录异常 + console.error('检查手机号码时发生错误:', error); + throw error; // 重新抛出异常以便调用方处理 } - await this.page.getByText('系统查询到当前手机号被建档后转为无效客,是否要恢复无效客?').waitFor(); - await this.page.getByRole('button', { name: '重新建档' }).click(); - const popupWindow = this.page.locator('.ant-message'); - await expect(popupWindow).not.toContainText('该手机号码已经被使用'); + // if (phoneStatus === undefined || phoneStatus === null) { + // 创建顾客成功 + // return; + // } + // await this.page.getByText('系统查询到当前手机号被建档后转为无效客,是否要恢复无效客?').waitFor(); + // await this.page.getByRole('button', { name: '重新建档' }).click(); + // const popupWindow = this.page.locator('.ant-message'); + // await expect(popupWindow).not.toContainText('该手机号码已经被使用'); // await popupWindow.waitFor(); // const popupContent = (await popupWindow.innerText()).trim(); diff --git a/tests/touch/boss_cashier.spec.ts b/tests/touch/boss_cashier.spec.ts index 31e7d7f..0900d21 100644 --- a/tests/touch/boss_cashier.spec.ts +++ b/tests/touch/boss_cashier.spec.ts @@ -276,27 +276,21 @@ test.describe('挂单', () => { }); test('已过期单据不能取单,只能删除', async ({ page }) => { - const $slidingMenu = page.locator('div.deleteList .m_sliding_menu'); await page.getByText('已过期 / 删除单据').click(); await page.getByText('警告:已过期服务无法取单结算,请至收银台处理').waitFor(); - await $slidingMenu.locator('div.item_box').first().waitFor(); - await expect( - $slidingMenu - .locator('div.item_box') - .first() - .getByRole('button', { name: /^取\s单$/ }), - ).not.toBeVisible(); - const $delete = $slidingMenu - .locator('div.item_box') - .first() - .locator('.comment > div:nth-child(2) > .touchIcon'); - await $delete.click(); + const $firstOrder = page.locator('.deleteList .m_sliding_menu .item_box').first(); + + await expect($firstOrder.getByRole('button', { name: /^取\s单$/ })).not.toBeVisible(); + + await $firstOrder.locator('.comment > div:nth-child(2) > .touchIcon').click(); await page.getByPlaceholder('请输入1-100个字符备注内容').click(); await page.getByPlaceholder('请输入1-100个字符备注内容').fill('测试备注'); - await page.getByRole('button', { name: /^保\s存$/ }).click(); - await expect(page.locator('.ant-message').filter({ hasText: '删除成功' }).nth(2)).toBeVisible(); + await Promise.all([ + page.getByRole('button', { name: /^保\s存$/ }).click(), + expect(page.locator('.ant-message').filter({ hasText: '删除成功' })).toBeVisible(), + ]); }); }); @@ -306,7 +300,7 @@ test.describe('挂单', () => { const phone = customer.phone; const username = customer.username; - await page.pause(); + // await page.pause(); await homeNavigation.gotoModule('收银'); await page.getByRole('button', { name: /^开\s单$/ }).click(); diff --git a/tests/touch/staff_goal.spec.ts b/tests/touch/staff_goal.spec.ts index 3d7c814..0c2b1bb 100644 --- a/tests/touch/staff_goal.spec.ts +++ b/tests/touch/staff_goal.spec.ts @@ -48,7 +48,7 @@ test.describe('目标-目标设置', () => { .locator('.m-table-cell') .nth(1) .click(); - await numberInput.setValue(Number(goal)); + await numberInput.setValue('common', Number(goal)); await numberInput.confirmValue(); await page.getByRole('button', { name: '保 存' }).click(); @@ -101,7 +101,7 @@ test.describe('目标-目标设置', () => { .locator('.m-table-cell') .nth(1) .click(); - await numberInput.setValue(0); + await numberInput.setValue('common', 0); await numberInput.confirmValue(); await page.getByRole('button', { name: '保 存' }).click(); }); @@ -191,7 +191,7 @@ test.describe('目标-目标设置', () => { const code = page.locator('.popupComTableStyle .m-table__body .main-table-body_tr'); await code.nth(nowRowB).locator('.m-table-cell').nth(1).click(); - await numberInput.setValue(Number(goalB)); + await numberInput.setValue('common', Number(goalB)); await numberInput.confirmValue(); // 员工C内行数 @@ -217,7 +217,7 @@ test.describe('目标-目标设置', () => { ).not.toBeVisible(); await code.nth(nowRowC).locator('.m-table-cell').nth(1).click(); - await numberInput.setValue(Number(goalC)); + await numberInput.setValue('common', Number(goalC)); await numberInput.confirmValue(); await page.getByRole('button', { name: /保\s存/ }).click();