Selenium Grid操作使用指北

官网下载地址:https://www.selenium.dev/downloads/

本文中用的之前老版本,历史版本下载地址:https://selenium-release.storage.googleapis.com/index.html

参考官方文档:https://www.selenium.dev/documentation/en/grid/setting_up_your_own_grid/

一、实现串行多浏览器执行脚本
1、启动selenium-server-standalone
java -jar selenium-server-standalone-3.141.59.jar

selenium grid我下载到本地并上传到了国内的网盘,下载地址: https://url90.ctfile.com/d/38447490-57003002-45437f?p=2587 (访问密码: 2587)

2、脚本代码

 1 from selenium import webdriver
 2 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 3 import time
 4 
 5 lists = ["chrome","firefox","safari"]
 6 for i in lists:
 7     print(i)
 8     driver = webdriver.Remote(
 9         command_executor="http://127.0.0.1:4444/wd/hub",
10         desired_capabilities={'platform':'ANY',
11                               'browserName':i,
12                               'version':'',
13                               'javascriptEnabled':True
14                               }
15     )
16     driver.get("http://www.baidu.com")
17     driver.find_element_by_id("kw").send_keys("hello")
18     driver.find_element_by_id("su").click()
19     time.sleep(3)
20     driver.quit()

二、selenium实现串行多节点(分布式)执行脚本

1、启动多节点selenium-server-standalone
主节点启动(代码所在主机)
java -jar selenium-server-standalone-3.141.59.jar -role hub

分支节点1启动(默认是5555端口,如果实际场景可视为比作北京机房,localhost改为主节点IP地址即可去注册主节点)
java -jar selenium-server-standalone-3.141.59.jar -role node -port 5555 -hub http://localhost:4444

分支节点2启动(默认端口占用了需要修改下端口,如果实际场景可视为是上海机房,localhost改为主节点IP地址即可去注册主节点)
java -jar selenium-server-standalone-3.141.59.jar -role node -port 5556 -hub http://localhost:4444

通过浏览器查看启动状态:http://代码所在主机/grid/console

如下显示启动成功

配置文件代码:

1 def getconfig():
2     d = {"http://192.168.109.1:5555/wd/hub":"chrome",
3          "http://192.168.109.1:5556/wd/hub":"firefox"}
4     return d

实现代码:

 1 from selenium import webdriver
 2 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 3 import time
 4 import config
 5 
 6 for host,browser in config.getconfig().items():
 7     print(host)
 8     print(browser)
 9     driver = webdriver.Remote(
10         command_executor="http://127.0.0.1:4444/wd/hub",
11         desired_capabilities={'platform':'ANY',
12                               'browserName':browser,
13                               'vwesion':'',
14                               'javascriptEnabled':True
15                               }
16     )
17     driver.get("http://www.baidu.com")
18     driver.find_element_by_id("kw").send_keys("hello")
19     driver.find_element_by_id("su").click()
20     time.sleep(3)
21     driver.quit()    

三、appium实现串行多节点(分布式)执行脚本 

1、启动主节点:java -jar selenium-server-standalone-2.44.0.jar -role hub

2、appium的多节点需要一个json配置文件。

如下为启动安卓的配置文件内容及启动成功展示:

 1 {
 2   "capabilities": [
 3     {
 4       "deviceName": "android",
 5       "version": "7.0",
 6       "maxInstances": 3,
 7       "platformName": "ANDROID"
 8     }
 9   ],
10   "configuration": {
11     "cleanUpCycle": 2000,
12     "timeout": 30000,
13     "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
14     "url": "http://192.168.56.1:4723/wd/hub",
15     "host": "192.168.56.1",
16     "port": 4723,
17     "maxSession": 6,
18     "register": true,
19     "registerCycle": 5000,
20     "hubPort": 4444,
21     "hubHost": "192.168.56.1"
22   }
23 }

如下为启动iOS的配置文件内容及启动成功展示:

 1 {
 2   "capabilities": [
 3     {
 4       "deviceName": "ios",
 5       "version": "13.5",
 6       "maxInstances": 3,
 7       "platformName": "IOS"
 8     }
 9   ],
10   "configuration": {
11     "cleanUpCycle": 2000,
12     "timeout": 30000,
13     "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
14     "url": "http://192.168.56.1:5723/wd/hub",
15     "host": "192.168.56.1",
16     "port": 5723,
17     "maxSession": 6,
18     "register": true,
19     "registerCycle": 5000,
20     "hubPort": 4444,
21     "hubHost": "192.168.56.1"
22   }
23 }

 通过浏览器查看注册状态:http://代码所在主机/grid/console

如上表示注册成功!

运行代码自动寻找设备实现自动化:

 如上显示运行用例成功!!!

 

总结:

Selenium Grid方案:

1、支持 Android iOS 模拟器

2、支持 Web 浏览器

3、支持所有兼容 WebDriver 协议的框架

 

使用 grid 模式做自动化:

1、与普通的自动化用例没有差别

2、URL 修改为对应的 hub 的 URL 即可

1  # self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
2  self.driver = webdriver.Remote("http://192.168.56.1:4444/wd/hub", caps)

 

Selenium Grid并不能实现并行执行脚本,如果想并行执行需要和多线程进行结合。

posted @ 2017-08-13 11:23  韩凯1990  阅读(4752)  评论(0编辑  收藏  举报