python+selenium grid配置与运行

环境准备

  • 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
python+selenium grid配置与运行_第1张图片
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
python+selenium grid配置与运行_第2张图片
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信息

python+selenium grid配置与运行_第3张图片
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()


python+selenium grid配置与运行_第4张图片
image.png

其他

  • 点击源码下载

  • 其他参考

你可能感兴趣的:(python+selenium grid配置与运行)