Instance of Protractor e2e test case to load image file

I have a web service that generates a dynamic image when /image.gif is called. How can I get a protractor test to load the file and do MD5sum on the result?

I have looked at a Protractor e2e test case for downloading a PDF file , but it relies on a browser having a download link.

There is no page that hosts this image.

+3


source to share


3 answers


I could not get the image to download in my windows installation using Leo to answer any errors, but I found the following which allowed me to get the ur64 base64 data that I can "expect".

Convert image to 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);
    });
});                                                      

      

+2


source


You should follow my chrome config on this pdf link .

Then, instead of clicking the button browser.get('/image.gif');

, the download should start.

Wait for the download to complete after Stephen Baillie 's solution or similar.



Finally, the NodeJS side 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"

      

+1


source


@rob What is targetUrl in your answer .. is it the path to the image or the URL of the browser?

0


source







All Articles