简体   繁体   中英

how to make webdriver threadsafe in c#?

I have searched in Google but did not find a solution. Could someone please help me in it as I am able to find the solution in java but not in C#.

private static final ThreadLocal < WebDriver > webDriver = 
    new ThreadLocal < WebDriver > () {
        @Override protected WebDriver initialValue() {
            return BrowserType.getBrowserType().getInstance();
        }
};

Using C# ThreadLocal , something like this?

public static ThreadLocal<IWebDriver> webDriver =
    new ThreadLocal<WebDriver>(() =>
    {
        return new InternetExplorerDriver();
    });

Hopefully you have the correct imports:

using OpenQA.Selenium;
using OpenQA.Selenium.IE;

Note, I barely code in C#, unless required.

Thread safety of Selenium is never guaranteed and strictly speaking it's not thread safe. Please refer to this

Java solution:

Multithreaded client code should use this to assert that it accesses webdriver in a thread-safe manner. Wrap WebDriver instances as follows:

WebDriver driver = ThreadGuard.protect(new FirefoxDriver());

Threading issues related to incorrect client threading may have mysterious and hard to diagnose errors. Using this wrapper prevents this category of errors. It is recommended for all multithreaded usage. This class has no overhead of any importance.

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM