小编Oas*_*001的帖子

使用 Cucumber、Jmeter 和 FailSafe 的自动化框架是否需要 ThreadLocal?

对不起,如果这个问题听起来很愚蠢。我们正在开发一个使用 Java、Cucumber、Junit 和 Failsafe 套件等的自动化框架。有人建议使用 ThreadLocal。但是我有点困惑,为什么当 Junit 在自己的线程中运行黄瓜功能时我们需要使用 ThreadLocal ..

建议是使用 ThreadLocal ,如下所示;-

public class WebDriverFactory {

    private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();

    public static synchronized void setDriver(String browser) {

        switch (browser) {
            case "chrome":
                driver = ThreadLocal.withInitial(() -> {
                    WebDriverManager.chromedriver().setup();
                    return new ChromeDriver(BrowserOptions.getChromeOptions());
                });
        
                break;
           
            default:
                throw new IllegalStateException("Unexpected value: " + browser);
        }
    }

    public static synchronized WebDriver getDriver(){
        return driver.get();
    }
Run Code Online (Sandbox Code Playgroud)

任何人都可以确认这是否真的需要并行运行测试。?另外,使用 ThreadLocal 时是否需要“同步”?

java selenium multithreading automated-tests thread-local

3
推荐指数
1
解决办法
339
查看次数