[本文出自天外归云的博客园]
前置工作
1. 需要安装adb;
2. 需要本机设置PowerShell脚本运行策略。
脚本示例
PowerShell例子如下:
Function GetPkgAndActName(){ #运行脚本前确保app处于激活状态 $a = adb shell dumpsys window windows|findstr Focu; $b = $a -like "*mCurrentFocus*"; $b = $b.Trim(); $startIndex = $b.IndexOf("{"); $endIndex = $b.IndexOf("}"); $pkgAndActName = (($b.Substring($startIndex+1,$endIndex-$startIndex-1)).split(" "))[2]; return $pkgAndActName } Function GetCurrFlow(){ #运行脚本前确保app处于激活状态 $pkgAndActName = GetPkgAndActName; $pkgName = ($pkgAndActName.split("/"))[0]; $activityName = ($pkgAndActName.split("/"))[1]; $userId = (((((adb shell dumpsys package $pkgName | findstr userId).Trim()).split("="))[1]).split(" "))[0] $rets = adb shell cat /proc/net/xt_qtaguid/stats | findstr $userId foreach ($ret in $rets) { $spices = ($ret.Split(" ")) $flow += [int]$spices[5]+[int]$spices[7] } $flow/1000 } function Convert-Size { [cmdletbinding()] param( [validateset("Bytes","KB","MB","GB","TB")] [string]$From, [validateset("Bytes","KB","MB","GB","TB")] [string]$To, [Parameter(Mandatory=$true)] [double]$Value, [int]$Precision = 4 ) switch($From) { "Bytes" {$value = $Value } "KB" {$value = $Value * 1024 } "MB" {$value = $Value * 1024 * 1024} "GB" {$value = $Value * 1024 * 1024 * 1024} "TB" {$value = $Value * 1024 * 1024 * 1024 * 1024} } switch ($To) { "Bytes" {return $value} "KB" {$Value = $Value/1KB} "MB" {$Value = $Value/1MB} "GB" {$Value = $Value/1GB} "TB" {$Value = $Value/1TB} } return [Math]::Round($value,$Precision,[MidPointRounding]::AwayFromZero) } while($true){ Write-Host "输入数字进行选择" -ForegroundColor Green Write-Host "1 唤醒屏幕" -ForegroundColor Yellow Write-Host "2 输入文字" -ForegroundColor Yellow Write-Host "3 触发事件" -ForegroundColor Yellow Write-Host "4 向上滑动" -ForegroundColor Yellow Write-Host "5 向下滑动" -ForegroundColor Yellow Write-Host "6 向左滑动" -ForegroundColor Yellow Write-Host "7 向右滑动" -ForegroundColor Yellow Write-Host "8 删除输入" -ForegroundColor Yellow Write-Host "9 屏幕截图" -ForegroundColor Yellow Write-Host "10 获取手机分辨率" -ForegroundColor Yellow Write-Host "11 获取手机系统版本" -ForegroundColor Yellow Write-Host "12 获取当前app包名和活动名(运行脚本前确保app处于激活状态)" -ForegroundColor Yellow Write-Host "13 流量统计(运行脚本前确保app处于激活状态)" -ForegroundColor Yellow Write-Host "14 进行简单monkey测试" -ForegroundColor Yellow $choice = Read-Host "请选择" switch($choice) { 1{adb shell input keyevent 26} 2{$text = Read-Host "输入文字";adb shell input text $text} 3{$event = Read-Host "输入事件代号";adb shell input keyevent $event} 4{adb shell input swipe 200 800 200 100} 5{adb shell input swipe 200 100 200 800} 6{adb shell input swipe 500 100 100 100} 7{adb shell input swipe 100 100 500 100} 8{ [int]$amount = Read-Host "输入要删除的字符数量"; for($i=0;$i -lt $amount;$i++) { adb shell input keyevent 67; } } 9{ $result = adb devices; $device_id = $result[1].Split()[0]; adb -s $device_id shell /system/bin/screencap -p /sdcard/screenshot.png; adb -s $device_id pull /sdcard/screenshot.png d:/screenshot.png; D:\screenshot.png } 10{adb shell wm size} 11{adb shell getprop ro.build.version.release} 12{ $pkgAndActName = GetPkgAndActName; $pkgName = ($pkgAndActName.split("/"))[0]; $activityName = ($pkgAndActName.split("/"))[1]; "包名:"+$pkgName; "活动名:"+$activityName; } 13{ Read-Host "按任意键开始统计"; $startFlow = GetCurrFlow; Write-Host "流量监控中……`n" -ForegroundColor DarkMagenta; Read-Host "按任意键结束统计"; $endFlow = GetCurrFlow; $consumedFlow = [int]$endFlow-[int]$startFlow $consumedFlowKb = Convert-Size -From KB -To KB -Value $consumedFlow $consumedFlowMb = Convert-Size -From KB -To MB -Value $consumedFlow "共消耗流量:"+$consumedFlowKb+"kb("+$consumedFlowMb+"mb)"; } 14{ $count = Read-Host "请指定随机事件数" $pkgAndActName = GetPkgAndActName; $pkgName = ($pkgAndActName.split("/"))[0]; adb shell monkey -p $pkgName -v $count; } } }
可以根据实际测试过程中反复手点的过程进行组装调配。比如在反复测试登录的情况下,就要反复输入密码,如果来回用手点就比较麻烦,用这个小工具的话就非常轻松了,按一下上再敲一下回车就搞定了。以下是进行统计指定时间内android应用流量的消耗:
退出:ctrl+c