概觀

任何自動化測試的目標都是編寫斷言並確保邏輯正常運作。在 Nightwatch 中,有 2 種方式可以完成斷言

  • app.assert.command(params)
  • expects

斷言

所有斷言都組織在 .assert 命名空間下。

文字包含

app.assert.textContains(selector,'text') 可以用來斷言元素的文字是否包含特定文字。


//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` contains text Browser
app.assert.textContains({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'Browser');

//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` contains text Browser
app.assert.textContains({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'Browser');

文字相等

app.assert.textEquals(selector,'text') 可以用來檢查元素的文字是否與特定文字完全相等。


//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` equals text BrowserStack
app.assert.textEquals({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'BrowserStack');

//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` equals text BrowserStack
app.assert.textEquals({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'BrowserStack');

文字符合

app.assert.textMatches(selector,'regex') 可以用來檢查元素的文字是否與給定的正規表示式相符。


//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` is alphabet only
app.assert.textMatches({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'/^[a-z]+$/i');

//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` is alphabet only
app.assert.textMatches({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'/^[a-z]+$/i');

元素具有諸如 text、index、resource-id 等屬性。這些可以使用 Appium 檢測器找到,如下所示

Element attributes in Appium inspector

可以使用與屬性相關的斷言在屬性上執行斷言。

屬性包含

app.assert.attributeContains(selector,'attribute','text') 可以用來斷言名為 attribute 的元素的屬性是否包含特定文字。


//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` text attribute contains Browser
app.assert.attributeContains({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'text','Browser');

//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` text attribute contains Browser
app.assert.attributeContains({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'text','Browser');

屬性相等

app.assert.attributeEquals(selector,'attribute','text') 可以用來斷言名為 attribute 的元素的屬性是否等於特定文字。


//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` text attribute equals BrowserStack
app.assert.attributeEquals({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'text','BrowserStack');

//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` text attribute equals BrowserStack
app.assert.attributeEquals({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'text','BrowserStack');

屬性符合

app.assert.attributeMatches(selector,'attribute','regex') 可以用來斷言名為 attribute 的元素的屬性是否與給定的正規表示式相符


//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` text attribute only contains alphabets
app.assert.attributeMatches({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'text','/^[a-z]+$/i');

//Assert if the element with id `org.wikipedia:id/pcs-edit-section-title` text attribute only contains alphabets
app.assert.attributeMatches({selector: 'org.wikipedia:id/pcs-edit-section-title',locateStrategy: 'id'},'text','/^[a-z]+$/i');

已選取

使用 app.assert.selected(selector) 方法驗證元素是否處於選取狀態。


//Assert if the element with id `org.wikipedia:id/button` is selected
app.assert.selected({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

//Assert if the element with id `org.wikipedia:id/button` is selected
app.assert.selected({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

已啟用

使用 app.assert.enabled(selector) 方法驗證元素是否處於啟用狀態。


//Assert if the element with id `org.wikipedia:id/button` is enabled
app.assert.enabled({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

//Assert if the element with id `org.wikipedia:id/button` is enabled
app.assert.enabled({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

可見

使用 app.assert.visible(selector) 方法驗證元素是否可見。


//Assert if the element with id `org.wikipedia:id/button` is visible
app.assert.visible({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

//Assert if the element with id `org.wikipedia:id/button` is visible
app.assert.visible({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

元素計數

若要驗證具有特定選擇器的元素計數,請使用 app.assert.elementsCount() API


//Assert if the element with id `org.wikipedia:id/list_item` has a count of 7
app.assert.elementsCount({selector: 'org.wikipedia:id/list_item',locateStrategy: 'id'},7);

//Assert if the element with id `org.wikipedia:id/list_item` has a count of 7
app.assert.elementsCount({selector: 'org.wikipedia:id/list_item',locateStrategy: 'id'},7);

存在

使用 app.assert.elementsPresent(selector) 方法驗證元素是否存在於轉譯樹狀結構中。


//Assert if the element with id `org.wikipedia:id/button` is present in the render tree
app.assert.elementsPresent({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

//Assert if the element with id `org.wikipedia:id/button` is present in the render tree
app.assert.elementsPresent({selector: 'org.wikipedia:id/button',locateStrategy: 'id'});

Chai Expects

除了 .assert 命名空間下的斷言之外,Nightwatch 也支援 BDD 樣式的 expect 斷言。例如:


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

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

將 expect 與行動應用程式搭配使用的方式與 Web 相同。如需更多詳細資訊,請參閱指南

瞭解如何在虛擬、真實和雲端裝置上執行原生行動測試