How to find the current activity of an app in ios - appium-ios

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.

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

When is sessionReachabilityDidChange(_:) called on a watch WCSessionDelegate?

Apple's documentation says this in the discussion section of the method description:
This method is called to let the
current process know that its counterpart session’s reachability
changed.
The description of the isReachable property says this: WatchKit
extension. The iOS device is within range, so communication can occur
and the WatchKit extension is running in the foreground, or is running
with a high priority in the background (for example, during a workout
session or when a complication is loading its initial timeline data).
I am assuming this would mean that if the watch moves out of or into range of the iOS device, the WatchKit extension would be launched and the WCSessionDelegate's sessionReachabilityDidChange() method would be called, and the WCSession's isReachable would be true if the iOS device just came into range and false if it just when out of range.
I have not found a way to verify this in xcode. For example I put a log message in sessionReachabilityDidChange(_:) and walked out of range, but xcode simply says the app lost connection with the iphone and can no longer debug it. Can someone verify this or point me to some documentation that better describes this?
I think you cannot verify this in Xcode.
I have an app on iOS and watchOS. To check this kind of situation, I can enable debug alerts on iOS and watchOS. When func sessionReachabilityDidChange(session: WCSession) is triggered, I display a debug alert.
Now, if I run (not under Xcode) my watch extension, and then switch off the paired iPhone, the debug alert is shown on the watch.
This shows that sessionReachabilityDidChange is actually called as expected.
Apparently, under Xcode a connected iOS device is always reachable.
EDIT:
To check the situation when the watch extension is not in foreground, I did the following:
Instead of showing a debug alert, I set now the complication to a unique value that is not possible otherwise. I launched the watch extension and put it into background by showing the watch face with the complication.
When I now switch off the iPhone, the complication is not updated.
This indicates to me that sessionReachabilityDidChange is not called in background.

iOS 13 Schedule iOS background tasks

