BrowserStack 是最受歡迎的雲端測試平台之一。將其與 Nightwatch 搭配使用非常簡單,並且在自動產生的 nightwatch.conf.js 檔案中有組態設定。

擁有帳戶後,您可以設定下列環境變數。Dotenv 檔案也受 Nightwatch 支援。

  • BROWSERSTACK_USERNAME
  • BROWSERSTACK_ACCESS_KEY

請記得同時啟用 HTTP keepalive 以提高網路效能。

Nightwatch 與 BrowserStack 開箱即用,支援下列整合功能

  • 在 BrowserStack 上設定測試名稱
  • 將測試標示為「通過」或「失敗」
  • 在測試執行結束時顯示 BrowserStack 組建頁面的連結。

組態範例

使用此組態範例,嘗試在 BrowserStack 上執行測試。

nightwatch.conf.js
module.exports = {
  src_folders: [],
  
webdriver: { keep_alive: true, timeout_options: { timeout: 60000, retry_attempts: 3 } },
test_settings: { default: { launch_url: 'https://nightwatch.dev.org.tw' },
browserstack: { selenium: { host: 'hub-cloud.browserstack.com', port: 443 },
// More info on configuring capabilities can be found on: // https://www.browserstack.com/automate/capabilities?tag=selenium-4 desiredCapabilities: { 'bstack:options' : { local: 'false', userName: '${BROWSERSTACK_USERNAME}', accessKey: '${BROWSERSTACK_ACCESS_KEY}', } } },
'browserstack.chrome': { extends: 'browserstack', desiredCapabilities: { browserName: 'chrome' } },
'browserstack.firefox': { extends: 'browserstack', desiredCapabilities: { browserName: 'firefox' } },
'browserstack.ie': { extends: 'browserstack', desiredCapabilities: { browserName: 'IE', browserVersion: '11.0', 'bstack:options' : { os: 'Windows', osVersion: '10', local: 'false', seleniumVersion: '3.5.2', resolution: '1366x768' } } } } }

整合 BrowserStack Local

BrowserStack Local 是一項功能,可讓您測試無法公開存取的 localhost、預備環境或其他私人網站。若要使用此功能,您需要下載 browserstack-local 套件並使用您的測試套件執行。

注意:BrowserStack Local 只需要在組建開始時例項化一次,並在所有測試執行完成後停止。

您可以參考 BrowserStack 團隊維護的範例實作

透過 Proxy 伺服器連線至 BrowserStack

您可以在 Nightwatch 中新增 proxy 索引鍵到 nightwatch.conf.js 來指定 Proxy 設定

nightwatch.conf.js

  test_settings: {
    default: {
      desiredCapabilities: {
        // Your capabilities
      },
      proxy: {
        "host": "",     // "127.0.0.1"
        "port": "",     // "8081"
        "protocol": ""  // "http"
      }
    }
  }