installEventHandler
installEventHandler(eventName, handlerFunctionNameOrReference)
This function installs a global event handler. The script function named or referenced in handlerFunctionNameOrReference
, will be called when an event of the eventName
type occurs.
The eventName
can be the name of any of the following event types:
- Crash: This event occurs if the AUT crashes.
- DialogOpened: This event occurs when a Dialog is opened.
- MessageBoxOpened: This event occurs when a native SWT message box is opened.
- Timeout: This event occurs when the Squish response timeout is reached.
The function named in handlerFunctionName
is called with a single argument—either the object on which the event occurred, or in the case of MessageBoxOpened
, the text of the message box.
Note: In Python scripts, you can specify the callback function to invoke by passing an actual function or a lambda function.
Using SWT's MessageBoxOpened
as an example, an event handler might look like this:
import names import os def handleMessageBox(notUsedText): test.log("MessageBox opened: '{}' - '{}'".format(lastAlert.title, lastAlert.text)) if lastAlert.text.startswith("Save"): closeMessageBox(waitForObject(names.sWT), SWT.NO) else: closeMessageBox(waitForObject(names.sWT), SWT.YES) def main(): fn = os.path.join(os.getenv("SQUISH_PREFIX"), "examples/java/addressbook_swt/AddressBookSWT.jar") startApplication('"{}"'.format(fn)) installEventHandler("MessageBoxOpened", handleMessageBox) activateItem(waitForObjectItem(names.address_Book_Menu, "File"))
import * as names from 'names.js'; function messageBoxHandler(text) { test.log("MessageBox opened [" + lastAlert.title + ": " + lastAlert.text + "]") if (text.indexOf("Save") == 0) closeMessageBox(waitForObject(names.sWT), SWT.NO) else closeMessageBox(waitForObject(names.sWT), SWT.YES) } function main() { startApplication('"' + OS.getenv("SQUISH_PREFIX") + '/examples/java/addressbook_swt/AddressBookSWT.jar"'); installEventHandler("MessageBoxOpened", messageBoxHandler) activateItem(waitForObjectItem(names.addressBookMenu, "File"));
require 'names.pl'; sub messageBoxHandler() { my ($text) = @_; test::log ("MessageBox Opened: [" . lastAlert->title() . ": " . lastAlert->text() . "]"); if ($text =~ "^Save.*") { closeMessageBox(waitForObject($Names::swt), SWT->NO); } else { closeMessageBox(waitForObject($Names::swt), SWT->YES); } } sub main() { startApplication("\"$ENV{'SQUISH_PREFIX'}/examples/java/addressbook_swt/AddressBookSWT.jar\""); installEventHandler("MessageBoxOpened", "messageBoxHandler"); activateItem(waitForObjectItem($Names::address_book_menu, "File"));
require 'squish' require 'names' include Squish def HandleMessageBox(text) Test.log("MessageBox [" + LC::LastAlert.title + ": " + LC::LastAlert.text + "]") end def main fn = File.join(ENV['SQUISH_PREFIX'], "examples/java/addressbook_swt/AddressBookSWT.jar") startApplication('"' + fn + '"'); installEventHandler("MessageBoxOpened", "HandleMessageBox") activateItem(waitForObjectItem(Names::Address_Book_Menu, "File"))
source [findFile "scripts" "names.tcl"] proc handleMessageBox {text} { test log [concat "Text: " $text] test log [concat "LastAlert (" [property get lastAlert title] ": " [property get lastAlert text] ")"] } proc main {} { startApplication "\"$::env(SQUISH_PREFIX)/examples/java/addressbook_swt/AddressBookSWT.jar\"" installEventHandler "MessageBoxOpened" "handleMessageBox" invoke activateItem [waitForObjectItem $names::Address_Book_Menu "File"]
Note: The installEventHandler
function will only work if it is called after the AUT has been started. For example, by using the ApplicationContext startApplication(autName) function.
© 2024 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.
The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners.