Uploading a file by scripted test?


Our site relies heavily on users being able to upload images to our site. I would like to be able to script this in my monitor. However as I understand it uploads are not currently supported. I have received a somewhat unclear potential work around via a sales contact at New Relic, I wonder if someone could clarify if this could be done?

Here’s the information I have; “we can’t host the file itself but if it was remotely hosted then if you can get at it from the internet you can do just about anything you can do from a web browser you just might have to download it, then later access the file if they are trying to upload”. Any clues as to how this could be scripted?


@rob_swindells - One of the optional node modules are the urllib and fs node modules. The proposed workaround would be to use those to make a web request to download your image from some online location you host, save it locally to the (temporary) file system and then upload from within your script itself. There is some additional explaination and discussion around a similar workaround with a binary file in this forum topic as well, Uploading a binary file to a URL using POST

If I require a file using:


I believe the image.png is written to the same directory the script is running from. I should then be able to access the filepath using __dirname + “/image.png”. However, the __dirname global object appears to be undefined when I validate my script.

@sdelight any way to get the __dirname of my file?

@rob_swindells - It should simply save to the relative path the script is running in and you should be reference the file relatively. If that isn’t working out for you, let me know and I can help dig deeper into the issue.

I am trying to upload a file using this method as discussed here request("https://www.generic.url/image.png").pipe(fs.createWriteStream("image.png")); .How do I upload the file to the file upload field? Any guidance would be much appreciated. Thank you.

@fagbemenya that line you posted will fetch image.png and write it to a file named “image.png” on the local runtime image (the container your script runs in), after that you need to use selenium to post the file to the form field like it’s done in this example: File upload example - just replace the fs calls to load your file (named image.png according to the code you posted).

@rferreira I am kind of confused a bit. I looked at the example link and was wondering about this line fs.writeFileSync(’/tmp/upload’, crypto.randomBytes(64).toString(‘hex’));

Also, how do I use the fs call to load my file. Find below what I was doing.

var file = request(“https://www.generic.url/image.png").pipe(fs.createWriteStream("poe_image.png”));

Now when I do
I get Error: unknown error: path is not absolute: poe_image.png

try this (you need a full file path):

var path = "/tmp/poe_image.png";
var file = request("https://www.generic.url/image.png").pipe(fs.createWriteStream(path));
1 Like

@rferreira Works perfectly now. Thank you! really appreciate your support.

1 Like

Hi @rferreira,

I am getting an error on the “request” function. it says it can’t find any name like that?


1 Like