Docker實踐--搭建分布式UI測試環境


  背景:項目需要在chrome firefox不同版本下做UI自動化測試

  現狀:單台機器只能安裝一個版本瀏覽器;多台電腦協同太麻煩;

  解決方案:通過Docker簡化Selenium Grid分布式測試的部署

 

  1.Grid介紹

  

  

  web端的自動化測試呈現一家獨大的狀態,大部分都在使用selenium完成,也是各大瀏覽器官方支持的工具,應用非常普遍。

  以傳統的方式部署分布式Selenium Grid集群需要耗費大量時間和機器成本來准備測試環境。比如為了針對不同版本的Chrome進行測試,需要將指定版本的Chrome瀏覽器安裝到不同物理機或虛擬機上。

  

  2.Docker部署

  2.1 Docker環境搭建

  本機使用windows7 64Bit 通過DockerToolbox安裝  宿主機基於linux

  安裝完成初始化比較慢、需要翻牆更快

  2.2  安裝images 

  Hub 安裝   chrome+firefox不同版本安裝

  baozhida/selenium-hub  https://hub.docker.com/r/baozhida/selenium-hub/

  baozhida/selenium-node-firefox-debug https://hub.docker.com/r/baozhida/selenium-node-firefox-debug/

  baozhida/selenium-node-chrome-debug https://hub.docker.com/r/baozhida/selenium-node-chrome-debug/ 

  suyunrong/selenium-node-chrome-debug https://hub.docker.com/r/suyunrong/selenium-node-chrome-debug/tags/

 

  安裝命令

  docker pull baozhida/selenium-hub:3.3.1

  docker pull baozhida/selenium-node-chrome-debug:48

  docker pull baozhida/selenium-node-chrome-debug:58

  docker pull baozhida/selenium-node-firefox-debug:47

  docker pull baozhida/selenium-node-firefox-debug:52

  docker pull suyunrong/selenium-node-chrome-debug:66.0.3359.170

 

  注1:debug版本可以通過VNC工具登錄看到遠程桌面

  注2:可以自由下載其它版本鏡像

 

  查看安裝成功 docker images

  REPOSITORY                                        TAG                    IMAGE ID            CREATED             SIZE

  suyunrong/selenium-node-chrome-debug  66.0.3359.170    7bccb076f06a    3 months ago 889MB
  baozhida/selenium-node-chrome-debug    58        36863bdc2bbf    15 months ago 923MB
  baozhida/selenium-node-firefox-debug   47          2792d74ddc5c    15 months ago 729MB
  baozhida/selenium-node-firefox-debug      52            6952015b6de6    15 months ago 736MB
  baozhida/selenium-hub           3.3.1       8bbca693c0c0       15 months ago 394MB

  REPOSITORY  代表鏡像倉庫名 

  IMAGE ID  代表鏡像ID

  TAG    代表版本號 latest--最新版本 一般不標准表示最新版本

  

  3.創建運行容器

  創建selenium hub容器
  docker run -d -p 4444:4444 --name selehub baozhida/selenium-hub:3.3.1


  創建chrome node容器
  docker run -d -p 5901:5900 --name gc48 --link selehub:hub --shm-size=512m baozhida/selenium-node-chrome-debug:48
  docker run -d -p 5902:5900 --name gc58 --link selehub:hub --shm-size=512m baozhida/selenium-node-chrome-debug:58
  docker run -d -p 5903:5900 --name gc66 --link selehub:hub --shm-size=512m suyunrong/selenium-node-chrome-debug:66.0.3359.170
  docker run -d -p 5904:5900 --name gc65 --link selehub:hub --shm-size=512m suyunrong/selenium-node-chrome-debug:65.0.3325.162

  創建firefox node容器
  docker run -d -p 5911:5900 --name ff47 --link selehub:hub --shm-size=512m baozhida/selenium-node-firefox-debug:47
  docker run -d -p 5912:5900 --name ff52 --link selehub:hub --shm-size=512m baozhida/selenium-node-firefox-debug:52

  -d 代表后台模式運行

  -p 代表端口映射 5901:5900 容器的5900端口映射到docker的5901端口 訪問docker 5901端口可訪問容器

  --name 代表 容器名字 可自定義 不設置系統自動分配

  --shm-size參數:docker默認的共享內存/dev/shm只有64m,有時導致chrome崩潰,該參數增加共享內存大小到512m.*

  --link 綁定到某個容器

  注:端口定義chrome nod [5901---] firefox nod[5911--]

  

  查看在運行容器

  docker ps

  

  NAMES 容器運行起來后名字

  PORTS 5912:5900 容器端口5900 docker端口5912

  CONTAINER ID 容器運行之后生成唯一的id 一個容器可以運行N次生成N個ID

 

  在瀏覽器輸入地址http://192.168.99.100:4444/grid/console
  查看Selenium Grid控制台,能看到剛創建的容器已經正常注冊。

  

  4.VNC遠程瀏覽器環境

  66為例

  輸入192.168.99.100:5903 回車 輸入密碼secret 

  5.Python初始化Driver 並發執行

  chrome_driver = os.path.abspath(r"D:\Python27")
  os.environ["webdriver.chrome.driver"] = chrome_driver
  chrome_capabilities = {
"browserName": "chrome",
"version": "58.0.3029.81",
"platform": "ANY",
"javascriptEnabled": True,
"webdriver.chrome.driver": chrome_driver
  }
  
  firfox_capabilities = {
"browserName": "firefox",
"version": "47.0",
"platform": "ANY",
"javascriptEnabled": True
  }
  dr = webdriver.Remote(command_executor ="http://192.168.99.100:4444/wd/hub", desired_capabilities = chrome_capabilities)


  --chrome_driver 指定驅動路徑chromedriver.exe
  --firefox driver 低版本不需要驅動
  --firefox 48版本以上需要驅動
 
  firfox_driver = os.path.abspath(r"D:\Python27")
  os.environ["webdriver.gecko.driver"] = firfox_driver
  firfox_capabilities = {
"browserName": "firefox",
"version": "61.0.1",
"platform": "ANY",
"javascriptEnabled": True,
"webdriver.gecko.driver": firfox_driver
  }
  使用線程實現並發同時調用 (后續可以借助單元測試框架實現報告)
  

 
        

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM