自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

三種方式自動化控制APP

移動開發(fā)
自動化控制APP不管是在工作還是生活方面,都可以幫助我們高效地完成任務,節(jié)省時間和精力。本文主要介紹自動化控制APP的三種常用方式。

自動化控制APP不管是在工作還是生活方面,都可以幫助我們高效地完成任務,節(jié)省時間和精力。本文主要介紹自動化控制APP的3種常用方式。

一、Python + adb

這種方式需要對Android有一些基本的了解。adb是一種用于調試Android應用程序的工具。使用Python和adb可以輕松實現(xiàn)自動化控制移動端APP。

1.特點

這種方式最簡單,但是控制效果也最粗糙。不同的手機對應的元素x,y軸的位置不同,所以不適合操作不同尺寸的所有手機。這種方式也只適合于開發(fā)者,對于普通用戶使用成本過高。

2.使用步驟

(1) 安裝Android SDK

具體細節(jié)略過,自行google安裝。安裝完畢后,配置好ANDROID_HOME環(huán)境變量。

(2) 安裝Python

具體細節(jié)略過,自行google安裝。

(3) 打開手機的開發(fā)者模式

同時開啟USB調試和顯示指針位置。

(4) 此時操作手機

可以看到有2根軸,同時最上方會顯示頁面焦點元素的x,y軸位置。

(5) 使用Python代碼+adb簡單控制APP

import time
import subprocess

# 點擊某個位置
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input tap 564  1861")
time.sleep(2)
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input tap 188  980")
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input tap 869  1808")
time.sleep(4)
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input tap 320  965")
# 輸入數(shù)據(jù)
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input text 15850501595")
# 按返回鍵
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input keyevent KEYCODE_BACK")
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input tap 512  1120")
# 輸入數(shù)據(jù)
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input text 15850501595")
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input keyevent KEYCODE_BACK")
subprocess.getoutput(f"adb -s Y2J7N17C27000069 shell input tap 843  1824")

(6) adb常用命令

- 查看手機設備:adb devices
- 查看設備型號:adb shell getprop ro.product.model
- 查看電池信息:adb shell dumpsys battery
- 查看設備ID:adb shell settings get secure android_id
- 查看設備IMEI:adb shell dumpsys iphonesubinfo
- 查看Android版本:adb shell getprop ro.build.version.release
- 查看手機網(wǎng)絡信息:adb shell ifconfig
- 查看設備日志:adb logcat
- 重啟手機設備:adb reboot
- 安裝一個apk:adb install /path/demo.apk
- 卸載一個apk:adb uninstall <package>
- 查看系統(tǒng)運行進程:adb shell ps
- 查看系統(tǒng)磁盤情況:adb shell ls /path/
- 手機設備截屏:adb shell screencap -p /sdcard/aa.png
- 手機文件下載到電腦:adb pull /sdcard/aa.png ./
- 電腦文件上傳到手機:adb push aa.png /data/local/
- 手機設備錄像:adb shell screenrecord /sdcard/ab.mp4
- 手機屏幕分辨率:adb shell wm size
- 手機屏幕密度:adb shell wm density
- 手機屏幕點擊:adb -s xxxxxxxxxxxxxxxxxxxxxxxxx shell input tap xvalue yvalue
- 手機屏幕滑動:adb shell input swipe 1000 1500 200 200
- 手機屏幕帶時間滑動:adb shell input swipe 1000 1500 0 0 1000
- 手機文本輸入:adb shell input text xxxxx
- 手機鍵盤事件:adb shell input keyevent xxxx

二、Android無障礙

這種方式是使用Android無障礙功能實現(xiàn)自動控制APP的效果。需要開啟Android無障礙功能,然后編寫Android代碼來控制另外的APP應用。

1.特點

這種方式需要開發(fā)者對Android有一些開發(fā)經(jīng)驗。優(yōu)點是:可以用Android開發(fā)出獨立的apk安裝包,安裝到普通用戶手機里,方便用戶使用。

2.使用步驟

(1) 安裝Android SDK、安裝Android Studio

具體細節(jié)略過,自行google安裝。安裝完畢后,配置好ANDROID_HOME環(huán)境變量。

(2) 使用Android自帶的tool工具

Android在level-21和之前的低版本,安裝完畢后有一個tool工具包,但是高版本移除了此工具包。所以在安裝SDK時還需要加上level-21版本。

使用Android自帶的tool工具,主要是為了查看APP的頁面布局和元素。但是monitor已經(jīng)不可用了,只能使用uiAutormatorViewer。

(3) 利用uiAutormatorViewer工具找到元素信息

用uiAutormatorViewer查看頁面元素所在的x,y軸的布局。然后編寫Android代碼控制點擊等效果。此工具顯示出來的界面如下:

(4) 代碼示例

在AndroidManifest.xml代碼里配置無障礙service,然后實現(xiàn)AccessibilityService類,實現(xiàn)onAccessibilityEvent方法。后續(xù)手機界面如果有變動,內部會自動觸發(fā)調用onAccessibilityEvent方法。

