背景
公司內部的Selenium Grid服務基本都是我幫忙搭建在Win服務機器上,使用我的或者admin賬號運行了hub和node節點,那麼當別的團隊使用Selenium Grid時,如果想查看腳本現在執行頁面情況,經常需要找我遠程鏈接幫忙查看具體頁面情況,那個煩!!再加上公司安全性管控較嚴個人賬號也是絕對不允許同事間分享,全公司的Chrome都是IT推送升級,這樣我就得經常去更新node節點上ChromeDriver的版本,那個煩煩!
更坑的是,我們所用的電腦都是木有admin權限,而且Chrome很多功能都加入了admin管理,例如安裝本地插件等都需要admin權限才能安裝。 而且大家都知道,ChromDriver在2.28(如果我沒記錯的話)版本後,啓動Chrome都會本地加載一個Automation extensions,那麼這時如果你執行腳本啓動瀏覽器一定會報一個權限錯誤,儘管我們申請了win服務機器的admin權限,也申請了IT把這些機器排除在管控範圍外,然而。。。呵呵噠,總之煩煩煩!
所以我就默默決定找個時間,找個替代方案,一來解決腳本執行時,可以非常便捷而且不限制用戶的查看服務器瀏覽器情況,二來避免IT管控引起的各種問題。
方案確定
經過幾天折騰和比較,最後決定搭建兩套開源方案,供不同項目組不同需求使用。
方案1:Selenoid
方案2:Zalenium
下面簡單演示兩套方案的搭建和基本使用,並最後列出和Selenium Grid的比較
Selenoid
Selenoid 簡介
Selenoid是一個使用Go語言來實現的Selenium Gird。它通過Docker來啓動瀏覽器。
GitHub: https://github.com/aerokube/selenoid
官方幫助文檔: http://aerokube.com/selenoid/latest/#_getting_started
Selenoid 的搭建
Docker 安裝
簡介中知道是通過Docker來啓動瀏覽器的,所以如果機器上沒有docker,需要先安裝docker,安裝步驟和簡單使用詳細見:
https://www.jianshu.com/p/4bc3f8ece55d
https://docs.docker.com/engine/installation/
Selenoid 搭建過程我們需要用到Selenoid 和Selenoid UI,所以我們需要下載這兩個的images。
說明:
Selenoid UI 是Selenoid 的一個簡單頁面,通過頁面可以查看到時時狀態。
GitHub: https://github.com/aerokube/selenoid-ui
官方幫助文檔:http://aerokube.com/selenoid-ui/latest/
docker pull 相關鏡像:
$ docker pull aerokube/selenoid-ui
$ docker pull aerokube/selenoid
$ docker pull selenoid/video-recorder
$ docker pull selenoid/vnc:firefox_58.0
其中selenoid/video-recorder 用於錄屏,selenoid/vnc:firefox_58.0爲有VNC,火狐58版本的鏡像,如果想獲取更多支持的瀏覽器:
http://aerokube.com/selenoid/latest/#_browser_image_information
編寫browsers.json 文件:
如下新建個browsers.json文件,配上你已經下載的瀏覽器版本的鏡像,並放於宿主機的 /data/selenoid/config/ 路徑下。
{
"chrome": {
"default": "64.0",
"versions": {
"64.0": {
"image": "selenoid/vnc:chrome_64.0",
"port": "4444",
"path": "/"
}
}
},
"firefox": {
"default": "59.0",
"versions": {
"59.0": {
"image": "selenoid/vnc:firefox_59.0",
"port": "4444",
"path": "/wd/hub"
}
}
},
"phantomjs":{
"default": "2.1.1",
"versions": {
"2.1.1": {
"image": "selenoid/phantomjs:2.1.1",
"port": "4444",
"path": "/"
}
}
}
}
啓動:selenoid
執行如下命令:
docker run -d --name selenoid \
-p 14444:4444 \
-v /data/selenoid/:/etc/selenoid/ \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /data/selenoid/video/:/data/selenoid/video/ \
-e OVERRIDE_VIDEO_OUTPUT_DIR=/data/selenoid/video/ \
--privileged aerokube/selenoid:latest \
-conf /etc/selenoid/config/browsers.json -limit 10 -video-output-dir /data/selenoid/video/
selenoid命令支持以下配置:
-capture-driver-logs
是否向Selenoid輸出添加驅動程序日誌
-conf String
瀏覽器配置文件(默認爲“config / browsers.json”)
-container -network String
用於容器的網絡(默認爲“default”)
-cpu value
容器cpu限制爲float,例如0.2或1.0
-disable-docker
禁用docker 支持
-disable-privileged
是否禁用特權容器模式
-disable-queue
禁用等待隊列
-enable-file-upload
文件上傳支持
-limit int
同時容器運行(默認5)
-listen string
網絡地址接受連接(默認爲“4444”)
-log-conf string
字符串容器日誌記錄配置文件(默認爲“config / container-logs.json”)-
-mem value
容器內存限制,例如128m或1g
-retry-count int
新會話嘗試重試計數(默認值爲1)
-service-startup-timeout duration
服務啓動超時時間。持續格式(默認爲30秒)
-session-attempt-timeout持續時間
新會話嘗試超時time.Duration格式(默認30秒)
-session-delete-timeout duration
會話刪除超時時間。持續格式(默認30秒)
-timeout duration
持續時間會話空閒超時時間。持續格式(默認1m0s)
-version
顯示版本並退出
-video-output-dir String
將錄製視頻保存到(默認“video”)的目錄
-video-recorder-image string
用作錄像機的images(默認爲“selenoid / video-recorder”)
啓動:selenoid-ui:
docker run -d --name selenoid-ui -p 8080:8080 aerokube/selenoid-ui --selenoid-uri http://${SELENOID_HOST}:4444
編寫測試腳本
public void test6666() throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
// 設置要啓動的瀏覽器
capabilities.setBrowserName("chrome");
// 設置要啓動的瀏覽器版本
capabilities.setVersion("64.0");
// 設置是否實時查看 默認false
capabilities.setCapability("enableVNC", true);
// 設置是否錄製視頻 默認false
capabilities.setCapability("enableVideo", true);
// 設置錄製視頻名稱,可選,默認值爲session ID
capabilities.setCapability("videoName", "name");
// 設置測試名稱 可選
capabilities.setCapability("name", "BOTeam");
// 設置時區 可選
capabilities.setCapability("timeZone", "Asia/Shanghai");
// 設置標籤名,用於標識 可選
capabilities.setCapability("labels", "botest");
RemoteWebDriver driver = new RemoteWebDriver(
URI.create("http://localhost:14444/wd/hub").toURL(),
capabilities
);
driver.manage().window().setSize(new Dimension(1980, 1080));
driver.get("http://www.google.com");
System.out.println(driver.getTitle());
Thread.sleep(20000);
driver.getWindowHandle();
driver.quit();
}
實時查看
打開selenoid-ui 界面:http://localhost:8080。
1.gif
查看錄製的視頻
打開:http://localhost:14444/video/,選擇視頻並查看
image.png
查看使用統計
添加瀏覽器
如果當前瀏覽器已經無法滿足需求,可以手動需要添加瀏覽器,添加步驟如下:
- docker pull 需要的瀏覽器版本
- 修改browsers.json文件
- 重啓 selenoid
Zalenium
Zalenium 簡介
Zalenium 是一個Selenium Grid的擴展,同樣帶有視頻錄製,實時預覽,基本認證和儀表盤等功能。也是使用docker-selenium在本地運行Firefox和Chrome中的測試,如果需要不同的瀏覽器,可以把測試重定向到雲測提供商(Sauce Labs,BrowserStack,TestingBot)。 Zalenium也可以在Kubernetes中使用。
github:https://github.com/zalando/zalenium
官網:https://zalando.github.io/zalenium/
Zalenium 安裝
Zalenium 安裝前也得先安裝Dcoker引擎,而且官方給了版本限制必須Docker Version >=1.11.1。
docker pull 相關鏡像:
$ docker pull elgalu/selenium
$ docker pull dosel/zalenium
啓動Zalenium
Docker 執行如下面命令:
docker run -ti --name zalenium -p 4444:4444 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /tmp/videos:/home/seluser/videos \
--privileged dosel/zalenium start --timeZone "Asia/Shanghai"
--timeZone "Asia/Shanghai" : 表示設置啓動的容器的時區爲上海時間,更多時區查看:https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
/tmp/videos :爲視頻保存的宿主機路徑
更多配置參數
名稱 | 默認值 | 描述 |
---|---|---|
--desiredContainers |
2 | 啓動時創建的node/容器的數量。 |
--maxDockerSeleniumContainers |
10 | Docker-selenium 同時運行的容器的最大數量。 |
--sauceLabsEnabled |
false | 是否啓動Sauce Labs節點。 |
--browserStackEnabled |
false | 是否啓動BrowserStack節點。 |
--testingbotEnabled |
false | 是否啓動TestingBot節點。 |
--startTunnel |
false | 當啓用雲測試平臺時,啓動隧道以允許進行本地測試。請參閱每個提供商的文檔以瞭解使用情況和所需的Selenium功能。[Sauce Labs][BrowserStack][TestingBot]創建隧道時使用的本地標識符是zalenium 。 |
--videoRecordingEnabled |
true | 設置是否在每次測試中錄製視頻。 |
--screenWidth |
1920 | 設置屏幕寬度。 |
--screenHeight |
1080 | 設置屏幕高度。 |
--timeZone |
“Europe/Berlin" | 設置容器中的時區。更多https://en.wikipedia.org/wiki/List_of_tz_database_time_zones |
--debugEnabled |
假 | 啓用LogLevel.FINE。 |
--seleniumImageName |
“elgalu /selenium” | 啓用覆蓋要使用的Docker-selenium 鏡像。 |
--gridUser |
- | 允許指定用戶啓用基本身份驗證保護。--gridPassword 也必須提供。 |
--gridPassword |
- | 允許指定密碼以啓用基本身份驗證保護。--gridUser 也必須提供。 |
--maxTestSessions |
1 | 每個容器執行的最大測試數量。 |
--keepOnlyFailedTests |
false | 只保存失敗測試的視頻(您需要發送包含測試結果的cookie)。 |
編寫測試腳本
@Test
public void test1() throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
String className = this.getClass().getName();
String an = className + Thread.currentThread().getStackTrace()[1].getMethodName();
// 設置錄製視頻name (可選)
capabilities.setCapability("name", "testName");
// 設置構建名 (可選)
capabilities.setCapability("build", "BOTeam");
// 啓動容器的時區 (可選)
capabilities.setCapability("timeZone", "Asia/Shanghai");
RemoteWebDriver driver = new RemoteWebDriver(
URI.create("http://localhost:4444/wd/hub").toURL(),
capabilities
);
driver.manage().window().maximize();
driver.get("http://www.google.com");
System.out.println(driver.getTitle());
driver.getWindowHandle();
Thread.sleep(20000);
driver.quit();
}
實時查看
打開http://localhost:4444/grid/admin/live,我們可以看到如下,可以通過VNC查看到瀏覽器執行腳本情況。
查看錄製的視頻和log日誌
打開:http://localhost:4444/dashboard/#
image.png
image.png
簡單對比
對比項 | Selenium Grid | Selenoid | Zalenium |
---|---|---|---|
實時預覽 | N | Y | Y |
錄屏 | N | Y | Y |
多版本瀏覽器 | Y | Y | N (取決於Zalenium的版本)當然可以搭建多個版本的zalenium |
多瀏覽器 | Y | Y | N (除Chrome和Firefox外,其餘需要用雲服務) |
log回溯 | Y(但及其不方便) | N | Y |
搭建難度 | 簡單 | 複雜 | 簡單 |
資源自動回收 | N | Y | Y |
鏈接:https://www.jianshu.com/p/29c7240e9f48
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。