Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Mocha test dynamically after getting data from webdriver.io

I'm looking for a solution to define Mocha tests after getting data asynchronously.

For now, I use gulp-webdriver to getting HTML content with Selenium. And I want tests certain HTML tags structure.

For example, I want to get all buttons structure from an HTML page.

1° In Mocha Before(), I get buttons :

var buttons = browser.url("url").getHTML("button");

2° And after that, I want tests each button in a separate it :

buttons.forEach(function(button) {  it() }); 

The only solution found is loading HTML and extract buttons with Gulp before launch Mocha test with data_driven or leche.withData plugin.

Do you know another solution directly in Mocha test definition?

Thanks in advance,

like image 636
ali_o_kan Avatar asked Mar 14 '23 00:03

ali_o_kan


2 Answers

It doesn't seem possible to dynamically create it() tests with mocha.

I finally organise my test like this :

it('Check if all tag have attribute', function() {
        var errors = [];
        elements.forEach(function(element, index, array) {
            var $ = cheerio.load(element);
            var tag = $(tagName);
            if (tag.length) {
                if (!tag.attr(tagAttr)) errors.push(element);
            }
        });
        expect(errors).to.be.empty;
    }
}
like image 129
ali_o_kan Avatar answered Apr 25 '23 20:04

ali_o_kan


You can actually create dynamic It() tests with mocha if you don't mind abusing the before() hook a bit:

before(function () {
    console.log('Let the abuse begin...');
    return promiseFn().
        then(function (testSuite) {
            describe('here are some dynamic It() tests', function () {
                testSuite.specs.forEach(function (spec) {
                    it(spec.description, function () {
                        var actualResult = runMyTest(spec);
                        assert.equal(actualResult, spec.expectedResult);
                    });
                });
            });
        });
});

it('This is a required placeholder to allow before() to work', function () {
    console.log('Mocha should not require this hack IMHO');
});
like image 44
rob3c Avatar answered Apr 25 '23 20:04

rob3c