基于Selenium Grid搭建自动化并行执行环境

每天进步一点点,关注我们哦,每天分享测试技术文章

码同学抖音 :小码哥聊软件测试

Selenium Grid组件专门用于远程分布式测试或并发测试,通过并发执行测试用例的方式可以提高测试用例的执行速度和效率,解决界面自动化测试执行速度过慢的问题

它允许 Selenium 测试脚本将命令通过hub路由到远程 Web 浏览器。它的目的是提供一种在多台机器上并行运行测试的简单方法。使用 Selenium Grid,一台服务器充当枢纽,将测试命令路由到一个或多个注册的 Grid 节点。hub有一个注册服务器列表,它提供访问权限,并允许控制这些node实例。Selenium Grid 允许我们在多台机器上并行运行测试,并集中管理不同的浏览器版本和浏览器配置(而不是在每个单独的测试中)

selenium grids官 地址如下:

https://www.selenium.dev/documentation/en/grid/grid_3/

01 启动hub

java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4445

调试脚本

02 启动node

java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.2.161:4445/wd/hub

出现The node is registered to the hub,表示node已经在hub上完成了注册。

03 调试脚本

1、chrome

如下代码块,首先设置chrome配置信息“浏览器名称”、“操作系统名称”、“驱动文件路径”

RemoteWebDriver类对象初始化时使用hub地址以及chrome配置信息对象,死等待3秒后将chrome浏览器窗口最大化,然后访问 址:”http://www.mtxshop.com:3000/”,最后关闭浏览器窗口。

ChromeOptions chromeOptions = new ChromeOptions();chromeOptions.setCapability("browserName","chrome");chromeOptions.setCapability("platform","WINDOWS");chromeOptions.setCapability("webdriver.chrome.driver","D:\BrowserDriver\chromedriver.exe");String url = "http://192.168.2.161:4445/wd/hub";try {WebDriver driver = new RemoteWebDriver(new URL(url), chromeOptions);FindElementDemo.think(3000);driver.manage().window().maximize();driver.get("http://www.mtxshop.com:3000/");driver.close();} catch (MalformedURLException e) {    e.printStackTrace();}

执行后能够成功访问“http://www.mtxshop.com:3000/”

免费领取 码同学软件测试 课程笔记+超多学习资料+完整视频+最新面试题,可以转发文章 + 私信「码同学666」获取资料哦

2、firefox

如下代码块,首先设置firefox配置信息“浏览器名称”、“操作系统名称”、“驱动文件路径”

RemoteWebDriver类对象初始化时使用hub地址以及chrome配置信息对象,死等待3秒后将chrome浏览器窗口最大化,然后访问 址:”http://www.mtxshop.com:3000/”,最后关闭浏览器窗口

FirefoxOptions firefoxOptions = new FirefoxOptions();firefoxOptions.setCapability("browserName","firefox");firefoxOptions.setCapability("platform","WINDOWS");firefoxOptions.setCapability("webdriver.firefox.driver","D:\BrowserDriver\geckodriver.exe");String url = "http://192.168.2.161:4445/wd/hub";try {WebDriver driver = new RemoteWebDriver(new URL(url), firefoxOptions);FindElementDemo.think(3000);driver.manage().window().maximize();driver.get("http://www.mtxshop.com:3000/");driver.quit();} catch (MalformedURLException e) {    e.printStackTrace();}

执行后能够成功访问”http://www.mtxshop.com:3000/”。

04 分布式并发测试

testng+selenium grid3分布式并发测试。

testng配置文件配置如下所示:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="All Test Suite" parallel="classes" thread-count="2">    <test verbose="2" preserve-order="true" name="C:/Users/18611/IdeaProjects/UIautotest20210331/UIautotest20210331">        <classes>            <class name="com.mtx.study1.MyChrome"></class>            <class name="com.mtx.study1.MyFirefox"></class>        </classes>    </test></suite>

2个线程并行执行MyChrome和MyFirefox这两个测试类。

同时启动chrome和firefox浏览器,最大化窗口,访问 址:

“http://www.mtxshop.com:3000/”,关闭窗口。

tp://www.mtxshop.com:3000/”,关闭窗口。

END

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年2月10日
下一篇 2022年2月10日

相关推荐