How to check if new scene is created in Xcode UI Testing? - swift

The app is a simple editor app and it supports multiple windows on iPad and Mac Catalyst.
A new window can be opened by New toolbar item or New menu on the Mac menubar.
I'm writing UI testing and not for sure how to check new scene is created.
func testNewEditor() throws {
let app = XCUIApplication()
app.launch()
#if targetEnvironment(macCatalyst)
let menuBarsQuery = XCUIApplication().menuBars
menuBarsQuery.menuBarItems["File"].click()
menuBarsQuery.menuItems["new_editor"].click()
// check new scene is created?
#endif
}
Thanks in advance.

It seems working.
app.children(matching: .window).count

Related

(half solve)Why copy file reference to clipboard programmatically only working for system folder in MacOS?

Solution
add this code pasteboard.data(forType: .fileURL) after pasteboard.writeObjects(emptyArray), it will work.
But I don't know why?
System Verison: MacOS Ventura 13.0
copy.swift
#!/usr/bin/swift
import Cocoa
private func copyToClipBoard(path :String) {
let pasteboard = NSPasteboard.general
var emptyArray = [NSPasteboardWriting]()
emptyArray.append(NSURL(fileURLWithPath: path))
pasteboard.clearContents()
pasteboard.writeObjects(emptyArray)
}
copyToClipBoard(path: CommandLine.arguments[1])
If I run swift copy.swift /Users/USER/Downloads/FILE, paste to chat window for third-party applications is working.
If I run swift copy.swift /Users/USER/tmp/FILE, cannot paste to chat window for third-party applications, application occur no permission error, I already give full access disk to this application.
What's the difference between Downloads and tmp?
How can I make copy file reference programmatically working for any folder?

Tapping system alert using EarlGrey 2.0 & Swift

I'm trying to create UI tests for my app using EarlGrey 2.0 framework while using Swift language for those tests. However, I can't find a solution for tapping on a system alert, although EG 2.0 should support them. To be more specific, it is the native location iOS permission dialog when the app launches. Has anyone done this already? Any ideas? Thank you.
Here's the full code of a test accepting native system alert in Swift
func testExample() {
let app = XCUIApplication()
app.launch()
XCTAssertTrue(grey_wait(forAlertVisibility: true, withTimeout: 2))
XCTAssertTrue(grey_acceptSystemDialogWithError(nil))
XCTAssertTrue(grey_wait(forAlertVisibility: false, withTimeout: 1))
}

How to bring another cocoa application window to front

I have developed a Mac OS app. There is a main window app and a menubar helper app in the same bundle.
There is a menu item of helper app called "Show main app". The helper app should launch the main app if it is not launched after i click the menu item. And the helper app should bring the main app to front if it is hidden in dock or its main window is closed.
I know how to launch the main app. But I have no idea how to implement the re-active function. I used the code like below.
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: "com.xxx.yyy")
if let mainApp = apps.first {
mainApp.activate(options: [ .activateIgnoringOtherApps ])
}
It seems activate method does nothing when the main app is just launched and hide in dock. I noticed the main app is actually activated, because the main menu bar has changed to main app's main menu. But the "applicationShouldHandleReopen" method of its AppDelegate class is not called. So the main window can't be ordered front.
How can I make it works?
No matter the target application is launched or not, just call
NSWorkspace.shared().launchApplication("xxxx")
It works.

Programmatically minimize all windows or besides my Cocoa application?

I haven't been able to find anything about this in Swift. Is there a way to programmatically make my application minimize all other windows open in the background, or even just minimize Safari? I basically want my application to run against the desktop, without any clutter in the background. Is there a way to programmatically do this for a Cocoa app? I'm pretty new to swift, so any help would be appreciated.
You can use api on NSWorkspace which allows you to hide all visible app in the background.
You can find more about NSWorkspace here: link
Hides all applications other than the sender. This method must be called from your app’s main thread.
NSWorkspace.shared().hideOtherApplications()
If you only want to hide Safari,
let appPath = NSWorkspace.shared().fullPath(forApplication: "Safari")
let identifier = Bundle.init(path: appPath!)?.bundleIdentifier
if let bundleID = identifier {
let runningApps = NSRunningApplication.runningApplications(withBundleIdentifier:bundleID )
if !runningApps.isEmpty {
runningApps.first?.hide()
}
}

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