I am having issues figuring out how to take a screenshot ONLY when a test fails in InternJs. I have this simple test in my registerSuite;
'verify google homepage': function () {
var url = 'https://www.google.com/';
return this.remote
.get(url)
.getCurrentUrl()
.then(function (data) {
assert.strictEqual(data, url, 'Incorrect URL');
})
.findByName('q')
.click()
}
I can simply create a screenshot using the following code;
.takeScreenshot
.then(function (data) {
fs.writeFileSync('/path/to/some/file', data, 'base64');
)}
I want to only take a screenshot, if the above test fails the assertion or is unable to find the locator.
I looked into the afterEach
method, but I can't figure out how to get the status of the last test to apply a conditional.
So my question is, has anyone setup their internjs test to only take screenshots on failures and how was it accomplished?
I've been playing with this today and have managed to get it for an entire suite rather than needing to add the code to every single test which seems quite needless.
The annoying thing you will need to do is do this for every test suite rather than "globally" but is much better than doing it for every test.
Building on the answer by Hugo Oshiro,
Then in your intern config:
According to this issue, starting with the Intern 3.0 you can do a custom reporter that take an Screenshots when test fail. So you can centralize it in a simple way, just referencing the custom reporter in your
config.js
. In my case, what can I just add a reporter array in theconfig.js
with the path to my custom array:than I made an custom reporter overriding
testFail
:Pay attention to the relative paths both to reference the custom reporter and the place for screenshots. They all seems to be taken considering where you run
intern-runner
, not the place the source files are located. For more info about custom reporters go to this page.It is not currently possible to interact with the currently executing test from
beforeEach
orafterEach
methods; this capability is coming in the next version of Intern.Selenium server, by default, provides a screenshot on every Selenium command failure, which is a Buffer object on the
error.detail.screen
property. If a Selenium command fails, just use this property which already has the screenshot waiting for you.For assertion failures, you can create a simple promise helper to take a screenshot for you:
If it’s too inconvenient to wrap all your callbacks manually like this, you can also create and use a custom interface for registering your tests that wraps the test functions automatically for you in a similar manner. I’ll leave that as an exercise for the reader.
You can use catch method at the end of your chain and use error.detail.screen as suggested by C Snover.