Reputation: 8400
I have a web service that generates an dynamic image when /image.gif is called. How can I get the protractor test to download the file and do an MD5sum on the result?
I had a look at Protractor e2e test case for downloading pdf file but it relies on the browser having a download link to click.
There is no page hosting this image.
Upvotes: 3
Views: 2479
Reputation: 8400
I could not get the image to download on my windows setup, using Leo's answer no errors, but I found the following that allowed me to get the base64 data uri which I can "expect" on.
Convert an image into binary data in javascript
it('Image is version1)', function() {
browser.ignoreSynchronization = true
browser.get(targetUrl);
return browser.executeScript(function() {
var img = document.querySelector('img');
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}).then(function (data) {
expect(data).toEqual(base64VersionOfVersion1);
});
});
Upvotes: 2
Reputation: 16722
You should follow my Chrome configuration on that pdf-question link.
Then, instead of clicking something, browser.get('/image.gif');
should trigger the download.
Wait for the download to complete following Stephen Baillie solution or similar.
Finally, NodeJS side of things for the MD5 part, you can use execSync or something more fancy but execSync will do:
var execSync = require('exec-sync');
var md5sum = execSync('md5sum ' + pathToDownloadedFile).split(' ')[0]
// e.g. md5sum //=> "c9b833c49ffc9d48a19d0bd858667188"
Upvotes: 1