概述

任何自动化测试的目标都是编写断言并确保逻辑正常工作。在 Nightwatch 中,断言可以有两种方式

  • app.assert.command(params)
  • 期望

断言

所有断言都组织在 .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');

元素具有属性,如文本、索引、资源 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 期望

除了 .assert 命名空间下的断言之外,Nightwatch 还支持 BDD 风格的期望断言。例如


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

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

在移动应用程序中使用期望的方式与 Web 相同。请参阅 指南 以了解更多详细信息。

学习 如何在虚拟、真实和云设备上运行原生移动测试