Selenium Grid分布式测试实践

Selenium Grid介绍

Selenium Grid允许你在多台机器的多个浏览器上并行的进行测试,即分布式测试。
通常,以下两种情况会需要使用Selenium Grid:
1)通常多个浏览器的兼容性测试,即在不同浏览器或不同操作系统的浏览器中进行测试
2)测试用例较多时,可以通过分布式测试减少测试执行时间
image.png

思路

1)节点测试机(node1)需要部署的软件包括:
JDK+selenium-server-standalone-3.141.59.jar+谷歌浏览器+谷歌浏览器驱动
2)节点测试机(node2)需要部署的软件包括:
JDK+selenium-server-standalone-3.141.59.jar+火狐浏览器+火狐浏览器驱动
3)节点测试机(node3)需要部署的软件包括:
JDK+selenium-server-standalone-3.141.59.jar+IE浏览器+IE浏览器驱动
4)主控测试机(hub)需要部署的软件:
JDK+selenium-server-standalone-3.141.59.jar
5)本地计算机(local)需要部署的软件:
JDK+eclipse+maven+selenium-java+testng
6)在hub机的命令行窗口里面启动hub(不要关闭命令行窗口,如要停止,使用快捷键Ctrl+c)

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

7)在node1机的命令行窗口里面启动node(不要关闭命令行窗口,如要停止,使用快捷键Ctrl+c)

java -Dwebdriver.chrome.driver="D:\\drivers\\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -port 5555 -hub http://192.168.0.124:4444/grid/register

8)在node2机的命令行窗口里面启动node(不要关闭命令行窗口,如要停止,使用快捷键Ctrl+c)

java -Dwebdriver.gecko.driver="D:\\drivers\\geckodriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -port 5556 -hub http://192.168.0.124:4444/grid/register

9)在node3机的命令行窗口里面启动node(不要关闭命令行窗口,如要停止,使用快捷键Ctrl+c)

java -Dwebdriver.ie.driver="D:\\drivers\\IEDriverServer.exe" -jar selenium-server-standalone-3.141.59.jar -role node -port 5557 -hub http://192.168.0.124:4444/grid/register

10)配置各种主流浏览器

InternetExplorerOptions options = new InternetExplorerOptions();//获取ie浏览器的配置工具
options.setCapability("browser.name", "internet explorer");//设置浏览器的名称
options.setCapability("platform", Platform.WINDOWS);//设置浏览器的版本(WIN版)
options.requireWindowFocus();//解决ie浏览器输入文本很慢的问题
FirefoxOptions options = new FirefoxOptions();//获取firefox浏览器的配置工具
options.setCapability("browser.name", "firefox");//设置浏览器的名称(firefox)
options.setCapability("platform", Platform.WINDOWS);//设置浏览器的版本(WIN版)
ChromeOptions options = new ChromeOptions();//获取chrome浏览器的配置工具
options.setCapability("browser.name", "chrome");//设置浏览器的名称(chrome)
options.setCapability("platform", Platform.WINDOWS);//设置浏览器的版本(WIN版)

11)RemoteWebDriver可以理解为是一个远程驱动工具(操作远程的浏览器)

WebDriver driver = new RemoteWebDriver(new URL("http://192.168.0.124:4444/wd/hub"), options);//连接hub机

