【问题标题】:Log Appium testresults to console将 Appium 测试结果记录到控制台
【发布时间】:2019-08-16 12:44:35
【问题描述】:

Appium 不会将测试结果(使用 adb 模拟器执行的 UI 测试)记录到调试输出 (Deug.WriteLine)。

根据文档,可以使用以下行获取测试日志

ILogs logs = driver.Manage().Logs;

但是,Appium 有不同的日志类型:

  • 浏览器
  • 客户
  • 驱动程序
  • 分析器
  • 服务器

我使用以下代码尝试了每种日志类型。但是通过执行我没有得到任何结果,并且测试(我放置代码的地方)会失败。有人有解决这个问题的办法吗?

ReadOnlyCollection<LogEntry> logs = _driver.Manage().Logs.GetLog(LogType.Browser);
//  ReadOnlyCollection<LogEntry> logs = _driver.Manage().Logs.GetLog(LogType.Client);
//  ReadOnlyCollection<LogEntry> logs = _driver.Manage().Logs.GetLog(LogType.Driver);
//  ReadOnlyCollection<LogEntry> logs = _driver.Manage().Logs.GetLog(LogType.Profiler);
//  ReadOnlyCollection<LogEntry> logs = _driver.Manage().Logs.GetLog(LogType.Server);

foreach (var log in logs)
{
    Debug.WriteLine("Time: " + log.Timestamp);
    Debug.WriteLine("Message: " + log.Message);
    Debug.WriteLine("Level: " + log.Level);
}

【问题讨论】:

    标签: c# testing logging appium appium-desktop


    【解决方案1】:

    我只是想通了。

    1. 先查看这篇文章 relaxed Security AppiumService

    2. 获取日志类型

          IReadOnlyCollection<string> logTypes = driver.Manage().Logs.AvailableLogTypes;
          foreach (string logType in logTypes)
          {
              Console.WriteLine(logType);
              //logcat
              //bugreport
              //server
          }
      
    3. 打印日志

      public static void PrintLogs(string logType)
      {
          try
          {
              ILogs _logs = driver.Manage().Logs;
              var browserLogs = _logs.GetLog(logType);
              if (browserLogs.Count > 0)
              {
                  foreach (var log in browserLogs)
                  {
                      //log the message in a file
                      Console.WriteLine(log);
                  }
              }
          }
          catch(Exception e)
          {
              //There are no log types present
              Console.WriteLine(e.ToString());
          }
      

    Picture : C# Console Print appium log

    【讨论】:

    • 谢谢!我注意到他们已经更新了文档。
    • 别担心,伙计
    【解决方案2】:

    在 java 中,我使用以下代码:

    List<LogEntry> logEntries = driver.manage().logs().get("logcat").getAll();
    for (LogEntry logEntry : logEntries) {
            System.out.println(logEntry);
    }
    

    不确定此方法是否适用于 C#。请试一试

    List<LogEntry> logEntries = _driver.Manage().Logs().Get("logcat").GetAll();
    

    【讨论】:

    • 感谢您的回复,我试过了:ReadOnlyCollection logs = _driver.Manage().Logs.GetLog("logcat");但测试再次失败。 java中的日志是什么样子的?
    • 它将显示我们在运行 adb devices 命令时获得的日志。测试失败是什么意思?
    • 当我尝试将结果记录到调试输出时,测试将失败并显示以下消息:'System.NullReferenceException : Object reference not set to an instance of an object.'。如果我删除该特定代码(以记录结果),则测试成功。
    • 你能用 List 替换 ReadOnlyCollection 并尝试它是否显示相同的错误吗?
    • 请查看List了解如何在c#中使用list
    猜你喜欢
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多