stf设备集群管理

移动设备多了之后不易管理, 如下需求: 1、手机综合控制, 2、实时交互, 3、远程控制; 

一、安装过程:

1、首先确认安装基础:

node -v
npm -v
java -version
adb version

安装db:   brew uninstall rethinkdb graphicsmagick zeromq protobuf yasm pkg-config

安装stf:   npm install -g stf

报错1:提示权限问题,

使用sudo安装后还是报错:

解决办法: 安装stf过程中  遇到node权限问题, 后经查询 将系统的node管理改成.nvm管理  

安装nvm  :  curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

  501  nvm ls-remote       # 查看目前node版本

  502  nvm install v12.9.1  #安装一个版本

  503  nvm install v11.0.0

  504  nvm ls              #查看本地安装的node版本

  505  nvm alias default v11.0.0  #切换node版本

问题2: 使用nvm安装还是报错:

后经查询,使用node版本不能过高,改成v8.14.0 后,安装成功

二、使用stf:

启动stf:  

rethinkdb
stf local

输入本地浏览器:localhost:7100

练习: 1、使用一台本地机作为远程机部署stf    (本地链接局域网主机命令 open vnc 192.168.x.x)

       2、远程机通过hub链接移动设备群  

       3、本地机通过远程链接脚本链接stf获取到移动设备list ,获取远程adb,启动本地appium 根据申请的多设备给本地appium传多组caps;   (远程机部署stf  adb)

       4、远程部署jenkins,把本地机作为节点配置;通过adb在申请的多个移动设备上顺序或并行执行自动化测试  执行本地机appium appcrawler等自动化测试

1、远程机启动stf  本地启动appium  获取StfToken

2、复制stf的api控制脚本

```

#!/usr/bin/env bash

#STF_TOKEN=9d06d1b4dae94c88863138bac8bf50369be151b812a44215b19e0b4f137b13fb
STF_TOKEN=fa46cb5aff944731a5cf88491b5b02a54125c9711d3741c2b160e3ceaea3928d
#STF_URL=http://localhost:7100
STF_URL=http://192.168.1.50:7100
DEVICE_SERIAL="demo"

function add_device
{
response=$(curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer $STF_TOKEN" \
--data "{\"serial\": \"$DEVICE_SERIAL\"}" $STF_URL/api/v1/user/devices)

success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')

if [ "$success" != "true" ]; then
echo "Failed because $description"
echo exit 1
fi

echo "Device $DEVICE_SERIAL added successfully"
}

function remote_connect
{
response=$(curl -X POST \
-H "Authorization: Bearer $STF_TOKEN" \
$STF_URL/api/v1/user/devices/$DEVICE_SERIAL/remoteConnect)

success=$(echo "$response" | jq .success | tr -d '"')
#增加一个拉取的设备名字的变量,方便后面并行执行
remote_device=$(echo "$response" | jq .remoteConnectUrl | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')

if [ "$success" != "true" ]; then
echo "Failed because $description"
echo exit 1
fi
remote_connect_url=$(echo "$response" | jq .remoteConnectUrl | tr -d '"')

adb connect $remote_connect_url

echo "Device $DEVICE_SERIAL remote connected successfully"
}

function remove_device
{
response=$(curl -X DELETE \
-H "Authorization: Bearer $STF_TOKEN" \
$STF_URL/api/v1/user/devices/$DEVICE_SERIAL)

success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')

if [ "$success" != "true" ]; then
echo "Failed because $description"
echo exit 1
fi

echo "Device $DEVICE_SERIAL removed successfully"
}

every_device(){
#获得可用设备列表
devices=$(curl -H "Authorization: Bearer $STF_TOKEN" $STF_URL/api/v1/devices | jq -r '.devices[] | select(.present==true)|.serial ')
for DEVICE_SERIAL in $devices
do
add_device
remote_connect
sleep 2
device=$(adb devices | grep : | awk '{print $1}' | head -1)
adb devices
eval $1
adb disconnect $device
sleep 2
adb devices
remove_device
done
}

```

3、配置jenkins;  在脚本目录控制stf执行自动化测试

docker run -d --name jenkins_9 -p 8080:8080 -p 50000:50000 -v /Users/seveniruby/jenkins/9:/var/jenkins_home jenkins/jenkins

. stf.sh           #加载shell脚本
DEVICE_SERIAL=$device  #stf页面 设备info中显示的
add_device                     #从stf系统中申请一台设备  stf系统中被申请的设备会被至成StopAutomation状态
remote_connect     #会弹出提示是否把key添加到本地,添加完成后本地执行adb devices  会有一台远程的设备  此时本地机可以使用远程机的adb控制移动设备

adb disconnect     #释放链接的设备

sleep 2
adb devices
echo remote_device=$remote_device  #打印一下链接的设备
adb -s $remote_device install -r ~/Downloads/xueqiu_wxdialog.apk  #stf申请的设备在释放后会自动卸载安装app  因此需要重新安装
java -jar appcrawler-2.4.0-jar-with-dependencies.jar -c demo_1.yml --capability "udid=$remote_device,systemPort=1${remote_device#*:}" 启动appium时支持多设备并发 需要独立udid 独立端口
adb disconnect $remote_device  #断开设备链接
remove_device         #移除设备

4、appium支持多设备并发执行测试,Android需要udid systemPort  chromDriverPort 保持独立

猜你喜欢

转载自www.cnblogs.com/1026164853qqcom/p/11421439.html
STF