Search code examples
c#testingloggingappiumappium-desktop

Log Appium testresults to console


Appium won't log the test results (of the UI-tests, executed with adb emulator) to the debug output (Deug.WriteLine).

According to the documentation, get test logs is possible with the following line

ILogs logs = driver.Manage().Logs;

Hower, Appium has different log types:

  • Browser
  • Client
  • Driver
  • Profiler
  • Server

I tried every single log type with the following code. But by executing I don't get any result and the test will (where I put the code) fail. Does anyone have a solution for this problem?

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);
}

Solution

  • I just figure out.

    1. Check this article first relaxed Security AppiumService

    2. Get the log type

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

      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