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

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 架构

image.png

使用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
    image.png
  2. 访问:本地ip地址:7100


    image.png
  3. control功能


    image.png

原理

  • 启动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


    image.png
  • 授予权限


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


    image.png

    image.png

2、Selenium Grid

Selenium 工具集

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

Selenium Grid 模式

image.png

Selenium Grid for Appium

image.png

优点

  • 所有测试的中心入口点
  • 管理和控制浏览器,手机设备等运行的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
    image.png
  • 修改测试脚本中的driver地址为上图的client地址,修改capability中的udid为设备的udid
    • desired_caps["udid"] = 'emulator-5554'
    • self.driver = webdriver.Remote("http://192.168.56.1:4444/wd/hub",desired_caps)
image.png
  • 注册到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
    image.png
  • 运行测试脚本即可

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版本
      image.png
    • nvm use 12,使用版本为12的nodejs
      image.png
  • 如果还是不行,考虑在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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,427评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,551评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,747评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,939评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,955评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,737评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,448评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,352评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,834评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,992评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,133评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,815评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,477评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,022评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,147评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,398评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,077评论 2 355

推荐阅读更多精彩内容