We've already seen how to capture browser logs for iOS; the same can be done for Android! The technique is largely similar but varies in some respects. As a reminder of why we might want to capture logs and errors from a browser, we could:
- Get under-the-hood information from our app that modulates the way our test works.
- Store the logs for future reference; we could give them to an app dev if a test fails, and they might be able to use the logs to pinpoint the nature of an otherwise confusing bug.
HeadSpin’s global device infrastructure enables mobile, and browser testing on hundreds of real devices deployed worldwide. Book a trial!
Remotely test and debug mobile, web, audio and video application on thousands of iOS devices around the world. Learn more!
But let's skip to the action. With iOS there was some complexity where we used a non-standard safariConsole log type and had to parse some JSON to get the actual data, with Android we can take advantage of the fact that Chrome automation happens via Chromedriver, which supports the full WebDriver protocol all on its own. Given that, we can simply make a request for the browser log type:
This returns a list of LogEntry objects, and for each one we can retrieve its message (and, say, print it to the terminal):
That's all there is to it, in terms of the test code itself. However, we do need to use a special loggingPrefs capability to convince Chromedriver to turn on capturing of the browser logs for us. In it simplest form, the loggingPrefs capability is an object specifying log types and log levels. For our purposes it ends up looking like:
Also check: Accessing Android Logcat Logs with Appium
But in Java-client-world, we can build up this little structure using classes and constants instead! That way our IDE can guide us to the correct structure without having to remember strings like "loggingPrefs":
With this capability set, browser log retrieval will work, regardless of whether you're automating Chrome itself or a hybrid app. The code sample for this edition is basically the same as for the iOS equivalent, so I added a hybrid test just to show that it works equally well with hybrid apps (though of course we have to switch into the web context before making the call to get the browser logs). Here's the full example (which you can also view on GitHub):