只需一個指令,在 60 秒內設定好所有執行 Nightwatch 測試的環境。我們新的入門用戶端旨在為首次設定 Nightwatch 或在現有專案中設定的開發人員提供強大的入門體驗。學習如何快速設定組態,並自動安裝網頁驅動程式和所需的套件。

執行方式

一切都從指令 npm init nightwatch 開始

> npm init nightwatch
Need to install the following packages:
  create-nightwatch
Ok to proceed? (y)

這是使用 npx create-nightwatch 的 npm 別名。 npx 是一個用於執行套件二進位檔而無需下載到本機相依性的指令。 npm 隨後將套件安裝在快取中並執行二進位檔。如果您要啟動一個新專案,也可以執行 npm init nightwatch <目錄>

選擇流程

設定基於一系列問題,以適應各種使用案例。

測試執行器

Nightwatch 也支援其他測試執行器。除了 Nightwatch,您還可以選擇 MochaCucumber 作為測試執行器。

JavaScript / Typescript

Nightwatch 在 v1.6.0 之後的測試檔案 支援 typescript。因此,您可以選擇使用 Javascript 或 Typescript 設定測試。

===============================
Nightwatch Configuration Wizard
===============================

Just answer a few questions to get started with Nightwatch:

We'll setup everything for you :-)

? What is your Language - Test Runner setup? (Use arrow keys)
❯ JavaScript - Nightwatch Test Runner
  JavaScript - Mocha Test Runner
  JavaScript - CucumberJS Test Runner
  TypeScript - Nightwatch Test Runner
  TypeScript - Mocha Test Runner

本機 / 遠端

您可以根據您是要在本機還是遠端機器上執行測試(或兩者皆是)來設定組態和下載。

 Where do you want to run your e2e tests? (Use arrow keys)
❯ On my local machine
  On a remote machine (cloud)
  Both

瀏覽器選擇

您可以選擇您將在哪些瀏覽器上進行測試,並會為它們建立組態。我們提供多選選項,讓您可以選擇要在其上進行測試的任意數量的瀏覽器。在本機上進行測試時,您也可以使用 selenium-server。

(Local) Where you'll be testing on? (Press <space> to select, <a> to toggle all, <i> to invert selection, and <enter> to proceed)
❯◉ Firefox
 ◯ Chrome
 ◯ Edge
 ◯ Safari
 ◯ IE (requires selenium-server)
 ◯ Local selenium-server

如果您是從 Mac 執行,safaridriver 預設存在,但必須啟用。

? Enable safaridriver (requires sudo password)? (Use arrow keys)
  Yes
❯ No, I'll do that later.

對於遠端測試,您應該提供遠端機器的 host 位址和 port。

? (Remote) What is the host address of your remote machine? (localhost)
? (Remote) What is the port on which your test backend is running on your remote machine? (80)

其他部分

除了測試方式的基本差異外,我們還會繼續在問卷中新增其他額外的組態。截至 22 年 5 月,清單如下。

  • 端對端測試的目錄位置
  • 您專案的 base_url
  • 使用 --generate-config 僅建立組態而不下載任何相依性

結果

如果未提供 --generate-config,則腳本會安裝必要的套件。

Installing the following packages:
- nightwatch
- @nightwatch/selenium-server
- @cucumber/cucumber

對於本機測試,腳本還會根據您選擇的瀏覽器安裝網頁驅動程式。

Installing/Updating the following webdrivers:
- geckodriver
- chromedriver
- iedriver
- safaridriver

預設情況下,會產生組態檔 nightwatch.conf.js,範例位於 tests/nightwatch-examples。如果 nightwatch.conf.js 已經存在,您可以選擇覆寫現有的組態檔或建立新的組態檔。

? Do you want to overwrite the existing config file? No, create a new one!
? What should your new config file be called? ie-windows-test.conf.js

然後您可以使用此指令執行範例。

npx nightwatch ./tests/nightwatch-examples
💡
在 60 秒內開始使用! npm init nightwatch
在 60 秒內開始使用!
我們熱愛開源 - BrowserStack
BrowserStack 為開源專案提供免費的網頁和行動測試。在我們的真實裝置雲端上測試您的網頁和行動應用程式。提交專案詳細資訊以獲得資格