Android shell脚本自动启动app,关闭app,遇到crash自动停止

编程入门 行业动态 更新时间:2024-10-26 17:33:29

一、背景

当应用发现app在启动的时候会出现问题,但是需要手动测试很多次才可以复现的问题,可以考虑写一个自动化测试脚本

二、Android启动和关闭app命令

1、启动应用需要知道应用的包名和main activity类名

adb shell am start 包名/activity类名

2、关闭应用

adb shell am force-stop 包名

三、在shell中获取adb命令执行后的结果

可以通过如下方式将adb命令的结果赋值给变量

pid=`adb shell pidof 包名`

四、运行app和关闭app的代码

当检测到应用crash会自动停止运行

for((i = 1; i <= 1000; i++ ));  
do   
	# 启动应用
	adb shell am start 包名/activity类名
	echo "Run the app ${i} time." 

	# 判断应用是否启动,如果未启动等待5秒后重新启动
	for((j = 1; j <= 1000; j++ )); 
	do  
		pid=`adb shell pidof 包名`
		if [ ! -n "$pid" ]; 
		then 
			# 重试10次重新启动
			if [ $j -gt 10 ];
			then
				adb shell am start 包名/activity类名
			fi			
			sleep 0.5s
		else 
			break
		fi
	done

	# 等待5秒启动activity
	sleep 5s
	# 根据进程是否存在判断是否crash
	pid=`adb shell pidof 包名`
	if [ ! -n "$pid" ]; 
	then 
		echo "start crash"
		break
	else 
		echo "start res: ${pid}"
	fi
	
	# 关闭应用
	adb shell am force-stop 包名
	sleep 1s
done  

更多推荐

Android shell脚本自动启动app,关闭app,遇到crash自动停止

本文发布于:2023-06-10 22:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1349001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   自动启动   shell   Android   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!