I have been trying to get Sauce Labs to report the names of the tests that are being run with CodeceptJS, but so far I've only been able to report success/failure.
I followed the following thread as a guide to help with reporting anything to Sauce Labs in general: https://github.com/Codeception/CodeceptJS/issues/371
The snippet I found is as follows:
_after() {
if (process.env.SAUCE_USERNAME) {
var sessionId = this.helpers['WebDriverIO'].browser.requestHandler.sessionID;
var sauce_url = "Test finished. Link to job: https://saucelabs.com/jobs/";
sauce_url = sauce_url.concat(sessionId);
console.log(sauce_url);
var dataString = '{"passed": true}';
var status_url = 'https://saucelabs.com/rest/v1/';
status_url = status_url.concat(process.env.SAUCE_USERNAME);
status_url = status_url.concat('/jobs/');
status_url = status_url.concat(sessionId);
var options = {
url: status_url,
method: 'PUT',
body: dataString,
auth: {
'user': process.env.SAUCE_USERNAME,
'pass': process.env.SAUCE_ACCESS_KEY
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
}
}
The problem here is that I don't know how to access the test name from here, nor do I know how to obtain the status of the test (pass/fail).
I've since learned that with helpers you can dispatch events on test passed or test failed. I created a new file, made sure to add it in the hooks section of the config, and added the event dispatchers. This is great because now I have access to the test object, however the following code does not work:
event.dispatcher.on(event.test.failed, function(test, testError) {
console.log("TEST FAILED...");
console.log(this.helpers["WebDriverIO"]);
var sessionId = this.helpers["WebDriverIO"].browser.requestHandler
.sessionID;
console.log(sessionId);
var sauce_url = "Test finished. Link to job: https://saucelabs.com/jobs/";
sauce_url = sauce_url.concat(sessionId);
console.log(sauce_url);
var dataString = `{"passed": false, "name": "${
test.title
}","customData": { "error": ${test.steps} }}`;
var status_url = "https://saucelabs.com/rest/v1/";
status_url = status_url.concat(process.env.CLOUDSERVICE_USERNAME);
status_url = status_url.concat("/jobs/");
status_url = status_url.concat(sessionId);
var options = {
url: status_url,
method: "PUT",
body: dataString,
auth: {
user: process.env.CLOUDSERVICE_USERNAME,
pass: process.env.CLOUDSERVICE_KEY
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
});
The reason it doesn't work is because the following line does not seem to work:
var sessionId = this.helpers['WebDriverIO'].browser.requestHandler.sessionID;
Specifically, this.helpers['WebDriverIO']
does not get printed out to my console, leading me to believe that it does not work from a helper context.
Is there some way to retrieve the session ID of a test from the context of an event dispatch? The documentation for tests seems to suggest that there are other properties but does not list them. Here's the doc for reference: https://codecept.io/hooks/#api
Thanks in advance!
Looks like you can probably use the saucelabs package to handle this.
Set it up and auth first
And then once the test is complete you can post the results back to SauceLabs like so: