Mark Ku's Blog

解決問題

我目前任職流量不小的 B2C 電子商務公司,為避免不小心將結帳畫面改壞了,而沒被發現,而我著手研究 UI TEST,用於保護我們的結帳流程,之前就有用 playwright 來做 SEO 的預渲染,在評估了幾套 UI 測試後,發現 playwright 這幾年生態變得很完整及簡單,非常適合我們,其優點如下

  • 微軟推出的開源 UI-Test 框架
  • 整合 Jest
  • 支援 vs code 開發及除錯
  • 預設整合 Github
  • 提供完整的 Report
  • 支援 node js

我預期的運作流程

開發者推送程式碼至 GitHub 觸發 Playwright 測試並通知
開發者推送程式碼至 GitHub 觸發 Playwright 測試並通知

步驟

首先,我們先建立 playwright 測試專案 官方文件

npm init playwright@latest

選擇 Typescript 及 github action flow 整合

PowerShell 視窗顯示 Playwright 專案初始化設定
PowerShell 視窗顯示 Playwright 專案初始化設定

打開剛剛建立的測試專案,在資料夾裡其實己寫好基本的測試範例

VS Code 編輯器中 Playwright 的 Typescript 測試範例
VS Code 編輯器中 Playwright 的 Typescript 測試範例

請先在 vscode 安裝 jest 套件Playwright Test for VSCode 套件

並加入中斷點,並對綠色的啟動箭頭 >右鍵 > 對測試偵錯

VSCode右鍵選單中選取對測試偵錯
VSCode右鍵選單中選取對測試偵錯

此時就會跳出瀏覽器,並可以逐行除錯

VSCode偵錯Playwright測試時彈出的瀏覽器畫面
VSCode偵錯Playwright測試時彈出的瀏覽器畫面

看到這邊爽度就爽度超高,過去寫過 python selenium 都還要花很多時間配置系統環境。

接著,將你的專案推上 github,此時就會自動建置( 測試 ),點開工作記錄

GitHub Actions顯示Playwright測試工作記錄
GitHub Actions顯示Playwright測試工作記錄

隨後我們可以發現測試己成功完成建置,並發現主控台入口及報表的入口

GitHub Actions 測試成功,顯示主控台與報表入口
GitHub Actions 測試成功,顯示主控台與報表入口

點擊進入測試主控台,可以看 job 執行了些什麼

GitHub Actions Playwright 測試成功執行步驟列表
GitHub Actions Playwright 測試成功執行步驟列表

打開下載的報表

Playwright 測試報告顯示所有 6 個測試皆通過
Playwright 測試報告顯示所有 6 個測試皆通過

接著,因我們預期將測試的結果及報表推送到 Teams

Jenkins Teams通知顯示測試失敗與成功結果
Jenkins Teams通知顯示測試失敗與成功結果

請先至 github repo 的 Settings > Secrets > 建立 Actions secrets 參數。

GitHub設定頁面建立Actions密鑰MSTEAMS
GitHub設定頁面建立Actions密鑰MSTEAMS

修改 .github/workflows/playwright.yml 參考以下程式碼,加入通知

name: Playwright Tests
on:
  repository_dispatch:
    types: remote-trigger-ui-test
  push:
    branches: [ main, master ]
  pull_request:
    branches: [ main, master ]
jobs:
  test:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - name: Install dependencies
      run: npm ci
    - name: Install Playwright Browsers
      run: npx playwright install --with-deps
    - name: Run Playwright tests
      run: npx playwright test
    - uses: actions/upload-artifact@v3
      if: always()
      with:
        name: playwright-report
        path: playwright-report/
        retention-days: 30
  success_notify:
    needs: test
    name: notify
    runs-on: ubuntu-18.04    
    steps:  
      - name: 📣 Send teams notification
        uses: simbo/msteams-message-card-action@v1
        with:
          webhook: ${{ secrets.MSTEAMS_WEBHOOK }}
          title: 😊 UI - Test Success 
          message: <p>UI - Test Success<strong>\   /</strong></p>
          color: 007FFF
          buttons: |
            See Test Report! ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}      
  error_notify:
    needs: test
    name: error_notify
    if: ${{ always() && needs.test.result == 'failure' }}
    runs-on: ubuntu-18.04    
    steps:        
      - name: Send fail notification
        uses: simbo/msteams-message-card-action@v1        
        with:
          webhook: ${{ secrets.MSTEAMS_WEBHOOK }}
          title: 💩UI - Test Fail 
          message: <p>Ba Be Q ...<strong>= =...</strong></p>
          color: FF0000
          buttons: |
            See Test Report! ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

