Detecting app crashes on iOS/ Android using Appium

Abhishek Dhoundiyal
2 min readFeb 26, 2023

For iOS Devices:

The code will check if your app is getting code “1” which means that the app has Crashed.

If it will get the app code “1” then it will close the session and stop the test case execution.

mobile: queryAppState

Queries the state of an existing application on the device. There are five possible application states (check Apple’s documentation for more details):

  • 0: The current application state cannot be determined/is unknown
  • 1: The application is not running
  • 2: The application is running in the background and is suspended
  • 3: The application is running in the background and is not suspended
  • 4: The application is running in the foreground

Supported arguments

  • bundleId: The bundle identifier of the application, which state is going to be queried. Mandatory argument.
Map<String, Object> params = new HashMap<>();
params.put("bundleId", "com.yourAppBundleId");
final int state = (Integer)js.executeScript("mobile: queryAppState", params);
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
Map<String, Object> params = new HashMap<>();
params.put("bundleId", "com.yourAppBundleId");
final Long state = (Long)js.executeScript("mobile: queryAppState", params);"Application state code is :"+state);
if (state==1) {
log.error(LogStatus.FAIL, "Application is Crashed");
catch (Exception e){
log.error("Application state error :" +e);

For Android Devices:

List<LogEntry> adbLogs = driver().manage().logs().get("logcat").filter(Level.ALL);"First timestamp: " + adbLogs.get(0).getTimestamp());"Last timestamp: " + adbLogs.get(adbLogs.size()-1).getTimestamp());

public void getMobileLogs(AppiumDriver driver) {
try {
String formattedDate = new SimpleDateFormat(DATE_FORMAT).format(new Date()); + ": Saving device log...");
List<LogEntry> logEntries = driver.manage().logs().get("logcat").getAll();
for (LogEntry entry : logEntries) {
String debugLevel = entry.getLevel().toString().trim();
String debugMessage = entry.getMessage().trim();
log.debug(formattedDate + LINE_BREAK + debugLevel + LINE_BREAK + debugMessage);
} catch (Exception ex) {


List<LogEntry> adbLogs = driver().manage().logs().get("logcat").filter(Level.ALL);
List<LogEntry> adbLogs = driver().manage().logs().get("crashlog").filter(Level.ALL);

Types as per documentation:

  • logTypes.should.include(‘syslog’);
  • logTypes.should.include(‘crashlog’);
  • logTypes.should.not.include(‘logcat’);