使用Selenium Grid管理测试设备

Selenium Grid优点

  • Central entry point for all tests
  • Management and control of the nodes / environment where the browsers run
  • Scaling
  • Running tests in parallel
  • Cross platform testing
  • Load balancing

架构图

grid

Hub

  • Intermediary and manager
  • Accepts requests to run tests
  • Takes instructions from client and executes them remotely on the nodes
  • Manages threads

Hub是管理所有测试节点的中心。每个Selenium Grid由一个Hub组成。Hub需要与各自的客户端节点(如CI服务器、开发机器等)相连,Hub可以连接一个或多个测点节点。

Nodes

  • Where the browsers live
  • Registers itself to the hub and communicates its capabilities
  • Receives requests from the hub and executes them

这些测试设备节点运行着不同的Selenium实例。Grid中可以有很多节点。节点可以是不同的平台,不同的浏览器。Windows上的节点可能有能力提供IE作为浏览器选项,而这在Linux或Mac上是不可能的。

setting up you own Grid

官方说明: https://www.selenium.dev/documentation/en/grid/setting_up_your_own_grid/

java -jar selenium-server-standalone.jar -role hub -log log.txt

selenium-server-standalone.jar 从https://www.selenium.dev/downloads/ 下载。我下载的是selenium-server-standalone-3.141.59.jar。
下载selenium server(grid)
执行上述命令, 会看到grid运行在你本机IP的4444端口。
grid running

注册个iOS设备节点到Hub

按照http://appium.io/docs/en/advanced-concepts/grid/ 文档进行,

编辑节点配置json文件,红色区域需要修改,其他不变。

{
  "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>"
  }
}

我现在要添加一个系统版本为13.3的 iphone 11 Pro Max的模拟器节点:

{
  "capabilities":
      [
        {
          "browserName": "iphone 11 Pro Max", 
          "version":"13.3",
          "maxInstances": 1,
          "platform":"iOS"
        }
      ],
  "configuration":
  {
    "cleanUpCycle":2000,
    "timeout":30000,
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "url":"http://192.168.0.109:5723/wd/hub", //你本机IP加上一个未使用的端口,然后拼接/wd/hub
    "host": "192.168.0.109",//本机IP
    "port": 5723, //这里端口号跟上面url的端口保持一致
    "maxSession": 1,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444, //固定为4444
    "hubHost": "192.168.0.109" //本机IP
  }
}

保存文件到特定位置,命名为node_iphone11promax_emulator.json。

终端执行:

cd <node_iphone11promax_emulator.json路径下>

appium --nodeconfig node_iphone11promax_emulator.json -p 5723

看到如下log
nodeconfig
浏览器输入http://你的IP:4444/grid/console , 就可以看到有个新增节点。

grid console

注意: 如果是在本机上要注册多个节点,因为每个节点是不同的selenium实例,所以需要配置不同的端口,否则会提示端口错误。
配置Json里的port和url端口都配成与其他节点不同的端口,然后
appium --nodeconfig node_iphone11promax_emulator.json -p 5723 -p后面的端口也换成这次你json里修改后的端口。

如果在json配置文件里url、host和端口都没有给出,selenium grid会自动使用localhost:和appium启动的端口。
如果您的Appium server运行在与Selenium grid server不同的机器上,请确保在您的host 和url使用的是外部IP地址。localhost和127.0.0.1不同使用,会阻止Selenium grid正确连接。

使用Pytest+Appium server+ Selenium Grid跑用例

将之前的用例代码里的 webdriver.Remote("http://localhost:4723/wd/hub", caps) 改为webdriver.Remote("http://192.168.0.109:4444/wd/hub", caps) selenium grid的地址。

可以看到测试模拟器被启动了,用例跑起来了。

看appium log可以看出 是 appium 向 selenium grid发了session,然后selenium grid启动了相应节点,然后开始跑自动化测试。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值