Appium Mac2 - failing randomly

Hi ,

Appium is failing randomly when running tests, I am using Mac2Driver for mac app automation.
It is failing with this error

[WebDriverAgentMac] [xcodebuild] /usr/local/lib/node_modules/appium/node_modules/appium-mac2-driver/WebDriverAgentMac/WebDriverAgentRunner/WebDriverAgentRunner.m:48: error: -[UITestingUITests testRunner] : Failed to activate application 'com.map.mac at /Users/aznetvpnuser/myagent/_work/1/s/platform/macosx/export/Map Mac Client.app' (current state: Running Background)

Using appium - 1.22.2 version
Any pointers on how to fix this. Thank you in advance

Hi, Any pointer please. Sharing the whole error

[debug] [WebDriverAgentMac] 2022-02-09 19:49:03.685838+0530 WebDriverAgentRunner-Runner[96181:30835445] Launching macOS application via NSWorkspace while sandboxed. Launch arguments and environment variables may be dropped.

[debug] [WebDriverAgentMac] [xcodebuild] /usr/local/lib/node_modules/appium/node_modules/appium-mac2-driver/WebDriverAgentMac/WebDriverAgentRunner/WebDriverAgentRunner.m:48: error: -[UITestingUITests testRunner] : Failed to activate application 'com.map.mac at /Users/aznetvpnuser/myagent/_work/1/s/platform/macosx/export/Map Mac Client.app' (current state: Running Background)

[debug] [WebDriverAgentMac] [xcodebuild] t = 61.57s Setting up automation session

[debug] [WD Proxy] Got response with status 200: {"value":{"sessionId":"6157BD25-E771-48C5-8393-EED88CCA83CD","capabilities":{"CFBundleIdentifier":"com.map.mac"}},"sessionId":"6157BD25-E771-48C5-8393-EED88CCA83CD"}

[WD Proxy] Determined the downstream protocol as 'W3C'

[Appium] New Mac2Driver session created successfully, session d60a3ce5-6c4f-4dc9-93b1-20dadeace04c added to master session list

[debug] [BaseDriver] Event 'newSessionStarted' logged at 1644416403997 (19:50:03 GMT+0530 (India Standard Time))

[debug] [W3C (d60a3ce5)] Cached the protocol value 'W3C' for the new session d60a3ce5-6c4f-4dc9-93b1-20dadeace04c

[debug] [W3C (d60a3ce5)] Responding to client with driver.createSession() result: {"capabilities":{"platformName":"mac","showServerLogs":true,"automationName":"mac2","bundleId":"com.map.mac"}}

[HTTP] <-- POST /wd/hub/session 200 66840 ms - 185

[HTTP]

[debug] [WebDriverAgentMac] [xcodebuild] t = 61.61s Wait for com.map.mac to idle

[debug] [WebDriverAgentMac] t = 61.61s Tear Down

Feb 09, 2022 7:50:04 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0

INFO: Detected dialect: W3C

[HTTP] --> POST /wd/hub/session/d60a3ce5-6c4f-4dc9-93b1-20dadeace04c/elements

[HTTP] {"using":"accessibility id","value":"profile"}

[debug] [W3C (d60a3ce5)] Calling AppiumDriver.findElements() with args: ["accessibility id","profile","d60a3ce5-6c4f-4dc9-93b1-20dadeace04c"]

[debug] [BaseDriver] Valid locator strategies for this request: id, name, accessibility id, xpath, class name, -ios predicate string, predicate string, -ios class chain, class chain

[debug] [WD Proxy] Matched '/elements' to command name 'findElements'

[debug] [WD Proxy] Proxying [POST /elements] to [POST[ http://127.0.0.1:10100/session/6157BD25-E771-48C5-8393-EED88CCA83CD/elements](http://127.0.0.1:10100/session/6157BD25-E771-48C5-8393-EED88CCA83CD/elements)] with body: {"using":"accessibility id","value":"profile"}

[debug] [WebDriverAgentMac] [xcodebuild] t = 61.73s Find the Application 'com.map.mac'

[debug] [WebDriverAgentMac] [xcodebuild] t = 61.85s Find the Application 'com.map.mac'

[debug] [WebDriverAgentMac] [xcodebuild] t = 61.89s Get all elements bound by accessibility element for: Elements matching predicate '"profile" IN identifiers'

[debug] [WD Proxy] Got response with status 200: {"value":[],"sessionId":"6157BD25-E771-48C5-8393-EED88CCA83CD"}

[debug] [W3C (d60a3ce5)] Responding to client with driver.findElements() result: []

[HTTP] <-- POST /wd/hub/session/d60a3ce5-6c4f-4dc9-93b1-20dadeace04c/elements 200 264 ms - 12

[HTTP]

[debug] [WebDriverAgentMac] [xcodebuild] Test Case '-[UITestingUITests testRunner]' failed (61.967 seconds).

[debug] [WebDriverAgentMac] Test Suite 'UITestingUITests' failed at 2022-02-09 19:50:04.343.

[debug] [WebDriverAgentMac] Executed 1 test, with 1 failure (0 unexpected) in 61.967 (61.970) seconds

[debug] [WebDriverAgentMac] Test Suite 'WebDriverAgentRunner.xctest' failed at 2022-02-09 19:50:04.343.

[debug] [WebDriverAgentMac] Executed 1 test, with 1 failure (0 unexpected) in 61.967 (61.970) seconds

[debug] [WebDriverAgentMac] Test Suite 'All tests' failed at 2022-02-09 19:50:04.343.

[debug] [WebDriverAgentMac] Executed 1 test, with 1 failure (0 unexpected) in 61.967 (61.972) seconds

Figured one thing out, the tests are failing everytime when device is locked.
Need to figure out a way to unlock device before running tests.

You could try https://ss64.com/osx/caffeinate.html to prevent macOS going to sleep