猿问

使用 selenium 在 java 中使用 tesng 进行并行测试

实际上,我正在创建一个框架,但在创建它时我只希望它用于串行执行,但现在我想对方法进行并行测试。问题是我已将驱动程序实例声明为静态,并且由于静态第二个线程无法更改其值。

我正在一个单独的类中创建驱动程序并使用 getter 方法获取它。

现在的问题是,如果我将 Webdrive 设为非静态,那么我将无法在其他类中使用该驱动程序。

即使我尝试扩展类(我在其中创建驱动程序实例),它也会传递一个空值。

所以,基本上我想隔离所有实例,但我不能在本地创建实例到类。我尝试删除静态变量,但在进行并行执行时,打开了 2 个浏览器实例,但所有测试用例都在一个浏览器中执行,而且太并行了

我怎样才能做到这一点?


慕桂英4014372
浏览 128回答 1
1回答

慕的地10843

您必须为您的应用程序创建类似驱动程序池的东西。您可以从这个池中为您的测试用例启动一堆驱动程序。此外,请考虑如何取消您的驱动程序实例静态。我过去尝试过类似的事情:public class DriverPool {&nbsp; &nbsp; public static final int MAX_NUMBER = 5;&nbsp; &nbsp; private static final Object waitObj = new Object();&nbsp; &nbsp; private static AtomicInteger counter = new AtomicInteger(0);&nbsp; &nbsp; private static Logger log = Logger.getLogger(DriverPool.class);&nbsp; &nbsp; private static volatile ThreadLocal<WebDriver> instance = ThreadLocal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .withInitial(DriverManager::getInstance);&nbsp; &nbsp; public static synchronized WebDriver getDriver() {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (counter.get() > MAX_NUMBER) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (waitObj) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; waitObj.wait();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter.getAndIncrement();&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.error(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return instance.get();&nbsp; &nbsp; }&nbsp; &nbsp; public static synchronized void closeDriver() {&nbsp; &nbsp; &nbsp; &nbsp; WebDriver driver = instance.get();&nbsp; &nbsp; &nbsp; &nbsp; driver.close();&nbsp; &nbsp; &nbsp; &nbsp; driver.quit();&nbsp; &nbsp; &nbsp; &nbsp; instance.remove();&nbsp; &nbsp; &nbsp; &nbsp; counter.decrementAndGet();&nbsp; &nbsp; &nbsp; &nbsp; synchronized (waitObj) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; waitObj.notifyAll();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我希望它会有所帮助。
随时随地看视频慕课网APP

相关分类

Java
我要回答