How to find the current activity of an app in ios

Viewed 2655

I am using Appium for IOS testing. (java) I want to be able to check the current activity of an app . In the sense when an App switches from clickable to another (say I close the app or switch to another app or open the notifications scroll. I want to be able to detect the page name. To get the current activity name, i use driver.currentActivity(); which is included in the AndroidDriver class.

but There is no direct call for current package in ios, but many suggested that can be found out by looking at the attributes of existing elements in the device screen by calling driver.getPageSource(); how can I achieve this using driver.getPageSource or is there any other way to do it?Any advise on this would be helpful.

2 Answers

You can query current app state, if you have the bundleId of the app then you can query it's state if its running in background or foreground and it solves your problem.

Map<String, Object> params = new HashMap<>();
params.put("bundleId", "com.myapp");
final int state = (Integer)js.executeScript("mobile: queryAppState", params);

There can be 5 possible state as per the documentation.

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

Refrences

To see if your app is running, assuming you know the bundleId, you can use the driver directly:

driver.queryAppState('com.apple.Preferences');

It returns the ApplicationState:

NOT_INSTALLED,
NOT_RUNNING, 
RUNNING_IN_BACKGROUND_SUSPENDED,
RUNNING_IN_BACKGROUND, 
RUNNING_IN_FOREGROUND
Related