0
点赞
收藏
分享

微信扫一扫

八、App自动化测试平台实战(二)

科牛 2021-09-26 阅读 31

1、多设备管理平台STF

  • 手机综合控制
  • 实时交互
  • 状态维护
  • 远程调试

STF平台简介

  • 安装
    • ubuntu下推荐用docker安装
    • mac 下docker部署需要额外配置
    • 不推荐在windows上部署
    • 如果要定制就用源码安装
  • 接入设备
  • 完善权限
  • 公布服务

具体安装步骤

  • 安装:
  • 环境依赖:
    • adb 配置环境变量
    • Rethinkdb 数据库
    • Nodejs:使用v8.6.1版本
    • Stf doctor检查环境
    • 目前支持android 2.3-9.0版本
#如果已经安装可以使用brew upgrade进行升级
brew install rethinkdb graphicsmagick zeromq protobuf yasm pkg-config

#了解下自己的node版本和npm的版本
npm version

#解决好***问题,搞不定就用cnpm,安装之前先卸载为佳,因为npm bug挺多,会误判一些lib
#npm uninstall -g stf
#rm  -rf /usr/local/lib/node_modules/stf/
npm install -g stf

常见的坑

  • node 版本太新,最好使用 LTS 版本
  • 依赖库安装时候出现了中断导致了各种不兼容
  • 文件权限问题,用户、组的权限设置不对

adb 架构

使用nvm管理Node环境

  • 查看node版本列表:nvm list

启动STF

  1. rethinkdb 首先启动
    • rethinkdb 或者 brew services start rethinkdb(后台服务)
  2. stf local 启动stf 服务
  3. 其他设备访问
  • 参数:--public-ip
  • 支持远程设备:-- allow-remote(模拟器一定要加上这个参数)

手机连接失败

演示

  1. stf local --public-ip 本地ip地址 --allow-remote

  2. 访问:本地ip地址:7100


  3. control功能


原理

  • 启动STF时默认会向手机中安装service,通过安装的service实时截图,将图片上传至STF平台进行展示。

测试流程

  • 申请设备
  • 申请远程连接
  • udid=xxx pytest xXXX
  • 释放远程连接
  • 释放设备

API

https://github.com/openstf/stf/blob/master/doc/API.md

key=xxxxxxx
curl -H "Authorization: Bearer $key" http://127.0.0.1:7100/api/v1/user | jq

key=368d6df9a82147d29a7a15f94b3d1495af2a6e857a654933bdda686f5e1ef5bf
device=emulator-5554
#权限
curl -H "Authorization: Bearer YOUR-TOKEN-HERE" https://stf.example.org/api/v1/user
#获得设备列表:
curl -H "Authorization: Bearer $key" http://localhost:7100/api/v1/devices  | jq .devices[].serial
#申请设备:
curl -X POST --header "Content-Type: application/json" --data '{"serial":"$device"}' -H "Authorization: Bearer $key"  http://127.0.0.1:7100/api/v1/user/devices
#远程调试:
curl -X POST -H "Authorization: Bearer $key" http://127.0.0.1:7100/api/v1/user/devices/$device/remoteConnect
#释放设备:
curl -X DELETE -H "Authorization: Bearer $key" http://127.0.0.1:7100/api/v1/user/devices/$device

  • 创建Token


  • 授予权限


  • 获取设备列表(jq命令用于格式化输出json格式)



2、Selenium Grid

Selenium 工具集

  • Selenium Remote Control
  • Selenium WebDriver
  • Selenium Server
  • Selenium Client
  • Selenium IDE
  • Selenium Grid

Selenium Grid 模式

Selenium Grid for Appium

优点

  • 所有测试的中心入口点
  • 管理和控制浏览器,手机设备等运行的Nodes/环境
  • 扩展
  • 并行运行测试
  • 跨平台的测试
  • 负载平衡

前提条件

  • Appium Server (npm而不是cnpm )
  • Android SDK
  • 设备:虚拟设备或真机
  • Node.js
  • selenium-server-standalone
  • ADB

启动Hub

启动node与配置文件

  • 官网: https://www.selenium.dev/downloads/
  • 启动nodes (appium/ selenium)
    • appium -p 4723 --nodeconfig appium_node1.json -g ./ appium_node1.log --session-override
    • appium -p 5723 --nodeconfig appium_node2.json -g ./ appium_node2.log --session-override

Appium 作为Selenium Grid 的节点配置

