How can I dismiss/hide OSX notifications in XCUITests?
Asked Answered
H

1

-1

I have these notifications (on CI machine):

enter image description here

That "pollute" my screenshots of the app that I'm testing using XCUITests. How can I dismiss or hide them during the test?

I've seen some answers for iOS (like dragging on XCUIApplication(bundleIdentifier: "com.apple.springboard")), but the same things do not seem to work for Mac as springboard doesn't exists on Mac.

Hurtado answered 21/9, 2021 at 15:6 Comment(8)
Can you just take window-only screenshots, instead?Footwear
No, it looks like XCUIElement.screenshot just takes a screenshot of everything on screen and then clips it to the target element.Hurtado
Interesting, that's disappointing, given how good the usual window screenshotting capabilities of macOS are (e.g. you can screenshot a window that's partially occluded, with a transparent background, and still get a perfect shot of it). Would turning on do-not-disturb hide those notifications?Footwear
That's kinda what I'm trying to figure out - how to hide/dismiss those notifications as part of the test, i.e. in an automated way.Hurtado
You didn't answer my question, have you tried turning on DND? apple.stackexchange.com/questions/145487/…Footwear
How are you taking the screenshot? Can you share that code?Janis
@Footwear thanks! it was the solution, I just had to find the script that works on Big Sur as most answers don't.Hurtado
@Janis XCUIElement implements XCUIScreenshotProviding, which has screenshot method: developer.apple.com/documentation/xctest/…. You can also call it on XCUIScreen too.Hurtado
H
0

The solution was to enable "Do Not Disturb" mode from the terminal as that hides all notifications even the ones that you received before enabling it.

I used this script:

brew install vitorgalvao/tiny-scripts/calm-notifications
calm-notifications on

Taken from this answer: https://github.com/sindresorhus/do-not-disturb/issues/9#issuecomment-765083970

There are plenty of other answers on how to enable the "Do Not Disturb" mode from terminal, but most of them do not work on OSX Big Sur.

Hurtado answered 22/9, 2021 at 16:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.