如何使用threadlocal for testng java.util.concurrent.ex

syqv5f0l  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(278)

当我用cucumber在jenkins和maven上运行测试用例时,有时它会给出错误java.util.concurrent.ex。我需要使用同步的方法才能正常工作。我有Map,我必须使用threadlocal,但我不知道如何使用它。你能帮助我吗?

private static Map<Integer, WebDriver> webDriverMap = new HashMap<Integer, WebDriver>();
    private static Map<Integer, String> browserMap = new HashMap<Integer, String>();
    private static Map<Integer, String> scenarioNameMap = new HashMap<Integer, String>();
    private static Map<Integer, String> featureNameMap = new HashMap<Integer, String>();
    private static Map<Integer, String> tagNameMap = new HashMap<Integer, String>();
    public static Map<String, String> scenarioSteps = new HashMap<String, String>();
    public String getBrowser () {

        return browserMap.get((int)Thread.currentThread().getId());
    }
    public static WebDriver getDriver() {

        return (WebDriver) webDriverMap.get((int)Thread.currentThread().getId());
    }

    public static synchronized void startDriver(String browser, String scenarioName) throws Throwable {

        webDriverMap.put((int)Thread.currentThread().getId(), initializeDriver(browser, scenarioName));
    }

    public static synchronized void setScenarioName(String scenarioName) throws Throwable {

        scenarioNameMap.put((int)Thread.currentThread().getId(), scenarioName);
    }

    public static synchronized String getScenarioName() throws Throwable {

        return scenarioNameMap.get((int)Thread.currentThread().getId());
    }

    public static synchronized void setFeatureName(String featureName) throws Throwable {

        featureNameMap.put((int)Thread.currentThread().getId(), featureName);
    }

    public static synchronized String getFeatureName() throws Throwable {

        return featureNameMap.get((int)Thread.currentThread().getId());
    }

    public static synchronized void setTagName(String tagName) throws Throwable {

        tagNameMap.put((int)Thread.currentThread().getId(), tagName);
    }

    public static synchronized String getTagName() throws Throwable {

        return tagNameMap.get((int)Thread.currentThread().getId());
    }

    public static synchronized void setScenarioSteps(String scenario, String steps) throws Throwable {

        scenarioSteps.put(scenario, steps);
    }

    public static synchronized String getScenarioSteps(String scenario) throws Throwable {

        return scenarioSteps.get(scenario);
    }

    public static synchronized void removeFinishedMap() {

        webDriverMap.remove((int)Thread.currentThread().getId());
        scenarioNameMap.remove((int)Thread.currentThread().getId());
        browserMap.remove((int)Thread.currentThread().getId());
        featureNameMap.remove((int)Thread.currentThread().getId());
        tagNameMap.remove((int)Thread.currentThread().getId());
    }

public static void stopDriver() {
         getDriver().quit();
         webDriverMap.remove((int)Thread.currentThread().getId());
    }
``` `Thread.currentThread().getId()` 我想我必须改变这个。

public String getBrowser () {
public static WebDriver getDriver() {
public static void stopDriver() {

我为此方法添加了synchronized,但它不起作用。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题