5.19. How to Handle Exceptions Raised in Test Scripts

Some of Squish's functions raise catchable exceptions on failure. We can write our test scripts so that they can catch these exceptions and respond accordingly—for example, by recording a test failure in the test log.

The exception handling mechanism works the same way for each scripting language, no matter what function raised the exception, so we only need to look at one example to see how it is done. To get an exception immediately and suppress the Object Not Found dialog, we set objectNotFoundDebugging to false in testsettings.


def main():
    testSettings.objectNotFoundDebugging = False    
        waitForObject(names.make_Payment_Check_Signed_QCheckBox, 2000)
        test.xpasses("Found the checkbox")
    except LookupError as err:
        test.xfail("Expectedly failed to find the checkbox", str(err))

function main()
    testSettings.objectNotFoundDebugging = false;
    try {
        checkBox = waitForObject(names.makePaymentCheckSignedQCheckBox, 2000);
        test.xpass("Found the checkbox");
    } catch (err) {
        test.xfail("Expectedly failed to find the checkbox", String(err));

sub main
    eval {
        my $checkBox = waitForObject($Names::make_payment_check_signed_qcheckbox, 2000);
        test::xpass("Found the checkbox");
    test::xfail("Expectedly failed to find the checkbox", "$@") if $@;        

def main
    TestSettings.objectNotFoundDebugging = false
        checkBox = waitForObject(Names::Make_Payment_Check_Signed_QCheckBox, 2000)
        Test.xpass("Found the checkbox")
    rescue Squish::LookupError => err
        Test.xfail("Expectedly failed to find the checkbox", String(err))

    sendEvent("QCloseEvent", waitForObject(Names::Make_Payment_MainWindow))

proc main {} {
    startApplication "paymentform"
    testSettings set objectNotFoundDebugging false
    if {[catch {
        set checkBox [waitForObject $names::Make_Payment_Check_Signed_QCheckBox 2000]
        } result]} {
        test xfail "Expectedly failed to find the checkbox" $result
    } else {
        test xpass "Found the checkbox"

The waitForObject function tries to find the specified object. If the object is not accessible—perhaps because it isn't visible—within the timeout period, the function raises a catchable exception. Since we want to demonstrate a thrown exception but still have the test case pass, we call the test.xpass function if the object (a checkbox in this example) is found within the timeout period, and the test.xfail function if the object isn't found and giving the exception (in string form) as the details text.

In most cases and for most languages Squish simply raises the language's base class exception (e.g., Exception in Python and StandardError in Ruby). However, for Python and Ruby, when an object can't be found a more specific LookupError (Squish::LookupError in Ruby) is raised.


In Python, we must use the test.passes function instead of test.pass to avoid a name clash with Python's built-in pass statement. To be consistent with its Python counterpart, we must use test.xpasses in this example.