[英]Android UI Automation Without Computer
嗨,我想在我的 android 手機中自動執行一個簡單的任務,這涉及檢查單個應用程序頁面上的所有復選框(應用程序沒有檢查所有功能)。
有什么方法可以像使用 selenium 的瀏覽器一樣在 android 中自動化 UI?
我對 Android 測試自動化工具有所了解,但我不想每次都將手機連接到計算機。 我正在尋找一種方法來從我的手機上做到這一點。 也許一些允許這樣做的庫,我可以在其上構建一個簡單的 android 應用程序?
如果沒有這樣的庫或工具,你能推薦一些替代品嗎?
編輯:(注意:我不是 Android 開發人員,所以如果我解釋錯誤,我提前道歉)在答案中找到了解決方案。
(注意:我不是 Android 開發人員,所以如果我解釋錯誤,我提前道歉)
所以使用輔助功能服務android自動化可以完成。 以下是有關如何使用此服務進行自動化的一些詳細信息。
首先你為你的應用實現無障礙服務類
public class SomeAppAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
//if some event occurs this method is called you can implement appropriate action here when you send a custom event from your app code
}
@Override
public void onInterrupt() {
}
}
之后,像任何其他服務一樣在清單中聲明它。 服務聲明示例
<service android:name=".service.SomeAppAccessibilityService"
android:label="some app Accessibility Service"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/someappservice"/>
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>
</service>
您可以谷歌搜索不同參數的含義等等。 一些基本解釋如下: android:name (在這里你告訴服務你的 SomeAppAccessibilityService 類在哪里) <meta-data android:resource= (你的包含無障礙服務屬性的 xml 文件在哪里)
屬性的資源 xml 文件示例:
<?xml version="1.0" encoding="utf-8"?><accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes = "typeAllMask"
android:packageNames="com.example.app1,com.example.app2"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>
現在完成所有這些之后,您可以在 onAccessibilityEvent 方法中查找輔助功能事件,如下所示
if(accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_ANNOUNCEMENT) {} //for example i want to capture announcement event
當一些無障礙事件(可能是點擊、長按、公告甚至自定義觸發事件)發生時,OnAccessibilityEvent 方法被調用,我們可以使用它通過無障礙服務獲取當前屏幕信息
AccessibilityNodeInfo info=getRootInActiveWindow(); //root view node info
AccessibilityNodeInfo childElem = info.getChild(n); //for getting nth child of root node
info.getChildCount(); //to get total size of child nodes
//then you can iterate on those children to get their child nodes
childElem.performAction(AccessibilityNodeInfo.ACTION_CLICK); //to perform some action on child elem like click as shown here
//there are other many useful AccessibilityNodeInfo class method that you can use
從您的應用程序或某些其他服務發送自定義事件。 例如,我在一些第三方應用程序(假設稱為 App1)上繪制了疊加層,我想使用我自己的應用程序進行自動化。 當我單擊覆蓋上的某個按鈕時,我希望在該第三方應用程序 (App1) 的當前窗口上執行某些操作。 要執行某些操作,可訪問性服務需要捕獲某些事件。 您可以捕獲單擊等原始事件,也可以發送自定義事件(可能有更精細的方法可以做到這一點,但在發布時我沒有找到任何方法)。 這是例子
AccessibilityManager manager = (AccessibilityManager)this.getSystemService(getApplicationContext().ACCESSIBILITY_SERVICE);
if(manager.isEnabled()){
AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
//event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
event.setEnabled(true);
event.setClassName(getClass().getName());
event.setPackageName("com.example.App1"); //package name for that third part app
event.setContentDescription("checkAll:true"); //i used description to send data to accessibility service as i could not find any other option to do so at the time of writing
//for example you can send the id of button and state of button etc in content description
event.getText().add("some text");
manager.sendAccessibilityEvent(event);
}
在 onAccessibilityEvent 中,您可以按如下方式捕獲此事件
if(accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_ANNOUNCEMENT) {
accessibilityEvent.getContentDescription()//to get content description and then you can parse data out of this string to perform some specific action based on that data}
這些是我前兩天發現的點點滴滴。 我使用這些零碎的東西在 Steam 應用上自動執行了一些操作。 我知道這不是一個正確的指南,也可能不是很准確。 我寫這篇文章的唯一目的是,如果有人和我在同一條船上,他可能會開始使用這個我花了很多天的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.