總覽

Github actions 讓您可以輕鬆地自動化、自訂和執行您的軟體開發工作流程,例如直接從您的儲存庫建置和測試。

先決條件

  • 已推送至 Github 的待測專案
  • 測試在您的本機系統中正確執行

設定指南

在此範例中,我們將學習如何使用 Github actions 和 nightwatch-examples Github 儲存庫來執行 Nightwatch 測試

步驟 1:安裝 NodeJS 外掛程式

首先前往您儲存庫的 Github Actions 區段,然後按一下新增工作流程來設定 NodeJS 外掛程式,並在搜尋工作流程輸入方塊中輸入 node,然後按一下 Node.js 外掛程式的設定

NodeJS Plugin Setup

步驟 2:設定 .yml 檔案

現在您必須在 node.js.yml 檔案內寫入步驟來執行您的測試。

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Nightwatch Tests

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
        # See supported Node.js release schedule at https://node.dev.org.tw/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js $undefined
      uses: actions/setup-node@v3
      with:
        node-version: $undefined
        cache: 'npm'

    - name: npm-install
      run: npm ci
    - run: sudo apt-get install xvfb

    - name: Run Nightwatch tests
      run: xvfb-run --auto-servernum npm test -- --env chrome

請參閱 指南 以取得 .yml 檔案的更詳細版本。

步驟 3:將 .yml 檔案推送至 github

現在請在填寫完「提交新檔案」表單後,按一下提交新檔案按鈕。

NodeJS Plugin Setup

您也可以略過上述步驟,並直接從本機推送您的 .yml。但請確保檔案路徑應始終為 .github/workflow/file_name.yml

步驟 4:如何執行測試?

一旦您將變更推送至 Github 並提出提取請求,管線就會啟動,您的測試將自動執行。