ネイティブモバイルアプリテストのコマンド
概要
コマンドを使用すると、セレクターを指定してモバイルアプリケーションまたはOS自体内の要素と対話できます。ネイティブモバイルアプリケーションのコンテキストでのセレクターの仕組みについては、こちらのガイドを参照してください。
アプリ関連コマンド
これらのコマンドは、モバイルアプリケーションと対話するために使用できます。
クリック
要素をクリックするには、app.click('セレクター戦略','セレクター')
またはapp.click(セレクターオブジェクト)
を単純に使用します。Appium inspectorを使用して要素を見つけることができます。
// Mention the selector strategy followed by the selector itself
// Click on the element with id `org.wikipedia:id/search_container`
app.click('id','org.wikipedia:id/search_container')
// Mention a selector object and index.
// Click on the nth element with id `org.wikipedia:id/page_list_item_title`
app.click({selector: 'org.wikipedia:id/page_list_item_title',locateStrategy: 'id',index: n})
// Mention the selector strategy followed by the selector itself
// Click on the element with id `org.wikipedia:id/search_container`
app.click('id','org.wikipedia:id/search_container')
// Mention a selector object and index
// Click on the nth element with id `org.wikipedia:id/page_list_item_title`
app.click({selector: 'org.wikipedia:id/page_list_item_title',locateStrategy: 'id',index: n})
入力/キー送信
アプリにテキスト入力が必要なフィールドが含まれている場合は、app.sendKeys('セレクター戦略','セレクター','テキスト')
またはapp.sendKeys(セレクターオブジェクト,テキスト)
を使用して、このような要素と対話できます。
//Mention the selector strategy followed by the selector itself
//Type the text 'Test' the element with id `org.wikipedia:id/search_container`
app.sendKeys('id','org.wikipedia:id/search_src_text','Test')
//Mention a selector object and index
//Type 'Test' in the nth element with id `org.wikipedia:id/search_src_text`
app.sendKeys({selector: 'org.wikipedia:id/search_src_text',locateStrategy: 'id',index: n},'Test')
//Mention the selector strategy followed by the selector itself
//Type the text 'Test' the element with id `org.wikipedia:id/search_container`
app.sendKeys('id','org.wikipedia:id/search_src_text','Test')
//Mention a selector object and index
//Type 'Test' in the nth element with id `org.wikipedia:id/search_src_text`
app.sendKeys({selector: 'org.wikipedia:id/search_src_text',locateStrategy: 'id',index: n},'Test')
値のクリア
要素の値をクリアするには、app.clearValue('セレクター戦略','セレクター')
またはapp.clearValue(セレクターオブジェクト)
を単純に使用します。Appium inspectorを使用して要素を見つけることができます。
//Mention the selector strategy followed by the selector itself
//Clear the value of the element with id `org.wikipedia:id/search_src_text`
app.clearValue('id','org.wikipedia:id/search_src_text')
//Mention a selector object and index
//Click on the nth element with id `org.wikipedia:id/search_src_text`
app.clearValue({selector: 'org.wikipedia:id/search_src_text',locateStrategy: 'id',index: n})
//Mention the selector strategy followed by the selector itself
//Clear the value of the element with id `org.wikipedia:id/search_src_text`
app.clearValue('id','org.wikipedia:id/search_src_text')
//Mention a selector object and index
//Click on the nth element with id `org.wikipedia:id/search_src_text`
app.clearValue({selector: 'org.wikipedia:id/search_src_text',locateStrategy: 'id',index: n})
値の設定
app.setValue('セレクター戦略','セレクター','値')
またはapp.setValue(セレクターオブジェクト,'値')
を使用して、要素の値を直接設定できます。
//Mention the selector strategy followed by the selector itself
//Type the text 'Value' the element with id `org.wikipedia:id/search_container`
app.setValue('id','org.wikipedia:id/search_src_text','Value')
//Mention a selector object and index
//Type 'Value' in the nth element with id `org.wikipedia:id/search_src_text`
app.setValue({selector: 'org.wikipedia:id/search_src_text',locateStrategy: 'id',index: n},'Value')
//Mention the selector strategy followed by the selector itself
//Type the text 'Value' the element with id `org.wikipedia:id/search_container`
app.setValue('id','org.wikipedia:id/search_src_text','Value')
//Mention a selector object and index
//Type 'Value' in the nth element with id `org.wikipedia:id/search_src_text`
app.setValue({selector: 'org.wikipedia:id/search_src_text',locateStrategy: 'id',index: n},'Value')
コンテキスト
単一のアプリケーションは、Webビューやネイティブアプリなど、複数のコンテキストを持つことができます。ネイティブアプリでWebビューをロードする可能性のある認証
など、一部のフローではコンテキストの管理が不可欠です。
コンテキストの取得
app.appium.getContext()
は現在のコンテキストを取得します
//Retrieve the current context
const context = await app.appium.getContext()
//Retrieve the current context
const context = await app.appium.getContext()
コンテキストの取得
app.appium.getContexts()
は、すべてのコンテキストを配列形式で取得します
//Retrieve all the contexts
const context = await app.appium.getContexts()
//Retrieve all the contexts
const context = await app.appium.getContexts()
コンテキストの設定
app.appium.setContext(コンテキスト)
を使用してコンテキストを切り替えることができます
//Switch the current context to 'WEBVIEW_org.wikipedia'. You can get the context names via getContexts()
await app.appium.setContext('WEBVIEW_org.wikipedia')
//Switch the current context to 'WEBVIEW_org.wikipedia'. You can get the context names via getContexts()
await app.appium.setContext('WEBVIEW_org.wikipedia')
デバイス/OS関連コマンド
これらのコマンドは、OSと対話してOS/デバイスレベルの機能にアクセスするために使用できます
アクティビティの開始
app.appium.startActivity(options)
を使用して新しいアクティビティを開始できます。optionsは、次のパラメーターを持つJSONオブジェクトです
- appPackage
STRING
: パッケージの名前 - appActivity
STRING
: アクティビティの名前 - appWaitPackage
STRING
: このパッケージが開始された後、自動化が開始されます - appWaitActivity
STRING
: このアクティビティが開始された後、自動化が開始されます - intentAction
STRING
: アクティビティを開始するために使用されるインテントアクション - intentCategory
STRING
: アクティビティを開始するために使用されるインテントカテゴリ - intentFlags
STRING
: アクティビティを開始するために使用されるフラグ - optionalIntentArguments
STRING
: アクティビティを開始するために使用される追加のインテント引数 - dontStopAppOnReset
BOOLEAN
: リセット時にアプリを停止する必要があるかどうか
//Start an Activity with package name `com.example` & activity name 'Activity'
app.appium.startActivity({
appPackage: 'com.example',
appActivity: 'Activity'
})
//Start an Activity with package name `com.example` & activity name 'Activity'
app.appium.startActivity({
appPackage: 'com.example',
appActivity: 'Activity'
})
現在のアクティビティの取得
app.appium.getCurrentActivity()
を使用して現在のアクティビティ名を取得します
//Get current activity name
const activity = await app.appium.getCurrentActivity();
現在のパッケージの取得
app.appium.getCurrentPackage()
を使用して現在のパッケージ名を取得します
//Get current package name
const package = await app.appium.getCurrentPackage();
向きの取得
デバイスの現在の向きを取得します。返される値はPOTRAIT
またはLANDSCAPE
になります。
//Get current orientation
const orientation = await app.appium.getCurrentOrientation();
向きの設定
デバイスの向きをLANDSCAPE
またはPOTRAIT
に設定します
//Set current orientation to LANDSCAPE
await app.appium.setOrientation('LANDSCAPE');
ジオロケーションの取得
デバイスの現在のジオロケーションを取得します。返される値には、latitude
、longitude
、およびaltitude
が含まれます。
//Get current geolocation
const geolocation = await app.appium.getGeolocation();
ジオロケーションの設定
latitude
、longitude
、およびaltitude
を使用してデバイスのジオロケーションを設定します
//Set current geolocation
await app.appium.setGeolocation({latitude:23.03,longitude: 34.23,altitude: 35.03});
キーボード関連
キーを押す
app.appium.pressKeyCode(キーコード)
を使用してキーボードの特定のキーを押します。キーコード値はこちらにあります。
//Press Keycode Back
await app.appium.pressKeyCode(4);
これはAndroidでのみ機能します
キーの長押し
app.appium.longPressKeyCode(キーコード)
を使用してキーボードの特定のキーを長押しします。キーコード値はこちらにあります。
//Press Keycode Back
await app.appium.longPressKeyCode(4);
これはAndroidでのみ機能します
キーボードを非表示にする
app.appium.hideKeyboard()
を使用してキーボードを非表示にします
//Hide keyboard
await app.appium.hideKeyboard();
キーボードが表示されているか
app.appium.isKeyboardShown()
を使用してキーボードが表示されているかどうかを確認します。これはブール値を返します。
//Is keyboard shown
const keyboardShown = await app.appium.isKeyboardShown();
推奨される次のステップ
セレクターとコマンドを理解したので、Nightwatchでアサーションがどのように機能するかを理解に進むことができます