每天进步一点点,关注我们哦,每天分享测试技术文章 本文章出自【码同学软件测试】 码同学公众号:自动化软件测试 码同学抖音号:小码哥聊软件测试
Selenium Grid组件专门用于远程分布式测试或并发测试,通过并发执行测试用例的方式可以提高测试用例的执行速度和效率,解决界面自动化测试执行速度过慢的问题
它允许 Selenium 测试脚本将命令通过hub路由到远程 Web 浏览器。它的目的是提供一种在多台机器上并行运行测试的简单方法。使用 Selenium Grid,一台服务器充当枢纽,将测试命令路由到一个或多个注册的 Grid 节点。hub有一个注册服务器列表,它提供访问权限,并允许控制这些node实例。Selenium Grid 允许我们在多台机器上并行运行测试,并集中管理不同的浏览器版本和浏览器配置(而不是在每个单独的测试中)。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/02a1728eaf8c49d0ba051cadc086163b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGZhbl96aG91,size_20,color_FFFFFF,t_70,g_se,x_16)
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 ![在这里插入图片描述](https://img-blog.csdnimg.cn/af8009d85ae540c49e5dd7a37b2b7b60.png)
出现Clients should connect to ****表示hub启动成功。
02 启动node
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.2.161:4445/wd/hub
![在这里插入图片描述](https://img-blog.csdnimg.cn/2634b45f508b4e5d948b874f6b7d2209.png)
出现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"?>
<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>
2个线程并行执行MyChrome和MyFirefox这两个测试类。
同时启动chrome和firefox浏览器,最大化窗口,访问网址:
“http://www.mtxshop.com:3000/”,关闭窗口。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/72ac2bdb58d54e95b0d4f583faeea200.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGZhbl96aG91,size_20,color_FFFFFF,t_70,g_se,x_16)
笔者跟大家分享一个福利!扫码回复【csdn码哥群】入软件测试自学交流群,可免费听技术讲座+领学习资料+视频课免费看 ![在这里插入图片描述](https://img-blog.csdnimg.cn/424f5e23cbe34456a5ee0c9ddf5e415b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAVGVzdGZhbl96aG91,size_20,color_FFFFFF,t_70,g_se,x_16)
作者:码同学软件测试 本文著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
|