首页 > 其他分享 >基于Selenium Grid搭建自动化并行执行环境

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

时间:2022-09-05 09:33:56浏览次数:56  
标签:http hub chrome Selenium driver 并行执行 Grid 3000 浏览器

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

本文章出自【码同学软件测试】

码同学公众号:自动化软件测试,领取资料可加:magetest

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

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

 

 

出现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

 

 

出现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/"

 

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/”,关闭窗口。

 

 

免费领取码同学软件测试课程笔记+超多学习资料+学习完整视频 ☞ 可加:magetest/关注码同学公众号:自动化软件测试

本文著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

标签:http,hub,chrome,Selenium,driver,并行执行,Grid,3000,浏览器
From: https://www.cnblogs.com/testfan2019/p/16656947.html

相关文章

  • 关于selenium之 定位以及切换iframe跨域处理
    很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug也可以看到,就是定位不到,这种情况很有可能是frame在搞鬼(原因之一,改天专门说说定位不......
  • Selenium 教程第 3 部分
    Selenium教程第3部分我真的很抱歉放弃这部续集。实际上,我在写这篇文章的时候就失去了动力,想着谁会去读它,甚至会喜欢它。但是看到您的一些支持者喜欢这个故事或添加到......
  • selenium元素定位---ElementClickInterceptedException(元素点击交互异常)解决方法
    1、异常原因在编写ui自动化时,执行报错元素无法点击:ElementClickInterceptedException具体报错:selenium.common.exceptions.ElementClickInterceptedException:Message:......
  • 直播app开发搭建,存放多张图片的GridView,并可以点击放大
    直播app开发搭建,存放多张图片的GridView,并可以点击放大View <ScrollView    android:id="@+id/scorll"    android:layout_width="match_parent"  ......
  • 关于ag-grid-vue导出excel表格 进行内容valueFormatter
    最近在写ag-grid-vue的项目,用到了导出Excel功能,但是导出的数据是原始数据,不怎么理想,后来找了点方法进行处理,导出效果很好 <AgGrid...:defaultE......
  • 知道何时使用 flex 或 grid
    知道何时使用flex或grid当您第一次开始学习Web开发时,知道何时使用flex或grid可能会非常令人困惑。他们基本上做同样的事情,但学习这些差异可以帮助你的发展事业。......
  • 使用selenium自动化模块实现登录12306
    importtimefromselenium.webdriverimportChromefromselenium.webdriver.chrome.optionsimportOptionsfromselenium.webdriver.common.byimportByfromselenium.w......
  • dhtmlx中的grid加载不了数据
    我是一个初学者,求大神帮我看下这data为什么加载不了<scripttype="text/javascript">vardata={rows:[{"id":"1",data:["烟雾测试","金华尾气检测站","423cvhjj","1233676......
  • Selenium+Python微博爬虫实战
    读研后终日苦于写论文(zuoniuma),一年来也没时间更新微博,想想还是要在假期抽点时间,将一年所学记点笔记,或许日后还有些用处。惭愧的是这一年在快节奏的压迫下,很多知识没有......
  • vb.net DataGridViewCheckBoxColumn 自绘显示
     自定义自绘DataGridView扩展中发现CheckBox等控件显示错误,摸索了半天,总结一下片段:Case"DataGridViewCheckBoxCell"  NCell=NewDataGridViewCheckBoxC......