When i run my test cases with cucumber textNg on jenkins with maven not always bu sometimes it give it error java.util.concurrent.Ex. I need to use synchronized for my methods to work properly. I have maps and i have to use ThreadLocal but i am not sure how can i use it. Can you help me?
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()
i thing i have to change this one.
public String getBrowser () {
public static WebDriver getDriver() {
public static void stopDriver() {
i added synchronized for this methods but it doesn't work.