Build Mocha test dynamically after getting data from webdriver.io

后端 未结 3 1259
南旧
南旧 2021-01-19 09:32

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 wan

相关标签:
3条回答
  • 2021-01-19 09:52

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-19 09:59

    Mocha supports two ways to handle asynchronicity in tests. One way is using the done callback. Mocha will try to pass a function into all of your its, befores, etc. If you accept the done callback, it's your responsibility to invoke it when your asynchronous operation has completed. Callback style:

    before(function(done) { 
        browser.url("url").getHTML("button").then(function() {
            done();
        }); 
    }); 
    

    The other approach is to use Promises. Since your call to getHTML returns a Promise, you could just return that promise and Mocha would know to wait for the promise to settle before moving ahead with anything.

    Here is an example of the Promise style:

    before(function() { 
        return browser.url("url").getHTML("button"); 
    }); 
    

    A couple things worth noting about this: - getHtml() returns a promise for the html buttons. Whenever the asynchronous call to getHTML completes, the function passed into the then function gets invoked and the resulting value from the call to getHTML is passed in. - Returning that promise in the before lets mocha know that you're doing something asynchronous. Mocha will wait for that promise to settle before moving past your 'before'.

    For your specific example, you might want to try is something like this:

    describe('the buttons', function() {
       var buttons;
    
       before(function() { 
           return browser.url("url").getHTML("button").then(function(result) { 
                buttons = result;  
            }; 
       }); 
    
       it('does something', function() {
         buttons.forEach(function(button) {  
         }); 
       }); 
    
    });
    
    0 讨论(0)
  • 2021-01-19 10:05

    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');
    });
    
    0 讨论(0)
提交回复
热议问题