2017-05-28 41 views
0

我使用Slack將量角器測試掛鉤,如果測試開始並且一旦完成,想法是在鬆弛室中發送消息。Slack集成僅適用於onPrepare,但不適用於onLinunch後的onComplete?

但是,我只能在onPrepare期間成功地發送消息給slack。但不是在afterLanch或甚至在onComplete。

我試圖在afterLaunch和onComplete中做一個簡單的console.log,所以我知道它的工作原理。唯一讓我感到困惑的是爲什麼在測試完成之後,它並沒有將信息發送給我們。

如果有更好的方法來做到這一點,請告訴我你的想法。現在,這是我擁有的最好的。

請看看我的代碼:

let SpecReporter = require('jasmine-spec-reporter').SpecReporter; 
var webRep = require('jasmine-slack-reporter'); 
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter'); 
var SlackWebhook = require('slack-webhook'); 
var slack = new SlackWebhook('my-webhook-url'); 

exports.config = { 

capabilities: { 
    'browserName' : 'chrome', 
}, 
seleniumAddress: 'http://localhost:4446/wd/hub', 
specs: ['./smoke-test/loginAccountType.js'], 

onPrepare: function() { 
    browser.ignoreSynchronization = true, 
    slack.send({ 
    text: "Test is starting", 
    channel: '#test-report' 
    }), 
    jasmine.getEnv().addReporter(new SpecReporter({ 
    spec: { 
     displayStacktrace: true 
    } 
    })), 
    jasmine.getEnv().addReporter(
    new Jasmine2HtmlReporter({ 
     savePath: './reports', 
     takeScreenshotsOnlyOnFailures: true, 
    }) 
); 
}, 

afterLaunch: function() { 
    slack.send({ 
    text: "Test is Done", 
    channel: '#test-report' 
    }) 
}, 

jasmineNodeOpts: { 
    // Default time to wait in ms before a test fails. 
    defaultTimeoutInterval: 100000, 
    print: function() {}, 
    }, 

};

回答

0

看起來你的slack.send()是異步? 嘗試從你的afterLanch/onComplete中返回承諾 - 在這種情況下,量角器將在殺死node.js進程之前等待這個承諾解決。

我從來沒有與slack-webhook包合作過。但是我在文檔中看到的 - .send()正在回覆諾言。嘗試從你的afterLaunch/onComplete中返回它:

afterLaunch: function() { 
    return slack.send({ 
    text: "Test is Done", 
    channel: '#test-report' 
    }) 
}, 
0

除了@ Xotabu4他的回答。

根據docssend是異步的,所以做一個return;

但也catch的錯誤,只是爲了檢查什麼失敗,見下文。

return slack.send('some text') 
 
    .then(function(res) { 
 
    // succesful request 
 
    }) 
 
    .catch(function(err) { 
 
    // handle request error 
 
    })

希望它可以幫助

相關問題