macOS UI Test: Can't find popover in elements tree

I'm almost at the beginning of Swift programming education. Now i'm trying to create UI tests for my app. I can't find popover in application's elements tree. It's statusbar application and popover is the main UI but it's completely missed for testing framework…


Popover created programatically using UI from storyboard. In `AppDelegate.swift` i have this code:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
    let popover = NSPopover()
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        stopAutostarter()

        if let button = statusItem.button {
            button.image = NSImage(named: NSImage.Name("StatusBarButtonImage"))
            button.action = #selector(togglePopover(_:))
        }

        popover.contentViewController = PopoverViewController.freshController()
        popover.behavior = .transient
        popover.setAccessibilityEnabled(true)
        statusItem.button?.setAccessibilityEnabled(true)

        syncIcon()
    }

    @objc func togglePopover(_ sender: Any?) {
        if popover.isShown {
            closePopover(sender: sender)
        } else {
            showPopover(sender: sender)
        }
    }

    func showPopover(sender: Any?) {
        if let button = statusItem.button {
            popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
        }
    }

    func closePopover(sender: Any?) {
        popover.performClose(sender)
    }
}


My `PopoverViewController.swift` (where PopoverViewController.mainView is slightly modified NSView just to return `true` in `acceptsFirstResponder`):

class PopoverViewController: NSViewController {
    @IBOutlet weak var mainView: RespondingView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mainView.setAccessibilityEnabled(true)
        mainView.setAccessibilityRole(.window)
        mainView.setAccessibilityIdentifier("PopoverView")
    }

    static func freshController() -> PopoverViewController {
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        let identifier = NSStoryboard.SceneIdentifier("PopoverViewController")
        
        guard let viewcontroller = storyboard.instantiateController(
                withIdentifier: identifier) as? PopoverViewController else {
            fatalError("Why can't i find PopoverViewController? - Check Main.storyboard")
        }

        return viewcontroller
    }
}


So now, when i try to record clicking buttons placed in popover in UI tests i've got error: "Recorder Service Error: Left Mouse Down: Failed to find matching element"


When i try analyze application - it's just not have my ppover in elements tree. Only main menu bar, touch bar and menu bar with statusbar button Which is not contains popover.

po XCUIApplication()
    t =    91.06s Snapshot accessibility hierarchy for app with pid 62106
    t =    91.16s Snapshot accessibility hierarchy for app with pid 62106
