在Node.js環(huán)境下使用Playwright添加自定義實(shí)現(xiàn)方式
方法1:直接創(chuàng)建實(shí)用工具函數(shù)(推薦)
原理:創(chuàng)建獨(dú)立的工具函數(shù),通過(guò)參數(shù)傳入 page 對(duì)象操作頁(yè)面
// utils/custom-actions.js
module.exports = {
async login(page, username, password) {
await page.goto('https://example.com/login');
await page.fill('#username', username);
await page.fill('#password', password);
await page.click('#submit');
await page.waitForURL(/dashboard/);
},
async captureScreenshot(page, fileName) {
await page.screenshot({ path: `screenshots/${fileName}.png` });
}
};
使用方式:
const { test } = require('@playwright/test');
const { login, captureScreenshot } = require('./utils/custom-actions');
test('Authentication test', async ({ page }) => {
// 使用自定義方法
await login(page, 'test@email.com', 'p@ssw0rd');
await captureScreenshot(page, 'dashboard-view');
});
方法2:封裝自定義 Page 類(通過(guò)組合模式)
原理:創(chuàng)建代理類包裹原始 page 對(duì)象,添加額外方法
// lib/custom-page.js
module.exports = class CustomPage {
constructor(page) {
this.page = page;
}
async typeSlowly(selector, text, delay = 100) {
for (const char of text) {
await this.page.type(selector, char);
await this.page.waitForTimeout(delay);
}
}
async dragAndDrop(sourceSel, targetSel) {
const source = await this.page.$(sourceSel);
const target = await this.page.$(targetSel);
await this.page.dragAndDrop(source, target);
}
};
使用方式:
const { test } = require('@playwright/test');
const CustomPage = require('./lib/custom-page');
test('Form test', async ({ page }) => {
const customPage = new CustomPage(page);
await customPage.typeSlowly('#comment', 'Hello World', 150);
await customPage.dragAndDrop('#item1', '#drop-zone');
});
方法3:通過(guò) Test Fixtures 擴(kuò)展(Playwright 官方推薦)
原理:利用 Playwright 的 fixture 系統(tǒng)擴(kuò)展測(cè)試上下文
// fixtures.js
const base = require('@playwright/test');
const CustomPage = require('./lib/custom-page');
exports.test = base.test.extend({
customPage: async ({ page }, use) => {
const customPage = new CustomPage(page);
await use(customPage);
}
});
使用方式:
// tests.spec.js
const { test } = require('./fixtures');
test('Advanced test', async ({ customPage }) => {
await customPage.typeSlowly('#bio', 'Automation Engineer');
// 原始 page 仍然可用
await customPage.page.keyboard.press('Enter');
});
主要優(yōu)勢(shì)
- 無(wú)侵入性:不修改 Playwright 源碼或原型
- 按需組合:可以混合使用原生 API 和自定義方法
- 維護(hù)性強(qiáng):自定義邏輯集中管理
- TypeScript 支持:通過(guò)聲明文件添加類型提示(如使用 TS)
最佳實(shí)踐建議
- 將常用操作封裝成獨(dú)立功能(如登錄、數(shù)據(jù)生成)
- 為復(fù)雜交互創(chuàng)建專用工具類(如表格操作、拖放序列)
- 在 fixtures 中初始化常用測(cè)試狀態(tài)
- 對(duì)自定義方法添加錯(cuò)誤處理和日志
- 使用
page.waitForFunction()實(shí)現(xiàn)高級(jí)等待邏輯
// 高級(jí):等待元素包含特定文本
async waitForText(selector, text, timeout = 5000) {
await this.page.waitForFunction(
({ sel, txt }) => {
const el = document.querySelector(sel);
return el?.innerText?.includes(txt);
},
{ selector, text },
{ timeout }
);
}
這些方法既保持了 Playwright API 的原始完整性,又能有效擴(kuò)展測(cè)試能力,特別適合復(fù)雜應(yīng)用的測(cè)試場(chǎng)景。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于express與koa的使用對(duì)比詳解
很多人都在問(wèn)到底該用Koa還是express,所以下面這篇文章就來(lái)給大家再次的對(duì)比了關(guān)于express與koa的相關(guān)資料,通過(guò)對(duì)比大家可以更好的進(jìn)行選擇,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
nodejs實(shí)現(xiàn)套接字服務(wù)功能詳解
這篇文章主要介紹了nodejs實(shí)現(xiàn)套接字服務(wù)功能,簡(jiǎn)單描述了套接字的概念、功能,并結(jié)合實(shí)例形式分析了nodejs使用socket對(duì)象創(chuàng)建及使用套接字進(jìn)行數(shù)據(jù)傳輸相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06
如何利用node實(shí)現(xiàn)發(fā)送QQ郵箱驗(yàn)證碼
我們?cè)陂_發(fā)網(wǎng)站時(shí),發(fā)送驗(yàn)證碼的功能是必定會(huì)遇到的,下面這篇文章主要給大家介紹了關(guān)于如何利用node實(shí)現(xiàn)發(fā)送QQ郵箱驗(yàn)證碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Nodejs做文本數(shù)據(jù)處理實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Nodejs做文本數(shù)據(jù)處理實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
node.js中的url.parse方法使用說(shuō)明
這篇文章主要介紹了node.js中的url.parse方法使用說(shuō)明,本文介紹了url.parse的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
node-gyp安裝vuetify編譯失敗gyp?ERR的問(wèn)題及解決
這篇文章主要介紹了node-gyp安裝vuetify編譯失敗gyp?ERR的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Node.js 使用request模塊下載文件的實(shí)例
今天小編就為大家分享一篇Node.js 使用request模塊下載文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

