Openshift部署zalenium(容器化的selenium

作者: 潘晓华Michael | 来源:发表于2018-06-20 17:48 被阅读415次

    什么是zalenium

    zalenium是一个Selenium Grid扩展,用Docker容器动态扩展你的本地网格。它使用docker-selenium在本地运行Firefox和Chrome中的测试。官网地址 zalenium

    Openshift上部署zalenium

    创建zalenium项目

    oc new-project zalenium --display-name="自动测试Selenium Grid"
    

    创建Service Account

    # 创建ClusterRole
    # oc create -f zalenium-role.json
    {
        "kind": "ClusterRole",
        "apiVersion": "v1",
        "metadata": {
            "name": "zalenium-role"
        },
        "rules": [
            {
                "verbs": [
                    "create",
                    "list",
                    "get",
                    "delete",
                    "exec"
                ],
                "attributeRestrictions": null,
                "apiGroups": [
                    ""
                ],
                "resources": [
                    "pods"
                ]
            },
            {
                "verbs": [
                    "list",
                    "create",
                    "delete",
                    "get"
                ],
                "attributeRestrictions": null,
                "apiGroups": [
                    ""
                ],
                "resources": [
                    "services"
                ]
            },
            {
                "verbs": [
                    "create",
                    "get"
                ],
                "attributeRestrictions": null,
                "apiGroups": [
                    ""
                ],
                "resources": [
                    "pods/exec"
                ]
            }
         ]
    }
    
    oc create -f zalenium-role.json
    oc  create sa zalenium
    oc adm policy add-scc-to-user anyuid -z zalenium
    oc adm policy add-role-to-user zalenium-role -z zalenium
    

    部署zalenium

    oc run zalenium --image=dosel/zalenium \
            --env="ZALENIUM_KUBERNETES_CPU_REQUEST=250m" \
            --env="ZALENIUM_KUBERNETES_CPU_LIMIT=500m" \
            --env="ZALENIUM_KUBERNETES_MEMORY_REQUEST=1Gi" \
            --overrides='{"spec": {"template": {"spec": {"serviceAccount": "zalenium"}}}}' \
            -l app=zalenium,role=hub --port=4444 -- \
            start --desiredContainers 2 --seleniumImageName elgalu/selenium:latest
    

    创建Service

    #创建相应的service
    # oc create -f zalenium-service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      creationTimestamp: null
      labels:
        app: zalenium
      name: zalenium
    spec:
      ports:
      - name: 4444-tcp
        port: 4444
        protocol: TCP
        targetPort: 4444
      - name: 4445-tcp
        port: 4445
        protocol: TCP
        targetPort: 4445
      selector:
        app: zalenium
        role: hub
      sessionAffinity: None
      type: ClusterIP
    status:
    loadBalancer: {}
    

    创建Router

    # oc create -f ./zalenium-route.yaml
    apiVersion: v1
    kind: Route
    metadata:
      name: zalenium
      annotations:
        haproxy.router.openshift.io/timeout: 60s
    spec:
      host: zalenium.example.com
      port:
        targetPort: 4444-tcp
      to:
        kind: Service
    name: zalenium
    

    访问被管理selenium状态

    http://zalenium-zalenium.demo.example.com/grid/admin/live

    zalenium演示.PNG

    Python具体实现自动测试

    安装selenium模块

    pip install selenium
    

    代码

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    from selenium.webdriver.remote.remote_connection import RemoteConnection
    # 创建远程连接selenium grid
    remoteconnection = RemoteConnection('http://zalenium-zalenium.demo.example.com/wd/hub', keep_alive=False,
                                        resolve_ip=False)
    
    driver = webdriver.Remote(command_executor=remoteconnection,
                              desired_capabilities={
                                  'browserName': "chrome",
                                  'video': 'True',
                                  'platform': 'LINUX',
                                  'platformName': 'LINUX'
                              })
    try:
        driver.implicitly_wait(30)
        driver.maximize_window()
        driver.get("http://www.baidu.com")
        assert u'百度一下,你就知道' in driver.title
        kw_input = driver.find_element_by_id('kw')
        su_button = driver.find_element_by_id('su')
        kw_input.clear()
       # 输入关键字Openshift
        kw_input.send_keys('Openshift')
        su_button.click()
    
    finally:
        driver.quit()
    

    说明
    创建RemoteConnection时需要设置keep_alive=False及设置resolve_ip=False。
    resolve_ip默认为True,它会解析senelium grip服务器的ip,便通过Ip去访问。我们知道Openshift的应用是无法使用ip来访问的,必须使用域名。所以需要将resolve_ip设置为False。当然,如果我们部署zalenium中的service使用nodeport将4444端口暴露出去的resolve_ip可以使用默认值。

    查看自动测试结果

    http://zalenium-zalenium.demo.example.com/dashboard

    zalenium演示结果.PNG

    相关文章

      网友评论

        本文标题:Openshift部署zalenium(容器化的selenium

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