python+selenium grid配置与运行

作者: 望月成三人 | 来源:发表于2018-10-31 11:52 被阅读19次

环境准备

  • java安装,以及环境配置
  • python 3.4环境
  • selenium安装 pip install selenium
  • Chromedriver 要和本地的chrome浏览器匹配,点击下载
  • 下载selenium-server-standalone点击下载
  • 准备2台pc,如果是虚拟机,选择桥接,2台pc互相能ping通,本次测试为本机

开始部署

  • 启动hub
 java -jar selenium-server-standalone-v3.0.1.jar -role hub -port 4455
image.png
  • 启动node
java -jar selenium-server-standalone-v3.0.1.jar -role node -port 5555 -hub http://192.168.0.102:4444/grid/registe

java -jar selenium-server-standalone-v3.0.1.jar -role node -port 6666 -hub http://192.168.0.102:4444/grid/registe
image.png
  • 其他node启动方式
java -Dwebdriver.ie.driver=D:\IEDriverServer.exe -jar selenium-server-standalone-2.37.0.jar -role node -hub [http://127.0.0.1:4444/grid/register](http://127.0.0.1:4444/grid/register) -maxSession 20 -browser "browserName=internet explorer,version=9,platform=WINDOWS,maxInstances=20" -port 5555

node是只运行IE,并且并发数是20,最多有20个IE浏览器在运行

查看grid信息

浏览器打开:http://hub_ip:4444/grid/console可以查看hub信息,和已经连上的node信息

image.png

运行

  • 封装的unittest, BaseRunner .py
import unittest
from selenium import webdriver
import os

PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)


def get_driver(device):
    chromedriver = PATH("../exe/chromedriver.exe")
    os.environ["webdriver.chrome.driver"] = chromedriver
    chrome_capabilities = {
        "browserName": "chrome",  # 浏览器名称
        "version": "",  # 操作系统版本
        "platform": "ANY",  # 平台,这里可以是windows、linux、andriod等等
        "javascriptEnabled": True,  # 是否启用js
        "webdriver.chrome.driver": chromedriver
    }
    driver = webdriver.Remote(command_executor=device, desired_capabilities=chrome_capabilities)
    driver.maximize_window()  # 将浏览器最大化
    driver.get("https://www.baidu.com/")
    return driver


class ParametrizedTestCase(unittest.TestCase):
    def __init__(self, methodName='runTest', param=None):
        super(ParametrizedTestCase, self).__init__(methodName)
        global devicess
        devicess = param
    @classmethod
    def setUpClass(cls):
        pass
        cls.driver = get_driver(devicess)
        # cls.logTest = myLog().getLog("chrome")  # 每个设备实例化一个日志记录器

    def setUp(self):
        pass

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        pass

    def tearDown(self):
        pass

    @staticmethod
    def parametrize(testcase_klass, param=None):
        testloader = unittest.TestLoader()
        testnames = testloader.getTestCaseNames(testcase_klass)
        suite = unittest.TestSuite()
        for name in testnames:
            suite.addTest(testcase_klass(name, param=param))
        return suite
  • 编写测试用例
from Base.BaseRunner import ParametrizedTestCase
import os
import time

PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

class HomeTest(ParametrizedTestCase):
    def testSearch(self):
        time.sleep(1)
        self.driver.find_element_by_id("kw").send_keys("selenium")
        time.sleep(2)
        self.driver.find_element_by_id("su").click()
        time.sleep(2)

    @classmethod
    def setUpClass(cls):
        super(HomeTest, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(HomeTest, cls).tearDownClass()
  • 运行
import unittest
from multiprocessing.pool import Pool
from Base.BaseRunner import ParametrizedTestCase
from TestCase import HomeTest


def runner_pool():
    devices_Pool = ['http://192.168.0.102:5555/wd/hub', 'http://192.168.0.102:6666/wd/hub']
    pool = Pool(len(devices_Pool))
    pool.map(run, devices_Pool)
    pool.close()
    pool.join()

def run(device):
    suite = unittest.TestSuite()
    suite.addTest(ParametrizedTestCase.parametrize(HomeTest.HomeTest, param=device))
    unittest.TextTestRunner(verbosity=2).run(suite)


if __name__ == '__main__':
    runner_pool()


image.png

其他

相关文章

  • python+selenium grid配置与运行

    环境准备 java安装,以及环境配置 python 3.4环境 selenium安装 pip install se...

  • selenium grid是什么?

    一、Selenium Server 环境配置 1、selenium grid的组成与作用:由一个集线器hub和多...

  • Selenium |【案例篇】selenium爬取淘宝图片

    一、Python+Selenium安装及环境配置 Python环境安装 Window系统下,python的安装很简...

  • 2018-08-15

    Dart 开发与运行环境配置 本章内容安装与配置Dart SDK安装与配置VSCode验证 1 安装与配置Dart...

  • 成功运行grid studio

    史上最简单安装gridstudio教程 (在我的安装笔记前加上这个小白教程,是因为有朋友专门给我发消息说小白看不懂...

  • Selenium Grid简介

    什么是Selenium Grid Selenium Grid是Selenium套件的一部分,它专门用于并行运行多个...

  • Selenium Grid + Maven + TestNG +

    Selenium Grid 是什么? Selenium Grid 是一个可以方便的让你脚本运行在不同的平台以及不同...

  • 如何搭建Selenium-Grid?

    一、selenium-Grid的定义 Selenium-Grid允许您在不同的机器上并行地针对不同的浏览器运行测试...

  • vue集成ag-grid

    安装ag-grid插件和配置 第一步:进行下载对应的ag-grid插件npm install --save ag-...

  • AOTOIT3 - python上传图片

    第一步:配置搭建Python+selenium的Windows环境(在此不再细说,请看我的相关文档),安装pych...

网友评论

    本文标题:python+selenium grid配置与运行

    本文链接:https://www.haomeiwen.com/subject/glgotqtx.html