[【自动化测试不求人】docker搭建selenium server

Selenium Grid组件是Selenium一个非常重要的一个组件,它主要是用于远程分布式测试或者多浏览器并发测试。通常有如下两种情况下发生时会使用Selenium Grid。

  • 测试需要运行在多种浏览器(比如火狐、谷歌和IE等),多种版本的浏览器(如IE9IE11Chrome 70.0 等)和这些浏览器是运行在不同的操作系统上的(如WindowsLinux等)。
  • 需要并行测试来减少整体的项目周期。

docker现在比较流行,grid环境将采用docker搭建,有如下3个镜像需要下载,而这些都是官方已经存在的,其中hub是作为一个管理中枢的角色存在的。安装完之后用命令docker images查看镜像情况,如下3个镜像已下载好:

  • selenium/hub
  • selenium/node-firefox
  • selenium/node-chrome

 

 

 

启动selenium node

  • docker run -P -d --link hub:hub --name firefox selenimu/node-firefox
  • docker run -P -d --link hub:hub --name chrome selenimu/node-chrome

 

 

查看Selenium Grid Console界面

 

Docker容器的安装准备工作完成后,下一步需要在安装hub机器上查看Selenium Grid的配置情况。配置网址为http://localhost:5555/grid/console,界面显示如图所示,从图上可知,Grid的配置情况是和我们设置安装的一致,图上也有配置的浏览器node的相关细节,比如浏览器的具体版本等。通过命令“docker logs hub”也可以查看hub管理下有多少node节点,结果如图14.18所示。

 

 

 

 

 

Docker环境下执行多线程测试

 

#学习有疑问请联系作者
#作者qq:2574674466
#作者邮箱2574674466@qq.com
#coding=utf-8
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from thred_unit.basic_case import ParaCase
from thred_unit.basic_case import DetailCase
import  threading,unittest

#本例通过selenium server并发操作
#继承父类threading.Thread
class myThread (threading.Thread):   
    def __init__(self, device):
        threading.Thread.__init__(self)
        self.device=device

    def run(self):
        print ("Starting " + self.name)
        print ("Exiting " + self.name)
        run_suite(self.device)

def run_suite(device):
    suite = unittest.TestSuite()
    suite.addTest(ParaCase.parametrize(DetailCase, param=device))
    unittest.TextTestRunner(verbosity=1).run(suite)
if __name__ == '__main__':
url = 'http://127.0.0.1:4444/wd/hub'
browser = [DesiredCapabilities.CHROME,DesiredCapabilities.FIREFOX]
for i in range(len(browser)):
    th= myThread(webdriver.Remote(command_executor=url,desired_capabilities=browser[i]))
    th.start()
    th.join()

 

 

视频、学习笔记联系qq:2574674466
更多内容请关注公众号:“大牛测试

 

 

posted @ 2021-10-17 15:58  大牛测试技术  阅读(233)  评论(0编辑  收藏  举报