简体   繁体   English

如何在C#中使WebDriver线程安全?

[英]how to make webdriver threadsafe in c#?

I have searched in Google but did not find a solution. 我已经在Google中搜索过,但没有找到解决方案。 Could someone please help me in it as I am able to find the solution in java but not in C#. 有人可以帮我解决这个问题,因为我可以在Java中找到解决方案,但在C#中找不到。

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

Using C# ThreadLocal , something like this? 使用C# ThreadLocal ,像这样吗?

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. 注意,除非需要,否则我几乎不会用C#编写代码。

Thread safety of Selenium is never guaranteed and strictly speaking it's not thread safe. 永远不能保证Selenium线程安全,严格来说,它不是线程安全的。 Please refer to this 请参考这里

Java solution: Java解决方案:

Multithreaded client code should use this to assert that it accesses webdriver in a thread-safe manner. 多线程客户端代码应使用它来断言它以线程安全的方式访问webdriver。 Wrap WebDriver instances as follows: 将WebDriver实例包装如下:

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 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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