static Webdriver instance synchronization in java

后端 未结 4 1336
北荒
北荒 2020-12-05 22:05

GlobalVariables class holds different variables which are used across my framework one of them is WebDriver instance:

  public class GlobalVariables
                 


        
相关标签:
4条回答
  • 2020-12-05 22:44

    you can try something like this

    public class DriverManager {

    private static final ThreadLocal<WebDriver> threadLocal = new ThreadLocal<WebDriver>();
    
    public static WebDriver getDriver() {
        return threadLocal.get();
    }
    
    public static void setDriver(WebDriver driver) {
        threadLocal.set(driver);
    }
    
    public static void closeDriver() {
        if (getDriver() != null) {
            try {
                getDriver().close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                getDriver().quit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        threadLocal.remove();
    }
    

    }

    0 讨论(0)
  • 2020-12-05 22:51

    I have found out the solution : Use of ThreadLocal is the best solution to run tests in a huge multithreaded environment. Code snippet to use WebDriver in multithreaded environment:

    public static ThreadLocal<WebDriver> driver;
    driver=new ThreadLocal<WebDriver>()
                    {
                        @Override
                        protected WebDriver initialValue()
                        {
                            return new FirefoxDriver(); //You can use other driver based on your requirement.
                        }
                    };
    

    Now every time a test thread is created a new browser will open. ThreadLocal will make sure that there's only one copy of static webdriver instance per thread. [NOTE: Make sure your other global variables are too ThreadLocals. In my case they were not thats why i was running into test goof up issue]. Some extra knowledge which i would like to share so that others may find it informative. In ThreadLocal whenever the ThreadLocal.get() method is called you have to make sure that there is a provision to initialize the thread local as shown above in initialValue() method or you may run into null pointer exception. Thanks everyone.

    0 讨论(0)
  • 2020-12-05 22:59

    You are getting this because of how JVM handles static members and methods.

    You can't have a static webdriver object if you are going to run in parallel.

    Source: The automated regression system i implemented where I work - we had this issue.

    0 讨论(0)
  • 2020-12-05 23:04

    If you are going to run non-parallel, then using a static webdriver member (or a instance shared between test classes by passing by reference) is fine because it is a good way to not have to close the webdriver instance between test classes. If you want to run parallel though, you need to have one instance of webdriver for EACH thread and so in that case using a static member is the WRONG way to go. Instead you need to create or pass a webdriver instance when the test case class is invoked.

    Also, you are breaking a test into separate tests for each step of the test. That is very unusual and I do not see the reason why you are doing that. You could really simplify your tests by keeping all the test steps within one singe test case like people usually do.

    0 讨论(0)
提交回复
热议问题