美文网首页
使用TestNG并发测试搜索功能

使用TestNG并发测试搜索功能

作者: 测试老杨 | 来源:发表于2018-10-23 17:58 被阅读28次

思路

在使用TestNG做自动化测试时,基本上大家都会使用DataProvider来管理一个用例的不同测试数据。执行时会发现,测试数据集中的多组数据依然是顺序执行。
解决方式是:在 @DataProvider中添加parallel=true

设计脚本

代码如下:

package webtest;

import static org.testng.Assert.assertTrue;

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

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
 * 使用3个浏览器并发测试搜索
 * @author yangzc
 *
 */
public class SearchTest2 {
  //private WebDriver driver;   
    
  @BeforeMethod
  public void begin() throws Exception{
      //DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
      //capability.setBrowserName("internet explorer");
      //capability.setPlatform(Platform.WINDOWS);
      //driver = new RemoteWebDriver(new URL("http://192.168.0.130:5555/wd/hub"), capability);
      //设置浏览器的路径
      //System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");       
      //设置浏览器驱动程序的路径
      //System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");    
      //System.setProperty("webdriver.gecko.driver", "D:/Python27/geckodriver.exe");      
      //System.setProperty("webdriver.ie.driver", "D:\\MicrosoftWebDriver.exe");
      //创建一个驱动工具(目的是为了操作浏览器),创建驱动工具的时候,浏览器会被打开)
      //driver = new ChromeDriver();
      //driver = new FirefoxDriver();
      //driver = new InternetExplorerDriver();
  }
    
  @Test(dataProvider="mydrivers")
  public void f(WebDriver driver) throws Exception{
      //窗口最大化
      driver.manage().window().maximize();
      //设置等待时长
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      //打开必应搜索网站
      driver.get("https://cn.bing.com/");
      //点击搜索框
      driver.findElement(By.id("sb_form_q")).click();
      //清空搜索框里面的内容
      driver.findElement(By.id("sb_form_q")).clear();     
      //输入搜索的关键字
      driver.findElement(By.id("sb_form_q")).sendKeys("赵丽颖"); 
      Thread.sleep(3000);
      //点击搜索的按钮
      driver.findElement(By.id("sb_form_go")).click();
      Thread.sleep(3000);
      //提交表单sb_form
      //driver.findElement(By.id("sb_form")).submit();
      //获取HTML页面的源码
      String html = driver.getPageSource();
      System.out.println(html);
      //验证搜索是否成功
      assertTrue(html.contains("冯绍峰"));
      //
      Thread.sleep(3000);
      //
      driver.quit();
  }
  
  @DataProvider(parallel=true)
  public Object[][] mydrivers() throws Exception {
      DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
      capability.setBrowserName("internet explorer");
      capability.setPlatform(Platform.WINDOWS);
      DesiredCapabilities capability2 = DesiredCapabilities.firefox();
      capability2.setBrowserName("firefox");
      capability2.setPlatform(Platform.WINDOWS);
      DesiredCapabilities capability3 = DesiredCapabilities.chrome();
      capability3.setBrowserName("chrome");
      capability3.setPlatform(Platform.WINDOWS);      
      
      Object[][] drivers = {
              {new RemoteWebDriver(new URL("http://192.168.0.130:5555/wd/hub"), capability)},
              {new RemoteWebDriver(new URL("http://192.168.0.130:5556/wd/hub"), capability2)},
              {new RemoteWebDriver(new URL("http://192.168.0.130:5557/wd/hub"), capability3)},            
      };
      return drivers;
  }  
  
  @AfterMethod
  public void end(){
      //关闭浏览器
      //driver.quit();
  }
}

执行测试

swiper0912_03.gif

参考资料

https://www.cnblogs.com/znicy/p/6534893.html

相关文章

  • 使用TestNG并发测试搜索功能

    思路 在使用TestNG做自动化测试时,基本上大家都会使用DataProvider来管理一个用例的不同测试数据。执...

  • TestNG

    TestNG 的特点 注解TestNG使用Java和面向对象的功能支持综合类测试独立的编译时测试代码和运行时配置/...

  • k13–7/15 selenium04 - TestNG

    功能测试的流程: 分析需求 → 排测试计划 → 设计测试用例 → 执行测试用例 TestNG说明 TestNG 最...

  • 自动化测试框架selenium+java+TestNG——Tes

    TestNG是java的一个测试框架,相比较于junit,功能更强大和完善,我是直接学习和使用的TestNG就来...

  • 测试框架TestNG使用介绍

    今天分享TestNG测试框架的基础知识,使用TestNG的优点,TestNG的基本注解如何使用,套件、忽略、异常、...

  • 第三节 TestNG测试框架

    TestNG介绍 一、测试人员使用的原因1、比Junit涵盖功能更全面的测试框架2、Junit更适合隔离性比较强的...

  • TestNG用法总结

    TestNG用法总结 常用注解见表格 TestNG的简单使用 xml: 运行结果如图:运行结果 组测试组测试即为在...

  • 【Java进阶营】Java技术专题「TestNG专题」单元测试框

    TestNG介绍 **TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit, 功能都差不多, ...

  • testNg使用

    testNg使用 测试是检查应用程序的功能的过程是否按要求工作,在开发人员层面进行单元测试,在采取适当措施来测试每...

  • 批量执行测试用例

    想要批量执行测试用例,可以使用TestNG这个插件,配合maven使用。 1. 创建testng.xml文件 1)...

网友评论

      本文标题:使用TestNG并发测试搜索功能

      本文链接:https://www.haomeiwen.com/subject/sfwazftx.html