JavaScript for automation in OSX Yosemite

670 views Asked by At

Can we send a click event to a button inside a webview in Cocoa app using JavaScript?

I am trying to use the script editor under utilities (Yosemite) to record but unfortunately not able to record any events inside the webview.

I tried using the sample code under UI automation section provided in the Apple documentation with the testapp (cocoa app with webview) at https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/index.html.

TestApp = Application('TestApp')    
TestApp.activate()

delay(1)

SystemEvents = Application('System Events')

TestApp = SystemEvents.processes['TestApp']

TestApp.document.getElementById('testid').click();      // stuck at this last line not sure if I can    
//even call the document object in this way. Getting error undefined variable document.
1

There are 1 answers

0
Ben Zotto On BEST ANSWER

You're trying to use browser/DOM Javascript to access native app UI elements. Although this environment uses Javascript like a browser does, the underlying object model is not the DOM you're used to seeing on a web page. That's why you're seeing that document is undefined.

As implied by the little snippet in the "UI Automation" section of the docs, you need to access the window and button objects. The exact path you'll use depends on your TestApp, of course, but it might look something like:

TestApp.windows[0].buttons[0].click()

(It is probably also possible to scrobble through these arrays by control ID using whose, or the like, but don't have experience with that.)