Nightwatch JS
Outline
- nightwatch.conf.js 設定
- package.json 設定
- 資料夾結構
- nightwatch指令
- special case
nightwatch.conf.js 設定
config結構
|
|
基本設定
|
|
環境變數設定
|
|
將設定獨立成function,傳入globals所需參數
|
|
|
|
依照作業系統使用對應chrome driver
|
|
package.json 設定
|
|
nightwatch 參數
|
|
資料夾結構
|
|
- chrome driver和selenium執行擋
|
|
環境變數設定,包含url和account
|
|
參數設定、環境設定
|
|
測試程式碼
|
|
測試報告
|
|
nightwatch指令
windowMaximize(function callback)
- 將目前視窗最大化
- mac和windows最大化不同
- api
|
|
ResizeWindow
- 設定瀏覽器尺寸,尺寸可能導致dom element隱藏造成測試失敗
- api
|
|
Asynchronous before[Each] and after[Each]
- 每次測試執行前 / 後執行
- login, logout
- 在非同步操作完成後必須呼叫done(),否則會有timeout error
- api
|
|
perform(function callback)
- 執行簡單指令去介接API的callback function
- api
|
|
end(function callback)
- 關閉瀏覽器
- 結束session (使用session protocol command)
- api
|
|
url(string|function url)
|
|
click(string selector)
- DOM元素的click事件
- api
|
|
execute(string|function body, Array args)
- 如同使用chrome dev-tools執行javascript或jquery
- 注入Javascript執行
- api
|
|
pause(number ms)
- 以毫秒為單位做時間暫停
- api
|
|
waitForElementPresent(string selector, number time)
- 等待元素生成
- api
waitForElementNotPresent
|
|
waitForElementVisible(string selector, number time)
- 等待元素可見
- api
waitForElementNotVisible
|
|
moveToElement(string selector, number xoffset, number yoffset)
- 移動滑鼠游標
- api
- execute jQuery的寫法: $(element).trigger(“mouseenter”)
clearValue(string selector)
- 清除textarea 或 text input的值
- api
|
|
setValue(string selector, string|array value)
- 設定元素的值
- api
|
|
frame(string|number|null frameId)
|
|
element(string using, string value)
|
|
getAttribute(string selector, string attribute)
- 取得DOM元素上的屬性
- api
|
|
Demo - special case
- 上傳單個檔案
- 上傳多個檔案
- 抓取jQuery String
- 點擊submit按鈕
- 判斷ajax completed
上傳單個檔案
|
|
上傳多個檔案
|
|
抓取Query String
|
|
點擊submit按鈕
|
|
判斷ajax completed
- github找到的第三方元件,效果不確定
- 每0.1秒偵測是否有ajax completed event
|
|