public class XXXXXAccessibilityService extends AccessibilityService {
    @Override
    public void onInterrupt() {

    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.e("無障礙", "來了");
        
        // 創(chuàng)建線程去執(zhí)行任務
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 后續(xù)代碼
            }
        }).start();
    }
}

無障礙功能本身也提供了多種尋找頁面元素的方法,比如:

/**
 * 根據(jù)ID找元素
 */
private AccessibilityNodeInfo findNodeById(String id) {
    AccessibilityNodeInfo root = getRootInActiveWindow();
    if (root == null) {
        return null;
    }
    List<AccessibilityNodeInfo> nodeList = root.findAccessibilityNodeInfosByViewId(id);
    if (nodeList != null) {
        for (int i = 0; i < nodeList.size(); i++) {
            AccessibilityNodeInfo node = nodeList.get(i);
            if (node != null) {
                return node;
            }
        }
    }
    return null

三、Python + Appium

Python加Appium可以組合成為一種自動化測試工具,可以用于測試和自動化控制移動端APP。

1.特點

這種方式可以自動化操作APP,但是使用者基本是開發(fā)者,普通用戶很難完成這一系列的操作。但是也有個優(yōu)點,有些頁面標記為不可點擊的元素,通過這種方式是可以點擊的。

Appium提供了更多的尋找頁面元素的方式,比如uiautomator、xpath、id等。

2.使用步驟

(1) 安裝Python

此處略過步驟,自行google。

(2) 安裝Appium

老版本的Appium直接包含了Appium server 和 Appium inspector,新版本的Appium安裝完畢后,需要在單獨安裝Appium inspector。通過Appium inspector可以查看手機當前頁面的xml布局。

# 安裝appium
npm i --location=global appium

# 安裝自動查看UI頁面的驅動
appium   uiautomator2driver

# 安裝inspector
下載安裝地址:https://github.com/appium/appium-inspector?tab=readme-ov-file

# 啟動server
appium server --use-driver=uiautomator2

# 安裝客戶端,代碼里會使用 appium-python-client 與server通信,
# 然后server在將指令下發(fā)到手機里的appium端
pip3 install appium-python-client

(3) 使用過程

安裝完畢后,就可以通過Python代碼控制App了。

  • 啟動Appium-server,appium uiautomator2driver。
  • 啟動Appium inspector,配置好手機,然后點擊start session,界面如下:

  • 通過Appium inspector查看頁面布局和元素,找出目標元素。

  • 編寫代碼:
device_app_info = AppiumOptions()
# 操作系統(tǒng)
device_app_info.set_capability('platformName', 'Android')
# 操作系統(tǒng)版本
# device_app_info.set_capability('platformVersion', '10')
device_app_info.set_capability('platformVersion', '9')
# 設備名稱
# device_app_info.set_capability('deviceName', '46F4C19402000952')
device_app_info.set_capability('deviceName', 'Y2J7N17C27000069')
# app package
device_app_info.set_capability('appPackage', 'cn.damai')
# app activity name
device_app_info.set_capability('appActivity', '.launcher.splash.SplashMainActivity')
# 使用uiautomator2驅動
device_app_info.set_capability('automationName', 'UiAutomator2')

# 連接appium server,server地址查看appium啟動信息
driver = webdriver.Remote('http://127.0.0.1:4723', options=device_app_info)

# 找到元素,控制元素
buy_btn = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("cn.damai:id/trade_project_detail_purchase_status_bar_container_fl")')
if buy_btn:
    buy_btn.click()

四、總結

以上3種方式都可以用于自動化控制移動端APP,但是又各有適用的場景。讀者需根據(jù)實際情況選擇其中的一種方式來實現(xiàn)自動化控制。

以上的方式也可以認為是一種爬蟲。還有一些自動化的方式是:先分析api請求,然后逆向分析js或者逆向分析apk包,破解其中的加密方式。然后直接調用api,這種方式后面單獨講解。

責任編輯:趙寧寧 來源: 不焦躁程序員
相關推薦

2022-04-08 10:21:35

IT自動化數(shù)字化轉型首席信息官

2009-07-20 15:08:41

Spring實例化Be

2015-10-21 14:23:08

反模式持續(xù)部署自動化運維

2012-07-17 09:16:16

SpringSSH

2024-07-08 09:03:31

2010-10-26 13:33:08

Oracle自動備份

2023-08-10 14:14:19

數(shù)字化轉型CIO

2023-05-16 07:26:17

2014-07-30 17:10:38

LVS集群負載均衡

2009-07-03 18:32:18

JSP頁面跳轉

2014-12-31 17:42:47

LBSAndroid地圖

2021-06-24 08:52:19

單點登錄代碼前端

2021-11-05 21:33:28

Redis數(shù)據(jù)高并發(fā)

2019-11-20 18:52:24

物聯(lián)網(wǎng)智能照明智能恒溫器

2018-01-08 16:58:31

營銷自動化

2010-03-12 17:52:35

Python輸入方式

2010-06-13 16:04:14

MySQL三種安裝方式

2020-11-01 17:10:46

異步事件開發(fā)前端

2011-04-08 11:13:50

CISCO IOS令牌桶雙桶

2020-09-27 09:50:35

自動化
點贊
收藏

51CTO技術棧公眾號