参数化selenium测试与TestNG并行

首先,对不起我的英语,它不是那么完美:)

所以我面临以下问题:我正在尝试使用Selenium Grid和TestNg在不同的浏览器中运行并行测试,并且我在@BeforeTest方法中传递参数。 我的问题是,当每个测试都被初始化时,似乎他们将使用最后一个测试的参数。 因此,在此示例中,当我运行测试时,它将打开两个Chrome,而不是一个Firefox和一个Chrome。 (browser.getDriver()方法返回一个RemoteWebDriver)

testng.xml文件:

                     

AbstractTest类:

 public class SeleniumTest { private static List webDriverPool = Collections.synchronizedList(new ArrayList()); private static ThreadLocal driverThread; public static BrowserSetup browser; @Parameters({ "browserName", "browserVersion", "platform"}) @BeforeTest() public static void beforeTest(String browserName, @Optional("none") String browserVersion, String platform) throws WrongBrowserException, WrongPlatformException { final BrowserSetup browser = new BrowserSetup(browserName, browserVersion, platform); driverThread = new ThreadLocal() { @Override protected WebDriver initialValue() { final WebDriver webDriver = browser.getDriver(); webDriverPool.add(webDriver); return webDriver; } }; } public static WebDriver getDriver() { return driverThread.get(); } @AfterTest public static void afterTest() { for (WebDriver driver : webDriverPool) { driver.quit(); } } } 

我的例子@Tests:

 @Test public void test1() throws InterruptedException { WebDriver driver = getDriver(); System.out.println("START: test1"); driver.get("http://google.com"); Thread.sleep(5000); System.out.println("END: test1, title: " + driver.getTitle()); } @Test public void test2() throws InterruptedException { WebDriver driver = getDriver(); System.out.println("START: test2"); driver.get("http://amazon.com"); Thread.sleep(5000); System.out.println("END: test2, title: " + driver.getTitle()); } @Test public void test3() throws InterruptedException { WebDriver driver = getDriver(); System.out.println("START: test3"); driver.get("http://stackoverflow.com"); Thread.sleep(5000); System.out.println("END: test3, title: " + driver.getTitle()); } 

所以我的问题是如何在单独的线程中与给定参数并行运行测试?

提前致谢!

彼得

不要将字段设为静态。

 private static List webDriverPool = Collections.synchronizedList(new ArrayList()); private static ThreadLocal driverThread; public static BrowserSetup browser; 

如果要并行运行beforeTest()和afterTest(),则不应该是静态的,或者使其同步以使其线程安全。 此外,您不使用声明的变量:

 public static BrowserSetup browser; 

根本没有,或者你错过了那里,因为你也有:

 final BrowserSetup browser = new BrowserSetup(browserName, browserVersion, platform); 

在beforeTest内(…)