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():
    startApplication('"' + os.environ["SQUISH_PREFIX"] + '/examples/qt/paymentform/paymentform"')

    testSettings.objectNotFoundDebugging = False
    try:
        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()
{
    startApplication('"' + OS.getenv("SQUISH_PREFIX") + '/examples/qt/paymentform/paymentform"');

    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
{
    startApplication("\"$ENV{'SQUISH_PREFIX'}/examples/qt/paymentform/paymentform\"");

    testSettings->objectNotFoundDebugging(0);
    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
    startApplication("\"#{ENV['SQUISH_PREFIX']}/examples/qt/paymentform/paymentform\"")

    TestSettings.objectNotFoundDebugging = false
    begin
        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))
    end

    sendEvent("QCloseEvent", waitForObject(Names::Make_Payment_MainWindow))
end
proc main {} {
    startApplication "\"$::env(SQUISH_PREFIX)/examples/qt/paymentform/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 Object waitForObject(objectOrName) 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(message) function if the object (a checkbox in this example) is found within the timeout period, and the test.xfail(message) 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.

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