Read server/client/logcat logs using Appium and JAVA

Versions used while doing this blog post,
Appium server version : 1.20.2
Appium JAVA client version : 7.5.1

As we know, Appium is capable of automating Web, native and hybrid apps in android. In this post, lets see how we can invoke an app in android and read its logs. The APK file of the app is in executors machine.

Before getting into actual coding, lets get the steps to be followed to make any appium program functional:
1. We need the latest appium java client and latest stable selenium to be referred into the project library

2. The first step of the program should be setting up of desired capabilities. Here we need to provide the details of the android device with which we are gonna interact and app that is to be installed & invoked.
The mandatory parameters here are:
a. DEVICE_NAME
b. APP [As, we have the apk/app file in PC from which we execute the program]

3. Next inline should be the creation of android/iOS driver by passing appium server URL and device capabilities

4. The types of available logs for current session can be read using logs().getAvailableLogTypes() method

Set<String> typesOfLogs = driver.manage().logs().getAvailableLogTypes();
System.out.println("Types of available logs -> " + typesOfLogs);

5. Log information can be read passing the type of logs as parameter in driver.manage().logs().get() method

LogEntries currentServerLogs = driver.manage().logs().get("server");
System.out.println("Current server logs -> " + currentServerLogs.getAll());

LogEntries currentClientLogs = driver.manage().logs().get("client");
System.out.println("Current client logs -> " + currentClientLogs.getAll());

LogEntries currentLogcatLogs = driver.manage().logs().get("logcat");
System.out.println("Current logcat logs -> " + currentLogcatLogs.getAll());


6. Once done with execution of expected functionality, the session should be closed/quit

Complete code:

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class ReadLogsFunctionality {
    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "******");
        if (System.getProperty("os.name").toLowerCase().startsWith("mac"))
            capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"/src/main/resources/ApiDemos-debug.apk");
        else
            capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"\\src\\main\\resources\\ApiDemos-debug.apk");


        URL url = new URL("http://0.0.0.0:4723/wd/hub");
        AndroidDriver driver = new AndroidDriver(url, capabilities);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOf(driver.findElementByAccessibilityId("Views")));


        Set<String> typesOfLogs = driver.manage().logs().getAvailableLogTypes();
        System.out.println("Types of available logs -> " + typesOfLogs);

        LogEntries currentServerLogs = driver.manage().logs().get("server");
        System.out.println("Current server logs -> " + currentServerLogs.getAll());

        LogEntries currentClientLogs = driver.manage().logs().get("client");
        System.out.println("Current client logs -> " + currentClientLogs.getAll());

        LogEntries currentLogcatLogs = driver.manage().logs().get("logcat");
        System.out.println("Current logcat logs -> " + currentLogcatLogs.getAll());


        driver.closeApp();
        driver.quit();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *