之后在Nightwatch Global中运行节点命令

时间:2019-07-12 17:52:08

标签: node.js nightwatch.js

我想在我的夜视测试套件的“之后”全局中运行一个nodemailer js文件。这是我现在运行的命令以启动示例测试,并使用'nightwatch-html-reporter'生成html报告。

node_modules/.bin/nightwatch --config nightwatch.conf.BASIC.js --test test\e2e\search.js --reporter ./reports/reporter.js

想法是,在运行并创建此报告后,我想立即通过电子邮件发送该邮件。

这是我在上一个命令之后在powershell中使用的命令,用于通过“ nodemailer”和“ nodemailer-sendgrid-transport”通过电子邮件发送报告

node reports/nodemailer.js

我尝试将来自nodemailer.js的代码合并到report.js文件中,但是这会干扰报告的创建。生成报告后,如何通过电子邮件自动发送电子邮件。

globals.js文件中是否有一种方法可以像这样在“之后”命令中触发此节点命令?

module.exports = {
// this controls whether to abort the test execution when an assertion failed and skip the rest
// it's being used in waitFor commands and expect assertions
abortOnAssertionFailure: true,

... 

'default': {
    myGlobal: function () {
        return 'I\'m a method';
    }
},

'emailReport': { <-- HERE???
    myGlobal: function () {
         node reports/nodemailer.js <--???
    }
},

...

after(cb) {
    this.emailReport.myGlobal(); <---???
    cb();
},
afterEach(browser, cb) {
    browser.perform(function () {
        //console.log('GLOBAL afterEach')
        cb();
    });
},

reporter(results, cb) { <-- or in here somehow???
    cb();
}
};

1 个答案:

答案 0 :(得分:0)

想通了。

我需要像这样在“ after”命令中将所有代码直接放入global.js中:

var nodemailer = require('nodemailer');
var fs = require('fs');

module.exports = {
// this controls whether to abort the test execution when an assertion failed and skip the rest
// it's being used in waitFor commands and expect assertions
abortOnAssertionFailure: true,
...

'default': {
    myGlobal: function () {
        return 'I\'m a method';
    }
},

'test_env': {
    myGlobal: 'test_global',
    beforeEach: function () {

    }
},

before(cb) {
    //console.log('GLOBAL BEFORE')
    cb();
},

beforeEach(browser, cb) {
    //console.log('GLOBAL beforeEach')
    browser.maximizeWindow();
    cb();
},

after(cb) {
    // send email with generated report
    var file;
    if (fs.existsSync('./reports/html/report.html')) {
        file = fs.readFileSync('./reports/html/report.html', "utf8");
    }
    var client = nodemailer.createTransport({
        service: 'SendGrid',
        auth: {
            user: 'USERNAME',
            pass: 'PASSWORDAPIKEY'
        }
    });

    var email = {
        from: 'user@domain.com',
        to: 'user@domain.com',
        subject: 'Nightwatch Report',
        //text: 'Hello world',
        html: file
    };

    client.sendMail(email, function (err, info) {
        if (err) {
            console.log(err);
        }
        else {
            console.log('Message sent: ' + info.response);
        }
    });
    cb();
},

afterEach(browser, cb) {
    browser.perform(function () {
        //console.log('GLOBAL afterEach')
        cb();
    });
},

reporter(results, cb) {
    cb();
}

};