【问题标题】:Run a node command in Nightwatch global after之后在 Nightwatch 全局中运行节点命令
【发布时间】:2019-07-12 17:52:08
【问题描述】:

我想为我的夜间测试套件在“after”全局中运行一个 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 文件中是否有办法像这样在 'after' 命令中触发此节点命令?:

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();
}
};

【问题讨论】:

    标签: node.js nightwatch.js


    【解决方案1】:

    想通了。

    我需要将所有代码直接放在'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();
    }
    

    };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多