Download a file protractor test case
I was able to achieve download testing with Chrome.
Follow this config setup: https://stackoverflow.com/a/26127745/511069
Create a
function waitFileExists(fileAbsPath)
and performs your expectations after the file is completely downloaded: https://stackoverflow.com/a/27031924/511069
Protractor e2e test case for downloading image file
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);
});
});
E2E test with internet explorer where I need to download a file
While this doesn't answer your question directly, here is a link to an excellent blog about "How to download files with selenium and why you should not". Author talks about different use cases and provides a back door solution to do this in Java. The most critical question the article mentions is
"What are you going to do with this downloaded file?"
"Do you want to just makes sure that you get 200 OK from the server?"
"Do you want to verify the content?"
Obviously mostly people would say "yes" to "200 OK verification", If you want to validate the content of lets a PDF document, then its outside the scope of selenium anyways. The article provides Java solution to check if you get 200 OK from the server, Since you are using protractor, I'll provide a nodejs solution.
var request = require('request');
var fileDownloadElement = element(by.css('#download'));
fileDownloadElement.getAttribute('href').then(function(url) {
request(url,function(error, response, body){
if(error) {
throw new Error('Error downloading file', error);
}
if(response.statusCode !== 200) {
throw new Error('Server threw non 200 OK status ->', response.statusCode);
}
if(!body) {
throw new Error('Server did not respond with any data');
} else {
//write body to a file if you want to do anything with it
}
});
});
I used request client up here, you can use it or any other similar client.
Disclaimer: I haven't tested this code, but hope you get the idea.
Related Topics
Rails - How to Include JavaScript Files Only on Certain Pages
Crbug/1173575, Non-Js Module Files Deprecated. Chromewebdata/(Index)꞉5305:9:5551
Ruby Methods Within JavaScript Within Haml
Faye Ruby Client Is Not Working
How to Print an Iframe from JavaScript in Safari/Chrome
Rails Include JavaScripts Assets Folder Recursively
Access Parent's Parent from JavaScript Object
How to Disable Tree Shaking in Rollupjs
How to See Ping Pong in Web Socket in iOS
How to Print Part of a Rendered HTML Page in JavaScript
Wait for Multiple Promises to Finish
Implement JavaScript Alert and Confirm on Wkuidelegate Swiftui
Valueof() VS. Tostring() in JavaScript
Angularjs - Ng-Cloak/Ng-Show Elements Blink
How to Reference Other Properties During Object Declaration in JavaScript
JavaScript Function Leading Bang ! Syntax