Dismissing multiple alerts XCUITesing - swift

I am am writing my XCUITests for my app, and currently struggling in finding the best solutions to dismiss my apps alerts. I have two sets of alerts, one is a notifications alert which pops up at the beginning of a fresh install, the second is a location alert when I navigate to the nearby tab on my app after a fresh install. I have tried using
let alert = app.alerts["\u{201c}VoucherCodes\u{201d} Would Like to Send You Notifcations"].collectionViews.buttons["OK"]
if alert.exists{
alert.tap()
}
but no success, I have also tried using a systemAlertMonitorTokenin my my setUp()
systemAlertMonitorToken = addUIInterruptionMonitorWithDescription(systemAlertHandlerDescription) { (alert) -> Bool in
if alert.buttons.matchingIdentifier("OK").count > 0 {
alert.buttons["OK"].tap()
return true
} else {
return false
}
}
Does anyone have any suggestions or point out where I am going wrong so I can fix this, thanks.

You might find these two questions and their answers to be illuminating:
Xcode 7 UI Testing: Dismiss Push and Location alerts
Xcode 7 UI Testing: how to dismiss a series of system alerts in code

Related

MacOS Cocoa - keep dock icon open when all windows are closed

my application is a listener service that is normally idle, waiting for incoming messages over the network, or for instructions from the user via the Dock icon menus.
however something keeps closing or hiding the Dock icon, unless i keep a window open.
i am working in XCode Swift, with Cocoa with Storyboards, and am developing for MacOS 10.13
i started a new project as above, and edited AppDelegate.swift as follows:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
print( "app did finish launching" )
// Insert code here to initialize your application
for window in NSApp.windows {
print( window.debugDescription )
window.close()
}
DispatchQueue.global(qos: .userInitiated).async {
var seconds = 0
while true {
sleep( 1 )
print( ( seconds % 2 ) == 0 ? "tick" : "tock", seconds )
seconds += 1
}
}
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
print( "app should terminate after last window - set regular policy" )
sender.setActivationPolicy( .regular )
return false
}
}
with no other changes, the application builds and runs, closing one window, however;
1 - the dock icon vanishes as soon as the application loses focus.
2 - after about sixty seconds the application slows down, and the sleep call is taking much longer than 1 second to return.
i need to prevent both of these behaviours, from which two questions follow:
a - where does Apple document these behaviours?
b - what is the correct way to control these behaviours?
in my experience it seems that most MacOS apps stay open in the dock, even though all of their windows are closed. for example Finder, Terminal, Safari, Notes, etc, etc.
what am i not understanding?
edited - i found this answer a week ago
Mac app disappears when window is closed then another app is selected
and i added both of parameters to my application Info, however it would seem i made an error, because i checked again today, and found they were both set to YES.
setting them both to NO appears to answer my question, my application is now working properly, and i will continue to test.
thank you, very much appreciated.

Is there a way to remove the Push Notification Pop Up confirmation message in Fastlane screenshots?

I'm creating snapshots using Fastlane. As my application uses "Push Notifications", when the app is launched it always displays to the user the pop up that requests the authorization to send this kind of messages. There is a method that is called in the AppDelegate UIApplication.shared.registerForRemoteNotifications(), this method is the one that "shows" the pop up to the user.
I have tried something like:
#if !DEBUG
UIApplication.shared.registerForRemoteNotifications()
#endif
#if TARGET_IPHONE_SIMULATOR
UIApplication.shared.registerForRemoteNotifications()
#endif
I also tried to set a Global variable but it hasn't been possible to find a place where to set this variable, because it never works
I always get the same behavior.
I would expect that the first time I run the test in the simulator, it does not display the message.
Thanks.
I found an easy way to avoid this screenshot.
Before the screenshot is taken, I simply press the button "Allow"
let systemAlerts = XCUIApplication(bundleIdentifier: "com.apple.springboard").alerts
if systemAlerts.buttons["Allow"].exists {
systemAlerts.buttons["Allow"].tap()
}
Simple an easy :)

After hiding NSRunningApplications some automatically unhide

Im using the following code to hide all the running applications whose activationPolicy == .regular and if the bundle identifier is different from my app.
NSWorkspace.shared.runningApplications
.filter { runningApps.contains($0.bundleIdentifier) }
.forEach { item in
item.hide()
}
Right after hiding them some just automatically unhide, apps that usually have a search bar or first responder textview like telegram, finder, messages. It's random and sometimes it reappears, sometimes not...
I also tried with NSWorkspace.shared.hideOtherApplications() but it does the same
macOS 10.14.3
Any idea on what should I do before hiding?

print not working in Swift 3 extensions

I'm new at Swift 3 and I try to make a print("Test") in a Widget extension.
I tried the same code in ViewController.swift and It works ok. I don't know why it works there but it doesn't on TodayViewController.swift. I can't access to objects too.
func loadData() {
let query = PFQuery(className: "Noticias")
query.whereKey("titulo", equalTo:"Es Navidad")
query.findObjectsInBackground(block: { (objects : [PFObject]?, error: Error?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
for object in objects {
print(object.objectId!)
}
}
} else {
// Log details of the failure
print("bad day homie")
print(error!)
}
})
}
I attach I picture to see it clearly. If I try to print on the file marked as Work, it works. But if I try it on the file marked ad NO, it doesn't.
It is extremely difficult to retrieve print messages from an extension. The problem is that it's an extension! It isn't running in your app, so it doesn't arrive at your console. Sometimes I find you can solve this problem by switching the debugged process in the Debug Bar at the top of the debug area (at the bottom of the screen, not shown in your screen shot), but at other times this doesn't work.
I'll illustrate a possible technique that seems to be pretty reliable. Look at this screen shot:
"Expand" is an action extension. But my containing app is called "bk2ch13...". So how will I ever manage to pause at the breakpoint shown at the right, which is in the action extension? This is what I do.
First, with the screen as shown above, I build and run my containing app.
Then, I switch the target to the action extension:
Now I build and run again. But now I am trying to run an action extension, which you can't do, so Xcode asks me what app to run:
So I choose "bk2ch13...". So now we are running my host app again, but we are debugging the extension. So I use my host app to exercise the extension, and sure enough, we pause at the breakpoints and print statements arrive into the console.
Note, in that screen shot, how the debug bar clearly shows that we are talking to the extension, not the host app.

OSX/Swift Get Frontmost Running Application

I'm new to swift and osx programming in general, and I need help with one thing that I can't seem to find an answer for. I'm trying to get the frontmost application running on a computer through an app. Right now I'm able to detect when a target application opens, but not when it is in front of all other windows or when it goes into the background.
What I have so far is:
var workspace = NSWorkspace.sharedWorkspace()
var applications = workspace.runningApplications
for app in applications {
let x = "LolClient"
if app.localizedName == x {
println("League is open")
}
}
That will just tell me when a target app opens. I just need to identify what app is in front of all others... basically which one is recieving keystrokes etc. What code would I need to do this? Thank you in advance.
I think you want NSWorkspace.shared.frontmostApplication.
NSWorkspace.shared.frontmostApplication
For the name of the app:
NSWorkspace.shared.frontmostApplication?.localizedName