Mats Bryntse
21 December 2015

5 Great Siesta Tips n Tricks For Your Testing Toolbelt

Siesta contains a lot of useful features that you may or may not be aware of. Reading the Siesta docs […]

Siesta contains a lot of useful features that you may or may not be aware of. Reading the Siesta docs is a great way to learn and we also try to post about all the improvements here in the blog. If you keep track of our changelog, you might already be know some of the tips in this post but we’re pretty sure you’ll learn something new so let’s get to it!

1. Accessing the test instance in beforeEach/afterEach hooks

This is a new feature introduced in our latest release. Let’s say you have a large group of application tests in a test file where you want to assert something at the end of every test group. With access to the test instance in your test hooks, you can now easily add assertions to verify certain conditions your care about.

var dataStore = new Ext.data.Store();

t.afterEach(function(t) {
     t.expect(dataStore.getCount()).toBe(0);
     t.selectorNotExists('.form-input-invalid');
});

t.it('Should do this...', function(t) {
});

t.it('Should also do this...', function(t) {
});

t.it('Should maybe not do this...', function(t) {
});

2. Screenshot on a failed test

This has been requested a few times before and might become a supported feature in the future. Until then, here’s how easy it is to take a screenshot of a failed test:

var failCounter = 0;

yourHarness.on('testupdate', function(event, test) {
    if (test.getFailCount() > failCounter) {
        failCounter = test.getFailCount();

        test.screenshot(test.url, function() {});
    }
});

3. Using the custom :textEquals CSS selector

Support for targeting elements via exact string matching was added recently. Before this, Siesta only supported the :contains selector found in the Sizzle engine. Now it’s very easy to target elements that contain an exact string:

StartTest(function (t) {
    document.body.innerHTML = '<a href="somelink.html">foo</a>'

    t.chain(
        { click : 'a:textEquals(foo)' }
    ) 
});

4. Using t.iit to run a single test group

When debugging a single test group in a large test file, always isolate that test group using the t.iit API method.

t.it('Skipped', function (t) {
    // some assertions
});

t.iit('Only this test group will execute', function (t) {
    // some assertions
});

t.it('Skipped too', function (t) {
    // some assertions
});

5. Extending Siesta to find more bugs

Since Siesta is extensible and observable you can do some pretty cool things to find bugs earlier. You can for example override the console.error and console.warn methods to automatically fail your tests (we do this in our own test suites).

harness.on('teststart', function (ev, test) {
    var console = test.global.console;

    if (console) {
        console.error = console.warn = function (msg) {
            test.fail([].join.apply(arguments));
            console.log(msg);
        };
    }
});

You can also do global post-test sanity checks very easily. Here’s another nice snippet from our own vaults, detecting suspended layout components (in case someone forgot to call resumeLayouts):

harness.on('beforetestfinalizeearly', function () {
    var win = this.global;

    if (win.Ext) {
        var Ext = win.Ext;
        var suspendedComponents = this.cq('[isLayoutSuspended]{isLayoutSuspended()}');

        // only report in case of failure
        if (suspendedComponents.length > 0) {
            this.diag('POST TEST SANITY CHECKS');

            this.is(suspendedComponents.length, 0, 'No components found with layouts suspended');

            this.fail('Suspended layouts detected for components', {
                annotation : Ext.Array.map(suspendedComponents, function (cmp) {
                    return (cmp.id + '(' + cmp.xtype + ') ');
                }).join('\r\n')
            });
        }

        if (win.Ext.AbstractComponent.layoutSuspendCount > 0) {
            this.is(win.Ext.AbstractComponent.layoutSuspendCount, 0, 'Layouts should not be suspended globally by accident');
        }
    }
});

Share your tips

What are your favorite testing tricks which find bugs or make you more productive? Please share them with us and the Siesta community. Happy testing! 🙂

Mats Bryntse

Testing Tips 'n tricks