我想模拟谷歌搜索与使用Selenium和TestNG并行各种搜索参数。下面是我的测试类的testng.xml。我曾尝试使用下面的注解@Test(dataProvider="googlesearchDataProvider",threadPoolSize=3,singleThreaded=false)
或测试的testng.xml我的测试clases。这两种情况下的测试在单个浏览器单线程跑。能否请你让我知道什么是错的,什么需要做的事情。我想在同一时间会有并联不同的搜索数据的数据提供者服用运行3个独立的浏览器实例。
package com.test.google.search;
import static org.testng.Assert.fail;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.aig.testframework.Driver;
/**
* @author dpoddar
*
*/
public class GoogleSearch {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
//@BeforeClass(alwaysRun = true)
@BeforeTest(alwaysRun = true)
public void setUp() throws Exception {
Driver.getInstance().setDriver("firefox", "Windows 7", "local");
driver = Driver.getInstance().getDriver();
baseUrl = "https://www.google.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider="googlesearchDataProvider",threadPoolSize=3,singleThreaded=false)
public void testGoogleSearch(String serachParam) throws Exception {
System.out.println("testGoogleSearch. Thread id is: " + Thread.currentThread().getId());
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys(serachParam);
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
}
//@AfterClass(alwaysRun = true)
@AfterTest(alwaysRun=true)
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
@DataProvider(name = "googlesearchDataProvider")
public Object[][] createData(Method m) {
System.out.println(m.getName()); // print test method name
return new Object[][] { new Object[] { "Test"},{"google"},{"Java"},{"Spring"},{"AWS"},{"Market Trends"},{"Hotels Near 91367"},{"Starbucks"}};
}
}
的testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="instances" thread-count="3">
<test name="Regression 1" >
<classes>
<class name="com.test.google.search.GoogleSearch"/>
</classes>
</test>
</suite>
我解决了这个使用@Factory
和的testng.xml变化。变化如下。
请使用其他方法形成的问题陈述。
public class GoogleSearch {
private String searchParam;
@Factory(dataProvider = "googlesearchDataProvider",enabled=true)
public GoogleSearch(String searchParam) {
this.searchParam = searchParam;
}
@Test
public void testGoogleSearch() throws Exception {
System.out.println("testGoogleSearch with "+searchParam+". Thread id is: " + Thread.currentThread().getId());
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys(searchParam);
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
}
}
的testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" verbose="2">
<test name="Regression 1" parallel="instances" thread-count="3">
<classes>
<class name="com.test.google.search.GoogleSearch"/>
</classes>
</test>
</suite>