使用Mocha测试NightmareJS代码失败

时间:2016-07-13 10:58:27

标签: node.js mocha nightmare

大家好我是Web开发的新手,最近我开始学习NodeJS,我写了一个简单的Youtube刮刀,它使用NightmareJS并返回每个视频URL的喜欢,视图,作者和标题名称的数量。

现在我正在尝试使用Mocha对我的代码进行单元测试(以进行一些单元测试)并且由于某种原因它失败并出现以下错误:“错误:超出2000ms的超时。确保done()回调正在进行在这个测试中称呼。“

我试图增加超时(最多15秒),但它没有帮助,我猜它在某处悬挂。我错过了什么?我也很高兴听到一些关于代码结构和实现的建设性批评。

这是我的代码:

var Nightmare = require('nightmare');
var expect = require('chai').expect;
var assert = require('chai').assert;
youtube_url = 'https://www.youtube.com/watch?v=0_oPsFTyhjY';

describe('test youtube video url results', function() {
    it('should return the actual video url/title/author name/num of likes and views', function(done) {
        var nightmare = Nightmare({ show: false, gotoTimeout: 3000 })
        nightmare
            .goto(youtube_url)
            .scrollTo(10000,0)
            .wait('#comment-section-renderer-items')
            .evaluate(function (youtube_url) {
                var authorSelector = '#watch7-user-header > div > a';
                var titleSelector  = '#eow-title';
                var vcountSelector = '#watch7-views-info > div.watch-view-count';
                var lcountSelector = '#watch8-sentiment-actions > span > span:nth-child(1) > button > span';

                var authorElement = document.querySelector(authorSelector);
                var titleElement  = document.querySelector(titleSelector);
                var vcountElement = document.querySelector(vcountSelector);
                var lcountElement = document.querySelector(lcountSelector);

                var JSONres = {'VIDEO URL':url, 'VIDEO TITLE': titleElement.innerText,'AUTHOR NAME': authorElement.innerText,  
                'NUMBER OF VIEWS': vcountElement.innerText, 'NUMBER OF LIKES': lcountElement.innerText};

                return (JSONres)
            },youtube_url)
            .end()
            .then(function (result) {
                try{
                    expect(result['VIDEO URL']).to.equal(youtube_url);
                    expect(result['VIDEO TITLE']).to.equal('The Best Mouse in the World?');
                    expect(result['AUTHOR NAME']).to.equal('Unbox Therapy');
                    assert.isAtLeast(result['NUMBER OF VIEWS'], 1816808, 'The number of views is at least the number of views that has been already seen');
                    // It's possible to remove your like from the video so hypothetically many users may remove their likes thus there is no upper/lower
                    // bound on the like amount a video can have at any time except that it must be non-negative.
                    assert.isAtLeast(result['NUMBER OF LIKES'], 0, 'The number of likes is at least a non-negative number');
                    done();
                }
                catch(error){
                    done(error);
                }
            })

    });
});

1 个答案:

答案 0 :(得分:1)

意外地将超时时间提高到 20秒 [与cannot convert value of type UInt.Type to expected argument type UInt ]刚刚解决问题(出于某种原因)虽然测试实际上从未超过 6秒