gpt4 book ai didi

java - 与 TestNG 并行的参数化 Selenium 测试

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:22 26 4
gpt4 key购买 nike

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

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

TestNG.xml:

<suite thread-count="2" verbose="10" name="testSuite" parallel="tests">
<test name="nameOfTheTestFirefox">
<parameter name="platform" value="windows"/>
<parameter name="browserVersion" value="32"/>
<parameter name="browserName" value="firefox"/>
<classes>
<class name="example.test.login.LoginOverlayTest"/>
</classes>
</test> <!-- nameOfTheTestFirefox -->
<test name="nameOfTheTestChrome">
<parameter name="platform" value="windows"/>
<parameter name="browserVersion" value="38"/>
<parameter name="browserName" value="chrome"/>
<classes>
<class name="example.test.login.LoginOverlayTest"/>
</classes>
</test> <!-- nameOfTheTestChrome -->
</suite> <!-- testSuite -->

AbstractTest 类:

public class SeleniumTest {

private static List<WebDriver> webDriverPool = Collections.synchronizedList(new ArrayList<WebDriver>());
private static ThreadLocal<WebDriver> 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<WebDriver>() {

@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<WebDriver> webDriverPool = Collections.synchronizedList(new ArrayList<WebDriver>());
private static ThreadLocal<WebDriver> driverThread;
public static BrowserSetup browser;

关于java - 与 TestNG 并行的参数化 Selenium 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26604745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com