[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信息,如下

### 使用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") # 測試報告中展示環境信息
```
運行結果如:

---
>[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("http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n");"><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>