總覽

到目前為止,如果您正在遵循文件操作,您已經設定了行動測試需求,並且也使用 Wikipedia 應用程式執行了您的第一個範例測試。如果沒有,請參閱安裝指南

現在,是時候撰寫您自己的測試了。

Nightwatch 具有原生指令,可自動化原生行動應用程式的測試。在底層,Nightwatch 包裝了 Appium 方法,以便使用者可以享受使用 Nightwatch 的熟悉感和便利性。所有指令和斷言都可以在 app 全域變數上使用。要檢查元素並找出選擇器,您可以使用 Appium Inspector。

Appium Inspector

當您為網頁瀏覽器撰寫測試時,您可以開啟瀏覽器、開啟開發人員工具、檢查元素並衍生選擇器。對於原生行動應用程式,您可以使用 Appium inspector 來完成相同的工作。如果您尚未安裝 Appium inspector,請參閱以下指南

如何啟動 Appium inspector?

步驟 1

要啟動 Appium Inspector,請先在您要檢查的行中新增 .debug() 指令。

nightwatch/examples/mobile-app-tests/wikipedia-android.js
describe('Wikipedia Android app test', function() {
    before(function(app) {
        app.click('id', 'org.wikipedia:id/fragment_onboarding_skip_button');
    });
    
it('Search for BrowserStack', async function(app) { app .click('id', 'org.wikipedia:id/search_container') .sendKeys('id', 'org.wikipedia:id/search_src_text', 'browserstack') .debug() //Added debug command. The inspector will be at a state where previous step is executed .click({selector: 'org.wikipedia:id/page_list_item_title', locateStrategy: 'id', index: 0}) .waitUntil(async function() { // wait for webview context to be available const contexts = await this.appium.getContexts();
return contexts.includes('WEBVIEW_org.wikipedia'); }) .appium.setContext('WEBVIEW_org.wikipedia') .assert.textEquals('.pcs-edit-section-title', 'BrowserStack'); // command run in webview context }); });

步驟 2
使用偵錯指令執行測試。Nightwatch 將啟動一個新的測試會話,執行步驟,並在遇到 .debug() 指令時暫停執行。現在,啟動 Appium inspector 應用程式,以便它可以附加到測試會話。

步驟 3
在 Appium inspector 中,您可以透過點擊「附加會話」並從下拉式選單中選擇會話,將其附加到上一步中啟動的會話。

步驟 4
附加會話後,您可以點擊左側的元素,以在右側面板中取得選擇器選項,如下所示

Selectors using Appium Inspector

您甚至可以先在 .debug() 終端機主控台中執行命令/斷言來測試它們,然後再將其新增到您的測試中。

選擇器

與網頁自動化的選擇器類似,尋找應用程式相關元素也需要選擇器。Appium 支援以下選擇器策略,因此預設情況下,Nightwatch 也會支援所有這些選擇器

  • id
  • xpath

詳細範例可在此處找到這裡

指令

用於測試原生行動應用程式的指令可分為 2 類

  1. 應用程式相關指令可用於與應用程式互動
  2. 裝置相關指令可用於與裝置互動
  • app.click('選擇器策略'、'選擇器值')
  • app.sendKeys('選擇器策略'、'選擇器值'、'要輸入的值')
  • app.clearValue('選擇器策略'、'選擇器值')
  • app.setValue('選擇器策略'、'選擇器值')
  • app.appium.getContexts()
  • app.appium.getContext()
  • app.appium.setContext()
  • app.appium.startActivity([opts][4], callback)
  • app.appium.getCurrentActivity(callback) [Callback 會回傳活動名稱]
  • app.appium.getCurrentPackage(callback) [Callback 會回傳封包名稱]
  • app.appium.getOrientation(callback) [Callback 會回傳 LANDSCAPE | POTRAIT]
  • app.appium.setOrientation(orientation, callback) [Callback 會回傳 LANDSCAPE | POTRAIT]
  • app.appium.getGeolocation(callback) [Callback 會回傳地理位置]
  • app.appium.setGeolocation({latitude, longitude, altitude}, callback)
  • app.appium.pressKeyCode([keycode][5], callback)
  • app.appium.longPressKeyCode([keycode][5], callback)
  • app.appium.hideKeyboard([callback])
  • app.appium.isKeyboardShown(callback) [Callback 會回傳布林值]

詳細範例可在此處找到這裡

斷言

最後,撰寫測試的目標是新增斷言,以便驗證端對端功能流程。用於原生行動應用程式的斷言與用於網頁的斷言非常相似。

斷言函式庫

  • app.assert.textContains(selector,'文字')
  • app.assert.textEquals(selector,'文字')
  • app.assert.textMatches(selector, '文字')
  • app.assert.attributeContains(selector,'屬性','值')
  • app.assert.attributeEquals(selector,'屬性','值')
  • app.assert.attributeMatches(selector, '屬性','值')
  • app.assert.selected(selector)
  • app.assert.enabled(selector)
  • app.assert.visible(selector)
  • app.assert.elementsCount(selector)
  • app.assert.elementPresent(selector)

Chai expect

此外,您也可以使用 Chai 樣式斷言

例如。

範例 Chai 例外
app.appium.getCurrentActivity(function(activity) {
    expect(activity.value).to.equal('.page.PageActivity')
})

詳細範例可在此處找到這裡

現在您已了解撰寫行動應用程式測試的基本知識,是時候更詳細地了解選擇器、指令和斷言了

選擇器
指令
斷言