12)URL表示地址
new URL(“http://192.168.0.124:4444/wd/hub”):将String类型的地址转成URL类型的地址

在hub机的命令行窗口里面启动hub

image.png

在node1机的命令行窗口里面启动node

image.png

在node2机的命令行窗口里面启动node

image.png

在node3机的命令行窗口里面启动node

image.png

使用浏览器访问分布式控制台

image.png

完整代码

如下:

package examples;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.Matchers.*;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import org.testng.annotations.*;

public class SearchTest {
	  private WebDriver driver;

	  @DataProvider(name="ds",parallel=false)
	  public static Object[][] data(){
		  Object[][] arr = {
				  {"https://www.bing.com",By.id("sb_form_q"),By.id("sb_form"),"赵丽颖","冯绍峰"},
				  {"https://www.bing.com",By.id("sb_form_q"),By.id("sb_form"),"王宝强","马蓉"},
				  {"https://www.bing.com",By.id("sb_form_q"),By.id("sb_form"),"赵薇","黄有龙"}				  
		  };
		  return arr;
	  }
		
	  @Test(dataProvider="ds")
	  public void f(String url,By b1,By b2,String keyword,String expected) throws Exception{
		  driver.get(url);//打开必应搜索网站
		  WebElement inputBox = driver.findElement(b1);//定位关键字输入框	
		  inputBox.clear();
		  inputBox.sendKeys(keyword);//输入搜索关键字		
		  //driver.findElement(By.id("sb_form_go")).sendKeys(Keys.ENTER);//点击搜索
		  driver.findElement(b2).submit();
		  Thread.sleep(2000);		
		  String html = driver.getPageSource();//获取搜索结果页面的源代码
		  Assert.assertThat(html,containsString(expected));//验证结果
	  }
	  
	  @BeforeTest(enabled=true)
	  public void openIE() throws Exception {
	      InternetExplorerOptions options = new InternetExplorerOptions();//获取ie浏览器的配置工具
	      options.setCapability("browser name", "internet explorer");//设置浏览器的名称
	      options.setCapability("platform", Platform.WINDOWS);//设置浏览器的版本(WIN版)
	      options.requireWindowFocus();//解决ie浏览器输入文本很慢的问题
	      driver = new RemoteWebDriver(new URL("http://192.168.0.124:4444/wd/hub"), options);//连接hub
	      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置默认等待时长
	  }
	  
	  @BeforeTest(enabled=false)
	  public void openChrome() throws Exception {
	      ChromeOptions options = new ChromeOptions();//获取chrome浏览器的配置工具
	      options.setCapability("browser name", "chrome");//设置浏览器的名称(chrome)
	      options.setCapability("platform", Platform.WINDOWS);//设置浏览器的版本(WIN版)
	      driver = new RemoteWebDriver(new URL("http://192.168.0.153:4444/wd/hub"), options);//连接hub
	      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置默认等待时长
	  }	  
	  
	  @BeforeTest(enabled=false)
	  public void openFirefox() throws Exception {
	      FirefoxOptions options = new FirefoxOptions();//获取firefox浏览器的配置工具
	      options.setCapability("browser name", "firefox");//设置浏览器的名称(firefox)
	      options.setCapability("platform", Platform.WINDOWS);//设置浏览器的版本(WIN版)
	      driver = new RemoteWebDriver(new URL("http://192.168.0.124:4444/wd/hub"), options);//连接hub
	      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置默认等待时长
	  }	  	  
	  
	  @AfterTest
	  public void end() {
		  driver.quit();//关闭浏览器
	  }
	  
}

参考资料

[1]Selenium Grid的原理、配置与使用
https://blog.csdn.net/ouyanggengcheng/article/details/79935657
[2]Selenium 调用IEDriverServer打开IE浏览器
https://www.cnblogs.com/misswjr/p/9453566.html
[3]Selenium IE webdriver 常见的一些问题
https://www.jianshu.com/p/3ee5587ee364
[4]python+selenium+IE11登陆页面click失效,submit()没有加密问题
https://blog.csdn.net/qq_16045253/article/details/85767410
[5]解决selenium2在IE11上出错的问题,如Unable to get browser
https://blog.csdn.net/chengly0129/article/details/68482829
[6]selenium中为什么有些IE浏览器中输入英文和数字特别慢
https://blog.csdn.net/qew110123/article/details/85853374
[7]将元素拖动到指定处
https://blog.csdn.net/qq_36969649/article/details/84137456
[8]解决selenium Action模拟拖拽无效
https://blog.csdn.net/lxlyes/article/details/82420506

微信扫一扫关注公众号
image.png
点击链接加入群聊

https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值