思路
在使用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();
}
}
网友评论