Attributes: Application, pid: 62106, title: 'MyApp', Disabled
Element subtree:
 →Application, 0x600003924000, pid: 62106, title: 'MyApp', Disabled
    MenuBar, 0x6000039240d0, {{0.0, 0.0}, {0.0, 0.0}}
      MenuBarItem, 0x6000039241a0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Apple'
        Menu, 0x600003924270, {{880.0, 8448.0}, {196.0, 250.0}}
          MenuItem, 0x600003924340, {{0.0, 0.0}, {196.0, 19.0}}, title: 'About This Mac'
          MenuItem, 0x600003924410, {{0.0, 0.0}, {0.0, 0.0}}, title: 'System Information…'
          MenuItem, 0x6000039244e0, {{0.0, 19.0}, {196.0, 12.0}}, Disabled
          MenuItem, 0x6000039245b0, {{0.0, 31.0}, {196.0, 19.0}}, title: 'System Preferences…'
          MenuItem, 0x600003924680, {{0.0, 50.0}, {196.0, 19.0}}, title: 'App Store…'
          MenuItem, 0x600003924750, {{0.0, 69.0}, {196.0, 12.0}}, Disabled
          MenuItem, 0x600003924820, {{0.0, 81.0}, {196.0, 19.0}}, title: 'Recent Items'
            Menu, 0x6000039248f0, {{880.0, 1920.0}, {319.0, 532.0}}
              MenuItem, 0x6000039249c0, {{0.0, 0.0}, {319.0, 19.0}}, title: 'Applications', Disabled
              MenuItem, 0x600003924a90, {{0.0, 19.0}, {319.0, 21.0}}, title: 'Accessibility Inspector.app'
              MenuItem, 0x600003924b60, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Show “Accessibility Inspector.app” in Finder'MenuItem, 0x6000039156c0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Show “STDOUT-2018-11-14T01-10-23.log.gz” in Finder'
              MenuItem, 0x600003915790, {{0.0, 470.0}, {319.0, 12.0}}, Disabled
              MenuItem, 0x600003915860, {{0.0, 482.0}, {319.0, 19.0}}, title: 'Servers', Disabled
              MenuItem, 0x600003915930, {{0.0, 501.0}, {319.0, 12.0}}, Disabled
              MenuItem, 0x600003915a00, {{0.0, 513.0}, {319.0, 19.0}}, title: 'Clear Menu'
          MenuItem, 0x600003915ad0, {{0.0, 100.0}, {196.0, 12.0}}, Disabled
          MenuItem, 0x600003915ba0, {{0.0, 112.0}, {196.0, 19.0}}, title: 'Force Quit…'
          MenuItem, 0x600003915c70, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Force Quit MyApp'
          MenuItem, 0x600003915d40, {{0.0, 131.0}, {196.0, 12.0}}, Disabled
          MenuItem, 0x600003915e10, {{0.0, 143.0}, {196.0, 19.0}}, title: 'Sleep'
          MenuItem, 0x600003915ee0, {{0.0, 162.0}, {196.0, 19.0}}, title: 'Restart…'
          MenuItem, 0x600003915fb0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Restart'
          MenuItem, 0x600003916080, {{0.0, 181.0}, {196.0, 19.0}}, title: 'Shut Down…'
          MenuItem, 0x600003916150, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Shut Down'
          MenuItem, 0x600003916220, {{0.0, 200.0}, {196.0, 12.0}}, Disabled
          MenuItem, 0x6000039163c0, {{0.0, 212.0}, {196.0, 19.0}}, title: 'Lock Screen'
          MenuItem, 0x600003916490, {{0.0, 231.0}, {196.0, 19.0}}, title: 'Log Out qnub…'
          MenuItem, 0x600003916560, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Log Out qnub'
      MenuBarItem, 0x600003916630, {{0.0, 0.0}, {0.0, 0.0}}, title: 'MyApp'
        Menu, 0x600003916700, {{880.0, 10368.0}, {187.0, 181.0}}
          MenuItem, 0x6000039167d0, {{0.0, 0.0}, {187.0, 19.0}}, title: 'About MyApp'
          MenuItem, 0x6000039168a0, {{0.0, 19.0}, {187.0, 12.0}}, Disabled
          MenuItem, 0x600003916970, {{0.0, 31.0}, {187.0, 19.0}}, title: 'Preferences…', Disabled
          MenuItem, 0x600003916a40, {{0.0, 50.0}, {187.0, 12.0}}, Disabled
          MenuItem, 0x600003916b10, {{0.0, 62.0}, {187.0, 19.0}}, title: 'Services'
            Menu, 0x600003916be0, {{880.0, 10368.0}, {203.0, 38.0}}
              MenuItem, 0x600003916cb0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Add to iTunes as a Spoken Track'
              MenuItem, 0x600003916d80, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Convert Text to Full Width'
              MenuItem, 0x600003916e50, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Convert Text to Half Width'
              MenuItem, 0x600003916f20, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Convert Text to Simplified Chinese'
              MenuItem, 0x600003916ff0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Convert Text to Traditional Chinese'
              MenuItem, 0x6000039170c0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Make New Sticky Note'
              MenuItem, 0x600003917190, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Open'
              MenuItem, 0x600003917260, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Show in Finder'
              MenuItem, 0x600003917330, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Show Info in Finder'
              MenuItem, 0x600003917400, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Show Map'
              MenuItem, 0x6000039174d0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Import Image'
              MenuItem, 0x6000039175a0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Open with Monosnap'
              MenuItem, 0x600003917670, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Set Desktop Picture'
              MenuItem, 0x600003917740, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Compress using Keka'
              MenuItem, 0x600003917810, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Extract using Keka'
              MenuItem, 0x6000039178e0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Send to Keka'
              MenuItem, 0x6000039179b0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Encode Selected Audio Files'
              MenuItem, 0x600003917b50, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Encode Selected Video Files'
              MenuItem, 0x600003917c20, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Folder Actions Setup…'
              MenuItem, 0x600003917cf0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'New iTerm2 Tab Here'
              MenuItem, 0x600003917dc0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'New iTerm2 Window Here'
              MenuItem, 0x600003917e90, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Upload with Monosnap'
              MenuItem, 0x600003910270, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Look Up in Dictionary'
              MenuItem, 0x600003910340, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Search With Google'
              MenuItem, 0x600003913400, {{0.0, 0.0}, {0.0, 0.0}}, title: 'New Email To Address'
              MenuItem, 0x6000039134d0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'New Email With Selection'
              MenuItem, 0x600003913e90, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Add to Reading List'
              MenuItem, 0x600003913dc0, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Open URL'
              MenuItem, 0x600003913cf0, {{0.0, 0.0}, {203.0, 19.0}}, title: 'No Services Apply', Disabled
              MenuItem, 0x6000039138e0, {{0.0, 19.0}, {203.0, 19.0}}, title: 'Services Preferences…'
          MenuItem, 0x6000039139b0, {{0.0, 81.0}, {187.0, 12.0}}, Disabled
          MenuItem, 0x600003913c20, {{0.0, 93.0}, {187.0, 19.0}}, title: 'Hide MyApp', Disabled
          MenuItem, 0x600003913b50, {{0.0, 112.0}, {187.0, 19.0}}, title: 'Hide Others'
          MenuItem, 0x600003913a80, {{0.0, 131.0}, {187.0, 19.0}}, title: 'Show All', Disabled
          MenuItem, 0x600003913810, {{0.0, 150.0}, {187.0, 12.0}}, Disabled
          MenuItem, 0x600003913740, {{0.0, 162.0}, {187.0, 19.0}}, title: 'Quit MyApp'
      MenuBarItem, 0x600003913670, {{0.0, 0.0}, {0.0, 0.0}}, title: 'Edit'
        Menu, 0x6000039135a0, {{880.0, 10368.0}, {249.0, 321.0}}
          MenuItem, 0x6000039130c0, {{0.0, 0.0}, {249.0, 19.0}}, title: 'Undo', Disabled
          MenuItem, 0x600003913330, {{0.0, 19.0}, {249.0, 19.0}}, title: 'Redo', Disabled
          MenuItem, 0x600003913260, {{0.0, 38.0}, {249.0, 12.0}}, Disabled
          MenuItem, 0x600003913190, {{0.0, 50.0}, {249.0, 19.0}}, title: 'Cut', Disabled
          MenuItem, 0x600003912ff0, {{0.0, 69.0}, {249.0, 19.0}}, title: 'Copy', Disabled
          MenuItem, 0x600003912f20, {{0.0, 88.0}, {249.0, 19.0}}, title: 'Paste', Disabled
          MenuItem, 0x600003912e50, {{0.0, 107.0}, {249.0, 19.0}}, title: 'Paste and Match Style', Disabled
          MenuItem, 0x600003912d80, {{0.0, 126.0}, {249.0, 19.0}}, title: 'Delete', Disabled
          MenuItem, 0x600003912cb0, {{0.0, 145.0}, {249.0, 19.0}}, title: 'Select All', Disabled
          MenuItem, 0x600003912be0, {{0.0, 164.0}, {249.0, 12.0}}, Disabled
          MenuItem, 0x600003912b10, {{0.0, 176.0}, {249.0, 19.0}}, title: 'Find'
            Menu, 0x600003912a40, {{880.0, 7680.0}, {216.0, 114.0}}
              MenuItem, 0x600003912970, {{0.0, 0.0}, {216.0, 19.0}}, title: 'Find…', Disabled
              MenuItem, 0x6000039128a0, {{0.0, 19.0}, {216.0, 19.0}}, title: 'Find and Replace…', Disabled
              MenuItem, 0x6000039127d0, {{0.0, 38.0}, {216.0, 19.0}}, title: 'Find Next', Disabled
              MenuItem, 0x600003912700, {{0.0, 57.0}, {216.0, 19.0}}, title: 'Find Previous', Disabled
              MenuItem, 0x600003912630, {{0.0, 76.0}, {216.0, 19.0}}, title: 'Use Selection for Find', Disabled
              MenuItem, 0x600003912560, {{0.0, 95.0}, {216.0, 19.0}}, title: 'Jump to Selection', Disabled
          MenuItem, 0x600003912490, {{0.0, 195.0}, {249.0, 19.0}}, title: 'Spelling and Grammar'
            Menu, 0x6000039123c0, {{880.0, 7680.0}, {256.0, 107.0}}
              MenuItem, 0x6000039122f0, {{0.0, 0.0}, {256.0, 19.0}}, title: 'Show Spelling and Grammar'
              MenuItem, 0x600003912220, {{0.0, 19.0}, {256.0, 19.0}}, title: 'Check Document Now', Disabled
              MenuItem, 0x600003912150, {{0.0, 38.0}, {256.0, 12.0}}, Disabled
              MenuItem, 0x600003912080, {{0.0, 50.0}, {256.0, 19.0}}, title: 'Check Spelling While Typing', Disabled
              MenuItem, 0x600003911fb0, {{0.0, 69.0}, {256.0, 19.0}}, title: 'Check Grammar With Spelling', Disabled
              MenuItem, 0x600003911ee0, {{0.0, 88.0}, {256.0, 19.0}}, title: 'Correct Spelling Automatically', Disabled
          MenuItem, 0x600003911e10, {{0.0, 214.0}, {249.0, 19.0}}, title: 'Substitutions'
            Menu, 0x600003911d40, {{880.0, 8448.0}, {165.0, 145.0}}
              MenuItem, 0x600003911c70, {{0.0, 0.0}, {165.0, 19.0}}, title: 'Show Substitutions', Disabled
              MenuItem, 0x600003911ba0, {{0.0, 19.0}, {165.0, 12.0}}, Disabled
              MenuItem, 0x600003911ad0, {{0.0, 31.0}, {165.0, 19.0}}, title: 'Smart Copy/Paste', Disabled
              MenuItem, 0x600003911a00, {{0.0, 50.0}, {165.0, 19.0}}, title: 'Smart Quotes', Disabled
              MenuItem, 0x600003911930, {{0.0, 69.0}, {165.0, 19.0}}, title: 'Smart Dashes', Disabled
              MenuItem, 0x600003910f70, {{0.0, 88.0}, {165.0, 19.0}}, title: 'Smart Links', Disabled
              MenuItem, 0x600003911860, {{0.0, 107.0}, {165.0, 19.0}}, title: 'Data Detectors', Disabled
              MenuItem, 0x600003911790, {{0.0, 126.0}, {165.0, 19.0}}, title: 'Text Replacement', Disabled
          MenuItem, 0x6000039116c0, {{0.0, 233.0}, {249.0, 19.0}}, title: 'Transformations'
            Menu, 0x6000039115f0, {{880.0, 1920.0}, {155.0, 57.0}}
              MenuItem, 0x600003911520, {{0.0, 0.0}, {155.0, 19.0}}, title: 'Make Upper Case', Disabled
              MenuItem, 0x600003911450, {{0.0, 19.0}, {155.0, 19.0}}, title: 'Make Lower Case', Disabled
              MenuItem, 0x600003911380, {{0.0, 38.0}, {155.0, 19.0}}, title: 'Capitalize', Disabled
          MenuItem, 0x6000039112b0, {{0.0, 252.0}, {249.0, 19.0}}, title: 'Speech'
            Menu, 0x6000039111e0, {{880.0, 10368.0}, {136.0, 38.0}}
              MenuItem, 0x600003911110, {{0.0, 0.0}, {136.0, 19.0}}, title: 'Start Speaking', Disabled
              MenuItem, 0x600003910ea0, {{0.0, 19.0}, {136.0, 19.0}}, title: 'Stop Speaking', Disabled
          MenuItem, 0x600003910dd0, {{0.0, 271.0}, {249.0, 12.0}}, Disabled
          MenuItem, 0x600003910d00, {{0.0, 283.0}, {249.0, 19.0}}, title: 'Start Dictation…'
          MenuItem, 0x600003910b60, {{0.0, 302.0}, {249.0, 19.0}}, title: 'Emoji & Symbols'
    TouchBar, 0x600003910a90, {{80.0, 0.0}, {685.0, 30.0}}, Disabled
    MenuBar, 0x6000039109c0, {{935.0, 0.0}, {30.0, 22.0}}
      StatusItem, 0x6000039108f0, {{935.0, 0.0}, {30.0, 22.0}}
      StatusItem, 0x600003910820, {{1372.0, -1440.0}, {30.0, 22.0}}, Disabled
Path to element:
 →Application, pid: 62106, title: 'MyApp', Disabled
Query chain:
 →Find: Target Application 'my.app.MyApp'
  Output: {
    Application, pid: 62106, title: 'MyApp', Disabled  }

So trying to assert

XCTAssertTrue(XCUIApplication().descendants(matching: .other).matching(
    identifier: "PopoverView").element.exists)


is alwais fail.


How to fix it to get working UI testing with popover?

  • I have exactly the same problem 😞

Add a Comment

Replies

I just solved it! 🙌🏻

NSApp.setAccessibilityChildren([view])

I have an app that has content from the menu bar as well as a primary window.

I can't see it working with this solution.