概觀

使用指令,您可以透過指定選擇器與行動應用程式中的元素或 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')

內容

單一應用程式可以有多個內容,例如網頁檢視或原生應用程式。管理內容對於某些流程(例如 Authentication,可能會在您的原生應用程式中載入網頁檢視)至關重要。

取得內容

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

這些指令可用於與作業系統互動,以存取作業系統/裝置層級功能

啟動 Activity

您可以使用 app.appium.startActivity(options) 啟動新的 Activity。Options 是具有以下參數的 JSON 物件

  1. appPackage STRING:套件名稱
  2. appActivity STRING:Activity 名稱
  3. appWaitPackage STRING:在此套件啟動後,自動化將開始
  4. appWaitActivity STRING:在此 Activity 啟動後,自動化將開始
  5. intentAction STRING:將用於啟動 Activity 的 Intent 動作
  6. intentCategory STRING:將用於啟動 Activity 的 Intent 類別
  7. intentFlags STRING:將用於啟動 Activity 的旗標
  8. optionalIntentArguments STRING:將用於啟動 Activity 的其他 Intent 引數
  9. 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'
})

取得目前 Activity

使用 app.appium.getCurrentActivity() 擷取目前的 Activity 名稱


//Get current activity name
const activity =  await app.appium.getCurrentActivity();

取得目前套件

使用 app.appium.getCurrentPackage() 擷取目前的套件名稱


//Get current package name
const package =  await app.appium.getCurrentPackage();

取得方向

擷取裝置的目前方向。傳回的值將為 POTRAITLANDSCAPE


//Get current orientation
const orientation =  await app.appium.getCurrentOrientation();

設定方向

將裝置的方向設定為 LANDSCAPEPOTRAIT


//Set current orientation to LANDSCAPE
await app.appium.setOrientation('LANDSCAPE');

取得地理位置

擷取裝置的目前地理位置。傳回的值將包含 latitudelongitudealtitude


//Get current geolocation
const geolocation =  await app.appium.getGeolocation();

設定地理位置

使用 latitudelongitudealtitude 設定裝置的地理位置


//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 搭配使用

斷言