Android CTS的TCL/Expect交互式自动化脚本

runCTS.exp脚本主要是连接Android系统真机后运行Android cts测试计划用的,过程中对部分异常情况做的处理,脚本尚不完善,仅供参考。
(脚本中的内容的解释请参考 http://lhq1013.iteye.com/blog/907633)

#!/usr/bin/expect

set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set timeout -1
spawn $env(SHELL) ;#启动一个SHELL进程
match_max 2000 ;#设置最大匹配的字符数
expect "*"

# proc: reboot telephone
proc restartPhone {} {
spawn ./adb shell
expect "# "
sleep 5
send "reboot\r"
sleep 5
expect "*"
puts "-------------reboot ok"
}

# proc: set permissions for device
set passwords {rxyrxy vmkid} ; #list of passwords.You have three chances at most to enter the right password.

proc setPermissions {} {
puts "-----------setPermissions"
set count 0
global passwords ;#global 用于在过程中引用全局变量

send "sudo -s\r"
expect { ;#开始循环匹配send后输出的结果
"*# " { send "./adb kill-server\r" } ;#双引号中为要求匹配的字符,“*”为通配符,花括号中未匹配上后需要做的动作
"Sorry, try again." {sleep 2; incr count 1; set password [lindex $passwords $count]; send "${password}\r"; exp_continue }
"*password*" { set password [lindex $passwords $count]; send "${password}\r"; exp_continue } ;#exp_continue表示跳出此次循环,继续下次;如果不写在匹配上后,直接跳出循环
}
expect "*# "
send -- "./adb devices\r"
expect {
"*List of devices attached \r*device\r" { puts "------------------connect device ok"; sleep 2; }
"*List of devices attached *" { sleep 5; setPermissions; exp_continue}
}
}

# proc: start to run the test plan
proc executePlan {plan} {
set cmd "start --plan "
lappend cmd $plan ;#在变量cmd后面追加变量plan的值
set timeout 2400 ;#设置超时时间为40分钟

send $cmd
send "\r"
expect {
"*Choose a session" {sleep 1; send "1\r"; exp_continue}
"Test summary:*pass*fail*timeOut*notExecuted*Total" {puts "----------$plan.------run--over"; sleep 8 }
"offline" {puts "----------$plan.offline----"; restartPhone; sleep 13; exp_continue}
#"Installing met timeout due to Unknown reason." {sleep 10; puts "----------$plan.offline----"; restartPhone; exp_continue}
"*\r" {sleep 20; exp_continue}
timeout {puts "--------timeout--------"; restartPhone; exp_continue}
}
}

#prepare for start run plans, connect to telephone

# how to catch the ANDROID_BUILD_TOP no defined error.?
puts "----------------------------------------------------"
set status [catch { set dirPath $env(ANDROID_BUILD_TOP) } result ]
if { $status != 0 } {
puts stderr "Please run 'build/envsetup.sh' first!"
exit 1
}

setPermissions ;#调用前面定义好的setPermissions过程

send "cd ${dirPath}/out/host/linux-x86/cts/android-cts/tools\r"
expect "*# "
send "./startcts\r"
expect "*connected\r
*cts_host > "
sleep 8

# which plans would be run.
# Here you can use commandlineparameter to set the value of plans which would be run.
set plans $argv
if {$plans eq ""} {
set plans {Signature RefApp Performance AppSecurity Android VM Java CTS}
}

# if a plan is ok, which should be removed from the full plans!?
#The variable plans is a list. Here use FOR-Loop to read the value of the full plans that specified list index.
#It will run the next one until the previous one is over.
for {set index [expr [llength $plans] - 1]} {$index >= 0} {incr index -1} {

set plan [lindex $plans $index]
executePlan $plan
puts "--------------------next-------------------"
sleep 13
}

exit
expect eof

到此为止,该脚本结束。测试脚本,只要在终端运行"./runCTS.exp"即可,若不想运行所有测试计划,也可用命令行参数在启动前指定需要运行的计划,各个计划之间用空格隔开,如"./runCTS.exp Android Java Vm"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值