0

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.

  • Try using ConcurrentHashMap. Also please add the exception and the full stack trace to your question. – tgdavies Dec 31 '20 at 09:26
  • You need to use ThreadLocal driver instead of using maps. Check https://stackoverflow.com/a/62674338/5324105 – Rahul L Dec 31 '20 at 09:54

1 Answers1

0

Please use existing TestNG parallel execution process - xml config because if we separately do thread processing, it will lead us to dead-locking and we need to spend lot of time to make it stable. I strongly request you to use TestNG's parallel processing

parallel="methods" / "classess" / "suite"

 <test name="Object's Operations" parallel="methods" thread-count="4"> <!--// parallel="methods" thread-count="3"-->
    <classes>
        <class name="Pages.Sensor.SauceParallel"/>
    </classes>
</test>
Jayanth Bala
  • 758
  • 1
  • 5
  • 11