什么是zalenium
zalenium是一个Selenium Grid扩展,用Docker容器动态扩展你的本地网格。它使用docker-selenium在本地运行Firefox和Chrome中的测试。官网地址 zalenium
Openshift上部署zalenium
创建zalenium项目
1
| oc new-project zalenium --display-name="自动测试Selenium Grid"
|
创建Service Account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| # 创建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" ] } ] }
|
1 2 3 4
| 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
1 2 3 4 5 6 7
| 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #创建相应的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
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 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
Python具体实现自动测试
安装selenium模块
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| # -*- 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