I am implementing BackgroundTasks Framework for updating the data. But I got the below issue
Could not schedule refreshApp: Error Domain=BGTaskSchedulerErrorDomain Code=1 "(null)"
Could not schedule data featch: Error Domain=BGTaskSchedulerErrorDomain Code=1 "(null)"
2019-10-01 19:19:32.550320+0530 SOBackgroundTask[34131:1129470] Can't end BackgroundTask: no background task exists with identifier 3 (0x3), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
Here are possible error codes for Domain=BGTaskSchedulerErrorDomain extracted from ObjC headers with some explanation.
BGTaskSchedulerErrorCodeUnavailable = 1 // Background task scheduling functionality is not available for this app/extension. Background App Refresh may have been disabled in Settings.
BGTaskSchedulerErrorCodeTooManyPendingTaskRequests = 2 // The task request could not be submitted because there are too many pending task requests of this type. Cancel some existing task requests before trying again.
BGTaskSchedulerErrorCodeNotPermitted = 3 // The task request could not be submitted because the appropriate background mode is not included in the UIBackgroundModes array, or its identifier was not present in the BGTaskSchedulerPermittedIdentifiers array in the app's Info.plist.
The solution is to run on a device. I was running on a simulator. However, It was showing the Background App Refresh has been enabled in Settings while running on the simulator.
There may be some other reasons. Please visit
https://developer.apple.com/documentation/backgroundtasks/bgtaskschedulererrorcode/bgtaskschedulererrorcodeunavailable?language=objc
For:
BGTaskSchedulerErrorDomain error 3
Check inside the project’s .xcodeproj file for the appropriate target. Then go to the info tab and Custom iOS Target Properties and check that the permitted background task scheduler identifiers (BGTaskSchedulerPermittedIdentifiers) are added.
This solved my problem when adding BackgroundTasks to an existing project.
Please check if you miss registering BGTaskSchedulerPermittedIdentifiers in info.plist file of your project.
I have tested on real device(iOS13.2, and iOS13.2.2), but it's same result.
Error Domain=BGTaskSchedulerErrorDomain Code=2 "(null)"
Can't end BackgroundTask: no background task exists with identifier 37 (0x25), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
Error Domain=BGTaskSchedulerErrorDomain Code=1 "(null)"
Can't end BackgroundTask: no background task exists with identifier 113 (0x71), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
It seems that there is still exist bug.
https://forums.developer.apple.com/thread/121990
I think this is because that your mobile turn the bg refresh off!!!!!!!
In case of BGTaskSchedulerErrorDomain error 3, I didn't add below
<array>
<string>com.shiny.job</string>
<string>com.shiny.jobpower</string>
<string>com.shiny.jobnet</string>
<string>com.shiny.jobpowernet</string>
</array>```
Just check the Background Modes has been added to the target Capabilities and that Background fetch and Background processing options are selected
According to https://developer.apple.com/documentation/backgroundtasks/bgtaskschedulererrorcode/bgtaskschedulererrorcodeunavailable?language=objc, this error usually occurs for one of three reasons:
The user has disabled background refresh in settings.
The app is running on Simulator which doesn’t support background
processing.
The keyboard extension either hasn’t set RequestsOpenAccess to YES in
The Info.plist File, or the user hasn’t granted open access.
The extension type isn’t able to schedule background tasks.
check you Appdelegate.swift is including WorkmanagerPlugin.registerTask(withIdentifier: "your.task.id")

Why won't Application Insights data show in my dashboard when reported from a Background Task?

I've got MS App Insights data being logged from a Background Task when it runs. However, though I receive no exceptions from AI when debugging the code, nothing is showing in my dashboard as I would expect. All other telemetry from the same session (in main app, not Background Task) logs just fine.
I'm attempting this on Windows 10 Mobile with AI pkg v1.2.3
You might need to .flush() the TelemetryClient periodically, or before your Background Task exits.
Turns out the key was utilizing the new PersistenceChannel as the Channel for my Telemetry like so:
TelemetryConfiguration.Active.TelemetryChannel = new PersistenceChannel();
then in my bg task:
var c = new TelemetryClient() { InstrumentationKey = <my app insights key> };
c.TrackEvent(evt);
c.Flush();
The documentation for PersistenceChannel shows how it does what AI previously did automatically: cache results to local storage until they're able to be sent to the AI service.
This enabled my background task to fire off and cache its analytics until such time that the user launched the app (and therefore the AI objects got access to the backend service) or the bg task had enough time & muscle to do it itself.

Trying to use the MS Band from an android Service

Im trying to write a simple app for some running friends of mine. Basiclly they run for long distance and along the way they want to use the band to mark a location. Click on a tile on the band, click on a button that says "Mark" They do not want to worry about the application running or not. So I assume app will be closed on Android phones. I got my prototype working, kind-of. The main issue I'm having is app is closed!
BandPendingResult addTile(Activity var1, BandTile var2) throws BandIOException;
Add type first parameter requires an activity. I am trying to build this as a service that when a button on the Band is pressed "wakes" up the service, gets the GPS location and sends it to the server. Activity is not a service.
When the service starts I have tried to remember the activity its bound to, but when the application get closed the activity is killed off and BOOM!
Adding a Tile requires an Activity because it requires user confirmation (i.e. it displays UI). However, you should not be adding the Tile as part of your service; it should be done (just once) as part of the application's setup/settings flow. The service should only be concerned with responding to the Tile events.

HKWorkoutSession isn't keeping app at front of Apple Watch

It has been stated that an app running a HKWorkoutSession will have special privileges over other watchOS 2 apps, so when a user looks at their Apple Watch, it will go to the view showing running a workout rather than the watch face.
Currently, on both my device and simulator, this is not the case. If I start a HKWorkoutSession and then leave for 5 minutes and then interact with either the Apple Watch, or the Watch Simulator, it presents the watch face.
If I then open my app, it appears to have been frozen, rather than terminated (which is what I imagine happens to other apps). As the UI will update when I need receive a response in my query.updateHandler. Also if I set it to provide haptic feedback every time my query.updateHandler receives a new HKQuantitySample it will do so, so the app must be running in the background in some form.
Has anyone else noticed this behaviour, and am I doing anything wrong, or expecting something I shouldn't?
Here is how I start my HKWorkoutSession:
self.workoutSession = HKWorkoutSession(activityType: HKWorkoutActivityType.Other, locationType: HKWorkoutSessionLocationType.Indoor)
self.healthStore.startWorkoutSession(self.workoutSession) {
success, error in
if error != nil {
print("startWorkoutSession \(error)\n")
self.printLabel.setText("startWorkoutSession \(error)")
self.printLabel.setTextColor(UIColor.redColor())
}
We're seeing that too, for the moment we've made sure 'opens last activity' is configured.
When the UI is active we start a dispatch_timer to request and process data in 1 second intervals.
Make sure you do any significant processing using the NSUserProcessInfo method though and pause the dispatch_timers whenever you are no longer active. You'll get crashes otherwise.

Resources