Write assertions for native mobile apps tests
Overview
The goal of any automated test is to write assertions and ensure that the logic is working. In Nightwatch there are 2 ways in which assertions can be done:
- app.assert.command(params)
- expects
Assertions
All assertions are organised under the .assert
namespace.
Text Related
Text Contains
app.assert.textContains(selector,'text')
can be used to assert if an element’s text contains a certain 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');
Text Equals
app.assert.textEquals(selector,'text')
can be used to check if an element’s text exactly equals a particular 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');
Text Matches
app.assert.textMatches(selector,'regex')
can be used to check if the text of an element matches with the given 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');
Attribute related
Elements have attributes such as text,index,resource-id etc. These can be found using Appium inspector as shown below
Assertions can be performed on attributes using the attribute related assertions.
Attribute Contains
app.assert.attributeContains(selector,'attribute','text')
can be used to assert if an element’s attribute named attribute contains a certain text.
//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');
Attribute Equals
app.assert.attributeEquals(selector,'attribute','text')
can be used to assert if an element’s attribute named attribute equals to a particular text.
//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');
Attribute Matches
app.assert.attributeMatches(selector,'attribute','regex')
can be used to assert if an element’s attribute named attribute matches the given regex
//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');
Selected
Verify if an element is in a selected state with app.assert.selected(selector)
method.
//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'});
Enabled
Verify if an element is in an enabled state with app.assert.enabled(selector)
method.
//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'});
Visible
Verify if an element is visible with app.assert.visible(selector)
method.
//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'});
Elements count
To verify the count of elements with a particular selector use the 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);
Present
Verify if an element is present in the render tree with app.assert.elementsPresent(selector)
method.
//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
In addition to the assertions under the .assert namespace,Nightwatch also supports BDD style expect assertions. E.g.:
app.appium.getCurrentActivity(function(activity){
expect(activity.value).to.equal('.page.PageActivity')
})
app.appium.getCurrentActivity(function(activity){
expect(activity.value).to.equal('.page.PageActivity')
})
The way to use expects with mobile apps is the same as web. Please refer to the guide for more details.
Recommended next steps
Learn how to run native mobile tests on virtual,real and cloud devices