{
  "capabilities":
      [
        {
          "browserName": "<e.g._iPhone5_or_iPad4>",
          "version":"<version_of_iOS_e.g._7.1>",
          "maxInstances": 1,
          "platform":"<platform_e.g._MAC_or_ANDROID>"
        }
      ],
  "configuration":
  {
    "cleanUpCycle":2000,
    "timeout":30000,
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "url":"http://<host_name_appium_server_or_ip-address_appium_server>:<appium_port>/wd/hub",
    "host": <host_name_appium_server_or_ip-address_appium_server>,
    "port": <appium_port>,
    "maxSession": 1,
    "register": true,
    "registerCycle": 5000,
    "hubPort": <grid_port>,
    "hubHost": "<Grid_host_name_or_grid_ip-address>"
    "hubProtocol": "<Protocol_of_Grid_defaults_to_http>"
  }
}

Selenium Grid方案

  • 支持Android iOS模拟器
  • 支持Web浏览器
  • 支持所有兼容WebDriver协议的框架
  • 不支持在线交互调试

实战

  • 启动hub:java -jar path/selenium-server-standalone-3.141.59.jar -role hub

  • 修改测试脚本中的driver地址为上图的client地址,修改capability中的udid为设备的udid
    • desired_caps["udid"] = 'emulator-5554'
    • self.driver = webdriver.Remote("http://192.168.56.1:4444/wd/hub",desired_caps)

  • 注册到hub节点
    1. 创建文件node1.json
{
  "capabilities":
      [
        {
          "browserName": "<e.g._iPhone5_or_iPad4>",
          "version":"6.0",
          "maxInstances": 1,
          "platform":"ANDROID"
        }
      ],
  "configuration":
  {
    "cleanUpCycle":2000,
    "timeout":30000,
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "url":"http://127.0.0.1:4723/wd/hub",  # appium server所在服务的ip地址和端口
    "host": 127.0.0.1,
    "port": 4723,
    "maxSession": 1,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "hubHost": "192.168.56.1",  #Hub所在服务器的IP地址
    "hubProtocol": "http"
  }
}
  1. 启动node节点
    appium -p 4723 --nodeconfig path/node1.json

  • 运行测试脚本即可

3、并发执行多台设备

设置udid

  • desired_caps["udid"] = os.getenv("udid", None)

配置node.json,与设备一一对应

  • 分别配置三个node.json,文件中只需安卓版本对应即可,然后将url和port的端口号不一致即可(端口号最好隔一个设置)
    • node2.json
{
  "capabilities":
      [
        {
          "browserName": "<e.g._iPhone5_or_iPad4>",
          "version":"6.0",
          "maxInstances": 1,
          "platform":"ANDROID"
        }
      ],
  "configuration":
  {
    "cleanUpCycle":2000,
    "timeout":30000,
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "url":"http://127.0.0.1:4725/wd/hub",  # appium server所在服务的ip地址和端口
    "host": 127.0.0.1,
    "port": 4725,
    "maxSession": 1,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "hubHost": "192.168.56.1",  #Hub所在服务器的IP地址
    "hubProtocol": "http"
  }
}
  • node3.json
{
  "capabilities":
      [
        {
          "browserName": "<e.g._iPhone5_or_iPad4>",
          "version":"8.0",
          "maxInstances": 1,
          "platform":"ANDROID"
        }
      ],
  "configuration":
  {
    "cleanUpCycle":2000,
    "timeout":30000,
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "url":"http://127.0.0.1:4727/wd/hub",  # appium server所在服务的ip地址和端口
    "host": 127.0.0.1,
    "port": 4727,
    "maxSession": 1,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "hubHost": "192.168.56.1",  #Hub所在服务器的IP地址
    "hubProtocol": "http"
  }
}

分别开启三个appium服务

  • appium -p 4723 --nodeconfig path/node1.json
  • appium -p 4725 --nodeconfig path/node2.json
  • appium -p 4727 --nodeconfig path/node3.json

查看node节点注册结果

在终端执行脚本

  • udid=emulator-5556 pytest test_xueqiu.py
  • udid=emulator-5557 pytest test_xueqiu.py
  • udid=emulator-5558 pytest test_xueqiu.py

问题排查

  • 如果发现某些设备不能运行测试脚本,考虑切换nodejs版本
    • nvm list,查看所有nodejs版本

    • nvm use 12,使用版本为12的nodejs

  • 如果还是不行,考虑在capability中加入systemPort参数
    • desired_caps["systemPort"] = os.getenv("systemPort", None)
    • 在terminal中运行脚本时加入systemPort
      • udid=emulator-5556 systemPort=4723 pytest test_xueqiu.py

通过shell脚本并发运行

for i in `adn devices | grep 'device$' | awk '{print $1}'`
do
  echo $i
  udid=$i pytest 脚本文件路径/**.py &
done
举报

相关推荐

0 条评论