測試執行完的圖表

GitHub Actions 成功執行 Playwright 測試與通知流程
GitHub Actions 成功執行 Playwright 測試與通知流程

在欲應用程式,新增外部觸發 wrokflows,可參考此篇設定遠程觸發 git action,並將下面的程式碼參數換掉

:owner/:repo => markku636/first-ui-teset
:token => Github token 點我申請

on: push
jobs:
  test-curl-action:
    name: "Call Test CICD"
    runs-on: ubuntu-latest
    steps:      
      - name: "Call Github API"
        uses: indiesdev/[email protected]
        id: api2
        with:
          url: https://api.github.com/repos/:owner/:repo/dispatches
          method: "POST"
          accept: 200,201,204
          headers: '{ "authorization": "token :token", "accept": "application/vnd.github.everest-preview+json","content-type":"application/x-www-form-urlencoded" }'

          # you can use multiline format to construct json data object, the content should be yml format.
           
          # this format apply to inputs: body, headers and params
          "body": '{ "event_type": "remote-trigger-ui-test" }'
          log-response: true
      - name: "Use response"
        run: echo ${{ steps.api.outputs.response }}

錄製腳本

超酷的,什麼環境也沒特別配置,居然能夠跳出瀏覽器錄製,真的是太好用了

npx playwright codegen www.ibuypower.com --output ./tests/ibuypower.spec.ts
Playwright Inspector 錄製 ibuypower.com 訂單頁面自動化測試腳
Playwright Inspector 錄製 ibuypower.com 訂單頁面自動化測試腳

結論

使用 playwright 最大的好處,微軟挾著完整的生態,把整個工作流程都整合起來,我僅用很短的時間,就能將 vscode + github + teams 整合完成,爽度超級高。

補充

如果無法在 vscode 中使用 jest 偵錯測試檔,請執行以下指令

npm i -D @playwright/test
npx playwright install
npm install -D jest jest-playwright-preset playwright
code --install-extension Orta.vscode-jest
code --install-extension ms-playwright.playwright

參考資料

Github Action 上線後覺得太慢了,後來搬到自己家的 jenkins pipeline 中運行

pipeline {
    agent any

    tools {nodejs "Node Core"}

    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '20', daysToKeepStr: '', numToKeepStr: '20')
        timeout(time: 10, unit: 'MINUTES') 
    }

    stages {
        stage("GitHub Pull") {
            steps {
                git branch: "main", url: "https://[email protected]/iBuypowerUS/iBuypower.UI.Test.git"
            }
        }
        
        
        stage("Test and Report") {
            steps {
                script {
                    def errorEncountered = false
                    try {
                        bat "npm install"
                        bat "npm run testCase"
                    } catch(Exception e) {
                        errorEncountered = true
                        echo "Error during testing: ${e.getMessage()}"
                    } finally {
                        publishHTML([
                            alwaysLinkToLastBuild: true,
                            allowMissing: false,
                            keepAll: true,
                            reportDir: 'playwright-html-report',
                            reportFiles: 'index.html',
                            reportName: 'HTML Report',
                            reportTitles: 'HTML Report'
                        ])
                        if (errorEncountered) {
                            error "npm run testCase encountered an error."
                        }
                    }
                }
            }
        }
    }
}

P.S. Jenkins 顯示報表需要安裝 『 HTML Publisher plugin』,若報表的 html 顯示不出內容則需要在 Jenkins Script Console 執行

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

作者

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。閱讀更多

覺得這篇有幫助?

作者做的免費工具、每日 Podcast 與電子報,都在這裡。

Mark Ku · 本文採用 CC BY 4.0 授權,轉載請註明作者並附上原文連結。

留言

訂閱電子報

訂閱後即時收到新文章通知,不錯過任何技術分享。

提交即表示同意接收電子報,隨時可

熱門文章

View all
Mark Ku
··604

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案
Mark Ku
··471

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南
Mark Ku
··329

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略
Mark Ku
··240

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1
Mark Ku
··235

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調
Mark Ku
··216

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取