启动所需浏览器的容器
docker run -d -p 4444:4444 -p 7900:7900 --shm-size=“2g” selenium/standalone-chrome:4.1.2-20220131
docker run -d -p 4444:4444 -p 7901:7900 --shm-size=“2g” selenium/standalone-edge:4.1.2-20220131
docker run -d -p 4444:4444 -p 7902:7900 --shm-size=“2g” selenium/standalone-firefox:4.1.2-20220131
浏览器查看容器内部
http://localhost:7900/ 连接到Chrome 容器
http://localhost:7901/ 连接到Edge 容器
http://localhost:7902/ 连接到Firefox 容器
2.1 node 跟 hub 在一台Linux上
2.1.1单独部署
1、创建docker网络:
docker network create grid
2、创建hub集线器:
docker run -d -p 4442-4444:4442-4444 --net grid --name selenium-hub selenium/hub:4.1.2-20220131
3、创建node节点1:
docker run -d --net grid -e SE_EVENT_BUS_HOST =selenium-hub --shm-size=2g -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-chrome:4.1.2-20220131
4、创建node节点2:
docker run -d --net grid -e SE_EVENT_BUS_HOST =selenium-hub --shm-size=2g -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-edge:4.1.2-20220131
5、创建node节点3:
docker run -d --net grid -e SE_EVENT_BUS_HOST =selenium-hub --shm-size=2g -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 selenium/node-firefox:4.1.2-20220131
6、使用完成后,删除容器:docker rm 容器id,删除网格:docker network rm grid
2.2.2 docker-compose 集成部署
创建 docker-compose-seleniumgrid.yml 文件
version: "3"
services:
chrome:
image: selenium/node-chrome:4.1.2-20220208
shm_size: 2gb
depends_on:
- selenium-hub
ports:
- "7901:7900"
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_SESSION_TIMEOUT=350
- SE_NODE_MAX_SESSIONS=10
edge:
image: selenium/node-edge:4.1.2-20220208
shm_size: 2gb
depends_on:
- selenium-hub
ports:
- "7902:7900"
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_SESSION_TIMEOUT=350
- SE_NODE_MAX_SESSIONS=5
firefox:
image: selenium/node-firefox:4.1.2-20220208
shm_size: 2gb
depends_on:
- selenium-hub
ports:
- "7903:7900"
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_SESSION_TIMEOUT=350
- SE_NODE_MAX_SESSIONS=8
selenium-hub:
image: selenium/hub:4.1.2-20220208
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
启动容器:
docker-compose -f docker-compose-seleniumgrid.yml up -d
-f:指定yml文件
up:启动容器
-d:后台启动
运行
pytest test_gride_pytest.py --server=192.168.0.104 --port=4444 --locale=zh_cn -n=3
–server=ip --port=4444:部署 selenium grid 的ip
–locale=zh_cn:启动的浏览器默认是中文语言
-n = 3:分布式运行用例
停止容器:
docker-compose -f docker-compose-v3.yml down
访问上面三个容器:
谷歌:http://IP:7901
edge:http://IP:7902
火狐:http://IP:7903
2.2 node 在其他Linux上
虚拟机1启动hub集线器:
docker run -d -p 4442-4444:4442-4444 --name selenium-hub selenium/hub:4.1.2-20220131
虚拟机2启动node节点:
docker run -d -p 5555:5555 --shm-size=2g -e SE_EVENT_BUS_HOST=机器1IP -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -e SE_NODE_HOST=机器2IP selenium/node-chrome:4.1.2-20220131
虚拟机3启动node节点:
docker run -d -p 5555:5555 --shm-size=2g -e SE_EVENT_BUS_HOST=机器1IP -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -e SE_NODE_HOST =机器3IP selenium/node-edge:4.1.2-20220131
虚拟机4启动node节点:
docker run -d -p 5555:5555 --shm-size=2g -e SE_EVENT_BUS_HOST=机器1IP -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -e SE_NODE_HOST =机器4IP selenium/node-firefox:4.1.2-20220131
文件test_gride_pytest.py
#!/usr/bin/env python
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from util import drivers
import pytest,time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from seleniumbase import BaseCase
class TestSelenium(BaseCase):
def setUp(self):
d = drivers()
self.driver=d.driver
def test_baidu(self):
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 self.driver.title.startswith("docker") == True
def test_3testing(self):
self.driver.get("http://www.3testing.com")
self.driver.switch_to.default_content()
self.driver.switch_to.frame("head")
self.driver.find_element(By.ID,"introduce").click()
self.driver.switch_to.default_content()
assert self.driver.title=="啄木鸟软件测试咨询网-顾翔介绍"
def test_CheckJingdong(self):
self.driver.get("http://www.jd.com")
self.driver.find_element(By.ID,"key").clear()
self.driver.find_element(By.ID,"key").send_keys('巧克力')
self.driver.find_element(By.CLASS_NAME,"button").click()
current_windows = self.driver.current_window_handle
time.sleep(5)
self.driver.find_element(By.XPATH,"//*[@id='J_goodsList']/ul/li[1]/div/div[1]/a/img").click()
all_handles = self.driver.window_handles
for handle in all_handles:
if handle != current_windows:
self.driver.switch_to.window(handle)
break
for handle in all_handles:
if handle == current_windows:
self.driver.switch_to.window(handle)
self.driver.close()
break
for handle in all_handles:
if handle != current_windows:
self.driver.switch_to.window(handle)
break
element = WebDriverWait(self.driver,5,0.5).until(EC.presence_of_element_located((By.ID,"key")))
assert "巧克力" in self.driver.title
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
pytest.main(["-sv"," test_gride_pytest.py"])
这里使用seleniumbase架构,控制并发。
最后: 下方这份完整的软件测试视频学习教程已经整理上传完成,朋友们如果需要可以自行免费领取【保证100%免费】
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!
软件测试技术交流群社:786229024(里面还有工作内推机会,毕竟我们是关系社会。)
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。