您现在的位置是:首页 > 文章详情

用adb命令组装PowerShell实用小工具——Android小助手

日期:2017-04-12点击:549

[本文出自天外归云的博客园]

前置工作

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

原文链接:https://yq.aliyun.com/articles/331678
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章