daveenguyen / atomacos

Automated Testing on macOS
https://daveenguyen.github.io/atomacos/
GNU General Public License v2.0
47 stars 13 forks source link

How do you wait for the app's UI to be ready? #12

Closed steven-joruk closed 4 years ago

steven-joruk commented 4 years ago

Hey, I found your project yesterday after seeing how little maintainance atomac is receiving now, thanks for sharing your work

I can launch a bundle and get its app reference, but it isn't immediately usable because its UI may not have been set up yet.

I'd exect to be able to do this:

    app = atomacos.getAppRefByBundleId("com.examle.app")
    app.waitForWindowToAppear("Example")

Or alternatively app.waitForCreation()

But I see this error message for both:

        File "/usr/local/lib/python3.7/site-packages/atomacos/_mixin/_wait.py", line 29, in waitForCreation
          return self.waitFor(timeout, notification)
        File "/usr/local/lib/python3.7/site-packages/atomacos/_mixin/_wait.py", line 21, in waitFor
          timeout=timeout,
        File "/usr/local/lib/python3.7/site-packages/atomacos/_notification.py", line 53, in wait_for
          observer, self.ref.ref, notification, id(self.ref.ref)
        File "/usr/local/lib/python3.7/site-packages/atomacos/_macos.py", line 67, in PAXObserverAddNotification
          errors.check_ax_error(error_code, error_messages)
        File "/usr/local/lib/python3.7/site-packages/atomacos/errors.py", line 118, in check_ax_error
          raise AXErrorFactory(error_code)(error_message)
      atomacos.errors.AXErrorCannotComplete: The function cannot complete because messaging has failed in some way.

I can work around it by just sleeping, but I expect to have hundreds of app launches in my testing and it'll add way too much time.

daveenguyen commented 4 years ago

I personally don't know how the waitFor methods are intended to be used.

My best guess is this something like this where you would need the app reference before the window appears https://github.com/daveenguyen/atomacos/blob/master/tests/test_wait_for_methods.py#L4

The waitFor method blocks while waiting for the event to occur. I think there would be a race condition if you are launching the app, grabbing the reference then waiting because the event might've already occurred before you started waiting.

At work we have a helper method to do something similar to this https://github.com/daveenguyen/atomacos/blob/master/tests/test_input.py#L50-L55

As for the error message, going to be honest I don't know why the error occurs.

They are pretty much from apple https://developer.apple.com/documentation/applicationservices/1462089-axobserveraddnotification

steven-joruk commented 4 years ago

I've worked around it for now by polling with a timeout until the returned app reference and windows are valid.

I think the waitFor methods were also partially broken in the original atomac - at least I didn't see them return earlier than the specified timeout.

I expect I'll run in to the problem again soon for other test targets, and if so I'll try to fix the underlying issue.

Thanks 😄