由於 Playwright 是一套基於 Node.js 的 E2E 自動化測試工具,其優異的 Locators 與 Actions 的 APIs 非常的好用,可惜官方並沒有提供可執行在原生瀏覽器的 JS 函式庫版本。因此本專案特別實作了一遍幾個重要的 APIs,讓開發者可以在原生瀏覽器中使用 Playwright 的 Locators API。此舉將可大幅改善原生瀏覽器的自動化任務。
page.getByTestId() 來根據 data-testid
屬性定位元素(其他屬性可以配置)。
直接從網頁中加入
<script src="https://doggy8088.github.io/playwright-js/src/playwright.js"></script>
從網頁中動態加入
var pwjs = document.createElement('script');
pwjs.src = 'https://doggy8088.github.io/playwright-js/src/playwright.js';
pwjs.async = true;
pwjs.onload = () => {
console.log('Script loaded successfully!');
};
pwjs.onerror = () => {
console.error(`Failed to load script: ${pwjs.src}`);
};
document.head.appendChild(pwjs);
使用範例請見 tests/index.html