The next thing that we would like to talk about is Nightwatch reporting.
By default, Nightwatch has two reporters:
If we're using a CI server like Jenkins, then we'd be able to see the report in the Jenkins dashboard if we reference this folder. So, those are the two reporters.
Nightwatch also gives us the option to create a custom reporter if we need to.
We can create a custom reporter by using the keyword reporter
in our globals
file. And this reporter
has two arguments: results
and the done
callback.
The results
object has all the information that Nightwatch has gathered from our tests.
We can use this information to either store the data or parse the data to be used by another system.
So, what we'll do, we'll create a simple custom reporter, which will write the results to a JSON file.
We'll use the Node fs
module to write the data to a file.
We'll call fs.writeFile()
and we'll call our file testresults.json
.
We can add the results
here. But, let's use JSON.stringify and format the text so it would be easier to read.
We'll also check if there are errors - if there are we'll throw
those errors.
If there is no error, then the file is saved. We can also print out 'report saved'.
Then we'll call our done
callback.
const fs = require('fs')
module.exports = {
//Executed before selenium session is created
before: (done) => {
done();
},
//Executed after closing the selenium session
after: (done) => {
done();
},
beforeEach: (client, done) => {
done()
},
afterEach: (client, done) => {
done()
},
reporter: (results, done) => {
fs.writeFile('testresults.json', JSON.stringify(results, null, '\t'), (err) => {
if(err) throw err;
//then the save is saved
console.log('report saved');
done();
});
}
}
Let us run our debugging tests that we had and see if we get the results.
So it says "report saved". There is our file that's now in our root - testresults.json
.
This is the basic structure of the test data. It's telling us that we had two tests that passed, no failed
, no errors
, no skipped
.
We had two assertions
and the modules
are basically our test suites. Right now, we only had one test suite, which was a debugging.test
.
We got some information regarding how long the test took (time
).
We also see what was completed.
We had one test that was doing the search and it shows you the assertions
that were performed on that test.
It also showed the test cases that we had and it tells us the path
of the file that this test was run from.
This could be our reporter.
If we're saving this information to be using another system, then we can do that, or we could get more elaborate and basically parse the data and transform it into anything that we want to transform it into.