End-to-End Testing with Playwright (an Intro)

npm i -D @playwright/test# install supported browsersnpx playwright install
const { test, expect } = require('@playwright/test');const URL = 'http://localhost:5500/index.html';test('only valid email addresses can be submitted', async ({ page }) => {
// 前往該頁面
await page.goto(URL);
// 尋找 submit 按鈕 (Submit使用文字搜尋比較好,
// 因為可能因為樣式更改標籤或 class)
const submitBtn = page.locator('text=Submit');
// 輸入 email (可以特別寫一個專給 test 用的屬性)
await page.fill('[data-testid="email-input"]', 'asd@asd.com');
// 判斷 submit 按鈕是否可以點擊
await expect(submitBtn).toBeEnabled();
});

實作

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jason

Jason

Nothing is true, everything is permitted.