3

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);
}
Cornest
  • 269
  • 1
  • 15

2 Answers2

1

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

KNewman Liuu
  • 136
  • 1
  • 5
-1

In java I am doing it using the following code:

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

Not sure if this method works for C#. Please give it a try

List<LogEntry> logEntries = _driver.Manage().Logs().Get("logcat").GetAll();
Cornest
  • 269
  • 1
  • 15
Suban Dhyako
  • 2,436
  • 4
  • 16
  • 38
  • Thank you for your reply, I tried: ReadOnlyCollection logs = _driver.Manage().Logs.GetLog("logcat"); but the test fails again. What does the log look like in java? – Cornest Mar 26 '19 at 07:58
  • It will show the log that we get while running **adb devices** command. What do you mean by test fail? – Suban Dhyako Mar 26 '19 at 08:29
  • When I try to log the results to the debug output the test will fail with the message: 'System.NullReferenceException : Object reference not set to an instance of an object.'. If I remove that specific code (to log the results) the test succeeds. – Cornest Mar 26 '19 at 08:35
  • Can you replace ReadOnlyCollection with List and try if it is showing the same error? – Suban Dhyako Mar 26 '19 at 09:13
  • please check [List](https://www.tutorialsteacher.com/codeeditor?cid=cs-At1qDb) to learn how to use list in c# – Suban Dhyako Mar 26 '19 at 11:39
  • I know how to use lists in C#. But the GetLog method returns a ReadOnlyCollection of LogEntries. Link: https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_ILogs_GetLog.htm – Cornest Mar 26 '19 at 13:23