概要

自動テストの目標は、アサーションを作成し、ロジックが機能していることを確認することです。 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')は、属性という名前の要素の属性に特定のテキストが含まれているかどうかをアサートするために使用できます。


//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')は、属性という名前の要素の属性が特定のテキストと等しいかどうかをアサートするために使用できます。


//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')は、属性という名前の要素の属性が指定された正規表現と一致するかどうかをアサートするために使用できます。


//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')
})

モバイルアプリでexpectsを使用する方法は、Webと同じです。 詳細については、ガイドを参照してください。

仮想デバイス、実デバイス、クラウドデバイスでネイティブモバイルテストを実行する方法を学ぶ