1 回答
TA贡献1785条经验 获得超8个赞
您必须为您的应用程序创建类似驱动程序池的东西。您可以从这个池中为您的测试用例启动一堆驱动程序。
此外,请考虑如何取消您的驱动程序实例静态。
我过去尝试过类似的事情:
public class DriverPool {
public static final int MAX_NUMBER = 5;
private static final Object waitObj = new Object();
private static AtomicInteger counter = new AtomicInteger(0);
private static Logger log = Logger.getLogger(DriverPool.class);
private static volatile ThreadLocal<WebDriver> instance = ThreadLocal
.withInitial(DriverManager::getInstance);
public static synchronized WebDriver getDriver() {
try {
while (counter.get() > MAX_NUMBER) {
synchronized (waitObj) {
waitObj.wait();
}
}
counter.getAndIncrement();
} catch (InterruptedException e) {
log.error(e);
}
return instance.get();
}
public static synchronized void closeDriver() {
WebDriver driver = instance.get();
driver.close();
driver.quit();
instance.remove();
counter.decrementAndGet();
synchronized (waitObj) {
waitObj.notifyAll();
}
}
}
我希望它会有所帮助。
添加回答
举报