芭比哇玩123 发表于 2017-5-27 14:34:36

快速搞定 selenium grid 分布式测试

以传统的方式部署分布式Selenium Grid集群需要耗费大量时间和机器成本来准备测试环境。
Snake用docker来搭建Selenium grid环境,用ptest框架来写代码,尝试简化这个过程。

http://upload-images.jianshu.io/upload_images/689830-2b9f1dcd902a2034.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240


NO.1 搭环境首先你得把docker加个阿里云加速器,不然那个等待是漫长的。
写个docker-compose.yml
hub:image: selenium/hubports:    - "4444:4444"firefox:image: selenium/node-firefoxports:    - 5901:5900links:    - hubchrome:image: selenium/node-chromeports:    - 5902:5900links:    - hub启动起来:
docker-compose up看起来是这样的

http://upload-images.jianshu.io/upload_images/689830-6d9a4773ce8ee9e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240


启动成功。
环境部分,就大功告成了。So easy!
NO.2 写代码写了一个很简单的例子,用了同事写的ptest框架。
这个框架改进了很多市面上框架用起来不爽的地方,比如:报告,并发,写log, 传参等。
__author__ = 'anderson'

# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait

from ptest.decorator import TestClass, Test, BeforeMethod, AfterMethod

@TestClass(run_mode="parallel")# the test cases in this class will be executed by multiple threads
class SeleniumClass:
    @BeforeMethod(description="Prepare test Enviroment.")
    def before(self):
      preporter.info("set up driver")
      browser = config.get_property("browser")
      if browser == "firefox":
            self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
                                           desired_capabilities=DesiredCapabilities.FIREFOX)
      elif browser == "chrome":
            self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
                                           desired_capabilities=DesiredCapabilities.CHROME)

    @Test(tags=["regression", "smoke"])
    def test1(self):
      preporter.info("start to test")
      self.driver.get("http://www.baidu.com")
      inputElement = self.driver.find_element_by_name("wd")
      inputElement.send_keys("docker")
      inputElement.submit()
      WebDriverWait(self.driver, 20).until(lambda driver: driver.title.startswith("docker"))
      assert_true(self.driver.title.startswith("docker"))

    @AfterMethod(always_run=True, description="Clean up")
    def after(self):
      preporter.info("cleaning up")
      self.driver.quit()
运行,得到结果:

http://upload-images.jianshu.io/upload_images/689830-21f91f80e964992f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240


Oopos, 出错了!
http://upload-images.jianshu.io/upload_images/689830-98355a5ec4d71703.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240


仔细看log和截图,原来是没有支持中文。
解决方案是: 换docker image 源,搜到了支持中文的。
重启后再运行。

http://upload-images.jianshu.io/upload_images/689830-892fb30b618ff2c6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240


Nice!
http://upload-images.jianshu.io/upload_images/689830-f8cc6b92e8e3709f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240


NO.3 并发其实第二步里面已经打开了并发。运行的时候,输入并发数就可以了。
ptest3 -t test -n 2 -Dbrowser=firefox
这里的n是并发数,参数是firefox. 让firefox做为浏览器。(可以根据case 数量来设定)
当然,也可以在yml里面多注册几个hub, up to you!
感想其实在这里,我都没做啥,只是做了个搬运工而已,报告,截图,并发,重跑等等,我都不用操心,只要设置几个参数就行。好的框架,能事半功倍。

小皮球的故事 发表于 2017-5-27 15:07:29

赞!很全面!:victory:

梦想家 发表于 2017-5-27 15:08:15

:)

芭比哇玩123 发表于 2017-5-27 15:08:39

马走不谢!:lol

芭比哇玩123 发表于 2017-5-27 15:09:39

梦想家 发表于 2017-5-27 15:08


:lol
页: [1]
查看完整版本: 快速搞定 selenium grid 分布式测试