之前大佬给画了一整套app ui automation的框架,我负责写测试用例并尝试部署到服务器上自动跑,但由于airtest很依赖版本,当时踩坑踩的模拟器都打不开(后面会专门写一篇,希望大家不要走我的弯路😩),就没有继续往下走,但还是学到了一些部署思路,觉得自动化或者测开或许能用上,在这记录下
1.环境配置
Xcode11.4.1
Airtest
Python3.7
2.背景
希望在Mac Mini服务器上定时跑ui测试脚本,并发送测试报告
3.流程
Airtest写完的脚本可以通过launchctl定时跑
定时跑完的脚本在远程的mac mini服务器可以生成报告,报告会发到聊天群里,群里的人想要访问这个报告的话,就需要打开nginx
1.UI自动化脚本用 airtest
2.编写launch.py脚本,包含了测试的设备,测试的case,通知等操作
3.在终端非可视化的状态下,运行launch.py
4.将上述操作用shell 脚本代替(思路)
- git最新代码
- 打开模拟器
- 安装iOS-tagent: 1.2-1.4可以参考命令行方式打开
WebDriverAgent: Starting WebDriverAgent · facebookarchive/WebDriverAgent Wiki
- 打开nginx
Case跑完后,报告会以一个服务器ip地址+端口的形式出现,在服务器里可以在本地访问远程链接
4.连接远程服务器后的操作
连接远程服务器后,在终端开启模拟器
xcrun simctl list 查看存在的设备
xcrun simctl boot +设备id 开启模拟器
c test & 命令行替代Xcode里图形化的一些设置,此命令会生成nohup.out文件
tail -f nohup.out 查阅正在改变的nohup.out文件,这个文件末尾会包含ServerURLHere->http://xxx.xxx.x.x.x:8100<-ServerURLHere,如果没有,则因为模拟器的版本太高了,需要返回02重新开启一个较低版本的模拟器
curl http://xxx.xxx.x.x.x:8100 返回json字符串证明代理开启成功
tail -f nohup.out 也会返回ServerURLHere->http://xxx.xxx.x.x.x:8100<-ServerURLHere
然后可以clone代码,运行python launch.py
- 如果运行出错的话,可以看下服务器上的python版本是否合适
- 不合适的话需要看下python环境是否合适
- conda env list查看当前存在的python虚拟环境是哪个版本
- conda activate py37激活对应的python版本,再重新运行launch.py
- 开启nginx之后,curl +地址 返回对应report地址可以看到报告内容,在本地浏览器访问相应的地址也可以打开
- nginx -s stop 关闭nginx之后,则无法访问上面的地址
- 如果出现在服务器里可以看到返回但是本地无法访问,可能是服务器里开启的模拟器关闭了远程访问的权限,可以找运维开放下权限
5.将命令集成到shell脚本里
launctl 貌似还没集成到shell里:
#!/bin/sh
#进入launch.py程序所在目录
cd /Users/user/Library/LaunchAgents &&
#记录一下开始时间
echo ‘date’ >> log/run.log &&
#拉取最新代码
CONUSER='https://git.xxx.supply/xxx-qa/app-automation.git'
CONPASS='git password'
cd /Users/user/TestAutomation # test folder
rm -rf app-automation
git reset --hard
expect -c "spawn git clone https://git.xxx.supply/xxx-qa/app-automation.git; expect \"*Usernamee*\" { send \"${CONUSER}\n\"; exp_continue } \"*Password*\" { send \"${CONPASS}\n\" }; interact"
#判断模拟器是否开启
simulator_test=$(history | grep -wc 53D45EE7-EC1D-40F3-BA7F-CD11E20CC787)
if [ $simulator_test -eq 0 ]; then
xcrun simctl boot 53D45EE7-EC1D-40F3-BA7F-CD11E20CC787
nohup /Applications/Xcode11.4.app/Contents/Developer/usr/bin/xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination 'id=53D45EE7-EC1D-40F3-BA7F-CD11E20CC787’ test &
curl http://xxx.xxx.xxx.xx:8100
fi
nginx_process_num=$(ps -ef | grep nginx | grep -v grep | wc -l)
# 判断进程是否存在,不存在就启动Nginx
if [ $nginx_process_num -eq 0 ]; then
systemctl start nginx
fi
#运行airtest case
conda activate py37
python launch.py
#运行完成
echo 'finish' >> log/run.log
- 上面完全是东拼西凑的感觉,有时候会用到很多路径,各种拉代码,所以想着封装一下,后面用直接调就行,改造后看起来也比较整齐
- 在终端里运行run.sh(以下代码)
- 试了下可以自动拉取并更新最新的项目源码到本地,自动安装webdriveragent到模拟器上,但是在模拟器上需要手动点击网络允许框,之后会自动跑UI测试并生产报告
#!/bin/sh
#进入launch.py程序所在目录
#echo ‘date’ >> log/run.log &&
#echo "mac password" | sudo Xcode-select -s /Applications/Xcode\ 2.app/Contents/Developer
test_path='/Users/user/Desktop/test'
gitapp_path=$test_path'/project' # project git address
proj_path=$gitapp_path/xxx.xcodeproj # project.xcodeproj
wdaproj_path='/Users/user/iOS-Tagent/WebDriverAgent.xcodeproj'
automation_path='/Users/user/Desktop/work/app-automation'
sim_id=AEA9A845-BD28-467C-BE22-A443DB202566
sim_boot() {
xcrun simctl shutdown all
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app
xcrun simctl boot '$sim_id'
echo "模拟器已开启"
}
sim_shudown(){
xcrun simctl shutdown all
}
cmc_git() {
cd $cmc_path
rm -rf (project file)
CONUSER='https://git.xxx.supply/mobile/xxx.git'
CONPASS='xxx'
git reset --hard
expect -c "spawn git clone https://git.xxx.supply/mobile/xxx.git; expect \"*Usernamee*\" { send \"${CONUSER}\n\"; exp_continue } \"*Password*\" { send \"${CONPASS}\n\" }; interact"
cd $app_path
pod install
pod repo update
#timeout 5 cmd args
echo "app源码 已克隆到本地"
}
cmc_install(){
cd $gitapp_path
xcodebuild -scheme CoinMarketCap -project $proj_path -destination "id=$sim_id" -derivedDataPath build
xcrun simctl install booted build/Build/Products/Debug-iphonesimulator/xxx.app
echo "app 已安装到模拟器"
}
wda_install(){
cd $wdaproj_path
xcodebuild -project /Users/user/iOS-Tagent/WebDriverAgent.xcodeproj \
-scheme WebDriverAgentRunner \
-destination "id=$sim_id" \
test
echo "WDA已安装到模拟器"
}
script_launch(){
cd $automation_path
conda activate py37
python launch.py
}
simulator_test=$(ps -ef | grep $sim_id | grep -v grep | wc -l)
if [ $simulator_test -eq 1 ]; then
sim_shudown
echo "模拟器 & app & WDA 已就绪,准备跑测试脚本"
script_launch
else
echo "------模拟器未开启,准备开启------"
sim_boot
#echo "------准备克隆最新app代码------"
#cmc_git
echo "------准备安装app到模拟器------"
cmc_install
#echo "------准备安装WDA------"
#wda_install
#echo "------准备跑测试脚本------"
#script_launch
fi