量角器:afterLaunch中的命令不运行

时间:2017-01-24 02:41:59

标签: protractor

我正在尝试在测试运行完毕后发送一封包含结果的电子邮件。 当我将sendgrid调用放入onComplete部分时,电子邮件就会发送。但是,由于报告尚未完成保存,因此报告为空。

如果我将sendgrid调用放入afterLaunch部分,则保存该文件。但是,它似乎没有运行sendgrid命令。

这是我正在使用的配置:

var HtmlReporter = require ('protractor-jasmine2-html-reporter');
var browser = "chrome";
var environment = "dev";
var pname = "";
var pversion = "";
var dname = "";
var selenium_address = "http://localhost:4444/wd/hub";
var folder_name = (new Date()).toString().split(' ').splice(0,3).join('');
var report_folder = "./test/e2e/reports/" + folder_name + "_" + browser +   "_" + environment + "/";
var reporter = new HtmlReporter({
  savePath: report_folder,
  takeScreenshots: false,
  cleanDestination: true,
  fileName: 'CE_Smoketest'
});

function test_function() {
   var report = report_folder + 'CE_Smoketest.html'
   var sendgrid = require('sendgrid')('xxxxx', 'xxxxxxxxx');
   var email = new sendgrid.Email();
   email.addTo("destination@mail.com");
   email.setFrom("Smoketest@mail.com");
   email.setSubject("Smoketest");
   email.setHtml("Smoketest results");
   email.addFile({filename: report});
   sendgrid.send(email);
}

exports.config = {
//appium setup
seleniumAddress: selenium_address,
multiCapabilities: [
{
    'browserName' : browser,
    platformName : pname,
    platformVersion : pversion,
    deviceName: dname
}
],

getPageTimeout: 50000,
allScriptsTimeout: 50000,
jasmineNodeOpts: {
  defaultTimeoutInterval: 50000,
  isVerbose: true
},

onPrepare: function() {

    jasmine.getEnv().addReporter(reporter);
      },
onComplete: function(exitCode) {
    test_function();

},
afterLaunch: function(exitCode) {
    test_function();
}
}

关于为什么afterLaunch不发送电子邮件的任何想法?

2 个答案:

答案 0 :(得分:1)

在afterLaunch方法中,Webdriver实例已关闭(不可用)。那是你的代码将被执行。检查下面的afterLaunch()方法的描述:

SPDRP_COMPATIBLEIDS

答案 1 :(得分:0)

Suresh是对的,如果您希望在程序退出之前执行异步代码,则必须返回一个承诺

试试这个:

1-声明新var

var q = require('q');

2-重构你的afterLaunch

afterLaunch: function(exitCode) {
  return q.fcall(function () {
     test_function();
  }).delay(1000);
}