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.

Python

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

function main()
{
    startApplication("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));
    }
Perl

sub main
{
    startApplication("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 $@;        
Ruby

def main
    startApplication("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
end
Tcl

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.

[Note]Python-specific

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.