e2e 模擬用戶行爲的測試

nightwatch.js

Nightwatch.js是一種易於使用的基於瀏覽器的應用程序和網站的基於Node.js的端到端(E2E)測試解決方案。它使用強大的W3C WebDriver API來對DOM元素執行命令和斷言。

需要安裝的npm包

selenium-server:webdriver測試服務器的nodejs搭建
nightwatch:對selenium-server的包裝,簡化其配置
chromedriver:selenium的chrome測試環境插件,如果是firefox、ie等都需要重新下測試環境插件。
chromedriver npm 裝不上 就用 cnpm
Firefox 使用GeckoDriver進行測試

nightwatch.json

默認情況下 使用 nightwatch.json 的配置文件
也可以使用 nightwatch.conf.js 來配置文件
兩個文件同時存在 優先使用 nightw atch.conf.js

{
//  測試所在的文件夾 不能識別自文件夾
  "src_folders" : ["tests"],

//輸出報告文件位置
  "output_folder" : "reports",

//加載自定義命令
  "custom_commands_path" : "",

//加載自定義斷言
  "custom_assertions_path" : "",

//加載頁面對象文件
  "page_objects_path" : "",

//全局位置
  "globals_path" : "",

//Selenium Server 相關配置
  "selenium" : {

      //是否自動管理 selenium 過程。
    "start_process" : false,

    //selenium jar
    "server_path" : "",

    //Selenium日誌記錄
    "log_path" : "",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "",
      "webdriver.gecko.driver" : "",
      "webdriver.edge.driver" : ""
    }
  },
//測試相關的配置
  "test_settings" : {
    "default" : {
    //brower.launch_url 可以在用例中使用
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
    //是否顯示擴展的Selenium命令日誌
      "silent": true,

    //錯誤截圖
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
    //傳遞給Selenium WebDriver指定瀏覽器名稱以及其他功能
      "desiredCapabilities": {
        "browserName": "firefox",
        "marionette": true
      }
    },

    "chrome" : {
      "desiredCapabilities": {
        "browserName": "chrome"
      }
    },

    "edge" : {
      "desiredCapabilities": {
        "browserName": "MicrosoftEdge"
      }
    }
  }
}

desiredCapabilities 相關配置
nightwatch.js 官網

Chai Expect

Nightwatch 基於Chai expect 的斷言庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章