前一篇提到可以更改 session name 也可以標註 pass/fail
接著就可以將昨天提到的的設定加入到 afterEach
如此一來就可以在測項結束後,在 browserstack 上標注 pass/fail
// session.js
const setSessionStatus = async (sessionId, GUID, status) => {
const option = {
url: `https://api.browserstack.com/automate/sessions/${sessionId}.json`,
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
auth: {
user: process.env.BROWSERSTACK_USER,
password: process.env.BROWSERSTACK_PASSWORD,
},
body: `{"status": "${status}","reason":""}`,
};
request(option, function(error, response, body) {
if (!error && response.statusCode == 200) {
return body;
}
console.log('[session.js] SET Session status Fail');
console.log(error);
return error;
});
}
並加入 afterEach
// test.js
const { setSessionStatus } = require('session');
afterEach: function(browser, done) {
browser.end(() => done());
let status = `passed`;
if (browser.currentTest.results.failed > 0) {
status = `failed`;
}
const { sessionId, GUID } = sessionQueue.shift();
setSessionStatus(sessionId, GUID, status);
}
不過 browserstack 無法把 sessionId 傳到 afterEach 裡面
因此會需要在每條測試項中加入 sessionID
// test.js
'testing website': function(browser) {
sessionQueue.push({ sessionId: browser.sessionId });
browser.verify...
}