Search code examples
jasmineautomated-testswebdriver-iowdio-jasmine

Webdriver.IO: How do I run a specific 'it' statement in Jasmine using WDIO


I am trying to pull out a smoke suite from my regression suite written using the Jasmine framework (wdio-jasmine-framework).

Is it possible to just add a tag on specific testcases in Jasmine?


Solution

  • If I remember correctly from my Jasmine/Mocha days, there were several ways to achieve this. I'll detail a few, but I'm sure there might be some others too. Use the one that's best for you.

    1. Use the it.skip() statement inside a conditional operator expression to define the state of a test-case (e.g: in the case of a smokeRun, skip the non-smoke tests using: (smokeRun ? it.skip : it)('not a smoke test', () => { // > do smth here < });).

    Here is an extended example:

    // Reading the smokeRun state from a system variable:
    const smokeRun = (process.env.SMOKE ? true : false);
    
    describe('checkboxes testsuite', function () {
    
        // > this IS a smoke test! < //
        it('#smoketest: checkboxes page should open successfully', () => {
            CheckboxPage.open();
            // I am a mock test... 
            // I do absolutely nothing!
        });
    
        // > this IS NOT a smoke test! < //
        (smokeRun ? it.skip : it)('checkbox 2 should be enabled', () => {
            CheckboxPage.open();
            expect(CheckboxPage.firstCheckbox.isSelected()).toEqual(false);
            expect(CheckboxPage.lastCheckbox.isSelected()).toEqual(true);
        });
    
        // > this IS NOT a smoke test! < //
        (smokeRun ? it.skip : it)('checkbox 1 should be enabled after clicking on it', () => {
            CheckboxPage.open();
            expect(CheckboxPage.firstCheckbox.isSelected()).toEqual(false);
            CheckboxPage.firstCheckbox.click();
            expect(CheckboxPage.firstCheckbox.isSelected()).toEqual(true);
        });
    });
    

    2. Use it.only() to achieve mainly the same effect, the difference being the test-case refactor workload. I'll summarize these ideas as:

    • if you have more smoke tests than non-smoke tests, use the it.skip() approach;
    • if you have more non-smoke tests than smoke tests, use the it.only() approach;

    You can read more about pending-tests here.


    3. Use the runtime skip (.skip()) in conjunction with some nested describe statements.

    It should look something like this:

    // Reading the smokeRun state from a system variable:
    const smokeRun = (process.env.SMOKE ? true : false);
    
    describe('checkboxes testsuite', function () {
    
        // > this IS a smoke test! < //
        it('#smoketest: checkboxes page should open successfully', function () {
            CheckboxPage.open();
            // I am a mock test... 
            // I do absolutely nothing!
        });
    
        describe('non-smoke tests go here', function () {
            before(function() {
                if (smokeRun) {
                    this.skip();
                }
            });
            // > this IS NOT a smoke test! < //
            it('checkbox 2 should be enabled', function () {
                CheckboxPage.open();
                expect(CheckboxPage.firstCheckbox.isSelected()).toEqual(false);
                expect(CheckboxPage.lastCheckbox.isSelected()).toEqual(true);
            });
            // > this IS NOT a smoke test! < //
            it('checkbox 1 should be enabled after clicking on it', function () {
                CheckboxPage.open();
                expect(CheckboxPage.firstCheckbox.isSelected()).toEqual(false);
                CheckboxPage.firstCheckbox.click();
                expect(CheckboxPage.firstCheckbox.isSelected()).toEqual(true);
            });
        });
    });
    

    !Note: These are working examples! I tested them using WebdriverIO's recommended Jasmine Boilerplace project.

    !Obs: There multiple ways to filter Jasmine tests, unfortunately only at a test-file(testsuite) level (e.g: using grep piped statements, or the built-in WDIO specs & exclude attributes).