[TOC] ## Selenium 分布式运行 Selenium Grid 可以在不同的主机上建立 **主节点(hub)** 和 **分支节点(node)** , 可以`使主节点上的测试用例在不同的分支节点上运行`。对于不同的节点来说,可以搭建不同的测试环境(操作系统,浏览器),从而使一份测试用例得到不同环境下的执行结果。 主节点与分支节点之间的交互功能,有Selenium Server提供。 ### 安装 Selenium Server >[info]Selenium Server 的作用就是为了运行远程 Selenium WebDriver Selenium Server 是个java 程序,推荐使用JRE 1.6以上的java环境运行。我们可以从[download page of selenium website](https://www.seleniumhq.org/download/) 中下载 Selenium Standalone Server,名字类似 selenium-server-standalone-3.13.0.jar 百度云下载地址:[https://pan.baidu.com/s/1k0KRAHv1pqwdZ-n6XBE0gg](https://pan.baidu.com/s/1k0KRAHv1pqwdZ-n6XBE0gg) ### Step 1: 启动主节点 (假设主节点主机IP为:10.1.102.68) 主节点(hub)是接收所有测试请求并将它们分配给正确分支节点(node)的中心点。 ```cmd java -jar selenium-server-standalone-3.13.0.jar -role hub ``` 执行命令后,主节点会默认使用 4444端口,我们可以通过主节点控制器[ http://localhost:4444/grid/console](http://localhost:4444/grid/console) 来检查主节点的状态。 ### Step 2: 启动远程分支节点 (假设远程分支节点IP为:10.1.102.75) 在远程机器中,执行以下命令: ```cmd java -jar selenium-server-standalone-3.13.0.jar -role node -hub http://10.1.102.68:4444/grid/register -port 5555 ``` 通过本命令,将会启动远程分支节点,并且注册到主节点中。通过刷新主节点控制器可以看到连接到hub的所有node信息,如下 ![](https://box.kancloud.cn/2243a9f151747c5ede79388294c424c5_727x335.jpg) ### 使用Grid运行用例 在项目中,调整创建driver实例的方法,在前面的项目中,修改`conftest.py` 中的**get_driver** 函数如下 ```python #!/usr/bin/python # coding=utf-8 from selenium import webdriver import pytest import allure from selenium.webdriver import Remote @pytest.fixture() def get_driver(): # 创建Chrome驱动实例,这里创建driver时,传入chrome_options参数,告诉服务器,我是用移动端浏览器访问的。 options = webdriver.ChromeOptions() options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30') # driver = webdriver.Chrome(chrome_options=options) ############## 通过Remote调用远程WebDriver ############## driver = Remote(command_executor="http://10.1.102.75:5555/wd/hub", desired_capabilities={ 'platform': 'ANY', 'browserName': 'chrome', 'version': '', 'javascriptEnabled': True }, options=options) # 设置浏览器大小,让它看起来跟手机的样式差不多。 driver.set_window_size("380", "680") # 设置一个全局的等待超时时间 10s driver.implicitly_wait(10) yield driver driver.quit() @pytest.fixture(scope="session", autouse=True) def env(): """ Parse env config info """ allure.environment(report="英语移动端官网", browser="Chrome", version="1.0.1") # 测试报告中展示环境信息 ``` 运行结果如: ![](https://box.kancloud.cn/3a203657e86ab28b204c13eaaea55a2c_1033x772.jpg) --- >[warning] 远程node时,要求: a.本地hub主机与远程node主机之间可以用ping命令连通 b.远程主机必须安装用例执行的浏览器及驱动,并且驱动要放在环境变量path的目录下 c.远程主机必须安装java环境,运行Selenium Server <hr style="margin-top:50px"> <section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 10px;box-sizing: border-box;"><section class="" style="display: inline-block;width: 100%;border-width: 5px;border-style: double;border-color: rgb(23, 22, 24);padding: 10px;border-radius: 2px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(20px, 0px, 0px);-webkit-transform: translate3d(20px, 0px, 0px);-moz-transform: translate3d(20px, 0px, 0px);-o-transform: translate3d(20px, 0px, 0px);font-size: 11px;margin: -50px 0% 0px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;width: 7em;height: 7em;display: inline-block;vertical-align: bottom;border-radius: 100%;border-width: 4px;border-style: double;border-color: rgb(23, 22, 24);background-position: center center;background-repeat: no-repeat;background-size: cover;background-image: url(&quot;http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n&quot;);"><section class="" style="width: 100%;height: 100%;overflow: hidden;box-sizing: border-box;"><img class="" data-ratio="0.6012024" data-w="499" data-src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" style="opacity: 0; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" data-fail="0"></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 30px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 0px;box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">微信公众号:</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;">python测试开发圈</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;"><br style="box-sizing: border-box;"></p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="text-align: center;margin: 10px 0% 0px;box-sizing: border-box;"><section class="" style="max-width: 100%;vertical-align: middle;display: inline-block;border-width: 0px;border-radius: 0px;box-shadow: rgb(0, 0, 0) 0px 0px 0px;width: 90%;overflow: hidden !important;box-sizing: border-box;"><img data-ratio="1" data-w="430" data-src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" style="vertical-align: middle; max-width: 100%; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" class="" src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" data-fail="0"></section></section></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 0px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(5px, 0px, 0px);-webkit-transform: translate3d(5px, 0px, 0px);-moz-transform: translate3d(5px, 0px, 0px);-o-transform: translate3d(5px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">一起分享学习与成长路线</p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(10px, 0px, 0px);-webkit-transform: translate3d(10px, 0px, 0px);-moz-transform: translate3d(10px, 0px, 0px);-o-transform: translate3d(10px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">长按(或扫描)二维码关注</p></section></section></section></section></section></section></section></section></section>