【问题标题】:How to integrate event logging with C# and Selenium如何将事件日志记录与 C# 和 Selenium 集成
【发布时间】:2022-11-17 20:19:30
【问题描述】:

我正在记录中的所有事件项目与C#N单位使用 ThreadStatic 属性,因为每个测试都是在不同的线程中创建的。我面临的问题是在CreateDriver 方法中。该事件正在新线程中打开,现在 LoggingClass 中的 internalLog 变量位于不同的线程中。结果,该变量为空。我正在寻找一种方法来记录消息范围测试作为测试的其他消息。

技术:

  • C#
  • N单位
  • 范围报告
public class LoggingClass
{
    [ThreadStatic]
    private static ExtentTest internalLog;

    private void LogMessage(Status status, string message)
    {
        internalLog.Log(status, message);
    }
}

public class GlobalEnvironment
{
    // I initialize the log here
    public static ILog Logger => applicationLogger;
}

public class Driver
{
    public static IWebDriver CreateDriver()
    {       
        // I create the driver here and then I subscribe to the event for the console

        IJavaScriptEngine monitor = new JavaScriptEngine(driver);
        List<string> consoleMessages = new List<string>();
        monitor.JavaScriptConsoleApiCalled += (sender, e) =>
        {
            GlobalEnvironment.Logger.Info($"Log: {e.MessageContent}");
        };
        await monitor.StartEventMonitoring();
    }
}

【问题讨论】:

    标签: c# selenium-webdriver events extentreports selenium4


    【解决方案1】:

    我修改了 ExtentTest 变量。现在我没有使用线程,而是有一个包含测试全名的字典,我根据测试名称在其中保存日志。它看起来像这样:

    public class LoggingClass
    {
        private ConcurrentDictionary<string, ExtentTest> internalLogs;
    
        private void LogMessage(Status status, string message)
        {
            ExtentTest result;
            if (internalLogs.TryGetValue(TestContext.CurrentContext.Test.FullName, out result)) 
            {
                result.Log(status, message);
            }        
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 2018-02-26
      相关资源
      最近更新 更多