Override Appium Driver Log Method to Print Log Values to Console
info
Please note that this tool is classified as a Legacy tool. We recommend transitioning to our updated solutions to maintain optimal performance and security in your workflows. For more information on this matter, please reach out to technical support .
In order to have the IDE console print Appium Studio logs, you should create a new driver class by extending the existing AndroidDriver or IOSDriver classes and override the log method.
info
Overriding log method for AndroidDriver
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.SessionId;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
public class NewAndroidDriver extends AndroidDriver {
private final String deviceID;
private String deviceName = "";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
public NewAndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
this.deviceID = (String) desiredCapabilities.getCapability("udid");
try {
this.deviceName = ((String) desiredCapabilities.getCapability("deviceName")).replace(" ", "_").replace("'", "-").trim();
} catch (Exception e) {
this.deviceName = deviceID;
}
}
@Override
protected void log(SessionId sessionId, String commandName, Object toLog, When when) {
if (commandName.equals("newSession")) sdf = new SimpleDateFormat("HH:mm:ss");
super.log(sessionId, commandName, toLog, when);
System.out.println(sdf.format(new Date(System.currentTimeMillis())) + ": " + deviceID + " - " + when + ": " + commandName + " toLog:" + toLog);
if (deviceName != null) {
System.out.println(deviceName + sdf.format(new Date(System.currentTimeMillis())) + when + ": " + commandName + " toLog:" + toLog);
}
}
}
Overriding log method for IOSDriver
import io.appium.java_client.ios.IOSDriver;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.remote.SessionId;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
public class NewIOSDriver extends IOSDriver {
private String deviceID = null;
private String deviceName = null;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
public NewIOSDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
try {
this.deviceID = (String) desiredCapabilities.getCapability("udid");
this.deviceName = ((String) desiredCapabilities.getCapability("deviceName")).replace(" ", "_").replace("'", "-").trim();
} catch (Exception e) {
System.out.println("No Id or Name");
}
}
@Override
protected void log(SessionId sessionId, String commandName, Object toLog, When when) {
if (commandName.equals("newSession")) sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf.format(new Date(System.currentTimeMillis())) + ": " + deviceID + " - " + when + ": " + commandName + " toLog:" + toLog);
super.log(sessionId, commandName, toLog, when);
if (deviceName != null) {
System.out.println(deviceName +" - "+ sdf.format(new Date(System.currentTimeMillis())) + when + ": " + commandName + " toLog:" + toLog);
}
}
}
In your test script, initialize the driver using the newly created driver classes:
driver = new NewIOSDriver(new URL("http://localhost:4723/wd/hub"), dc);
OR
driver = new NewAndroidDriver(new URL("http://localhost:4723/wd/hub"), dc);