Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do file POST with CasperJS through plain Javascript, not through UI

I can't figure out how to do the following:

Before running my tests I would like to post (multipart) a file to the server. Our backend creates content profiles for these uploads which can then be accessed through the UI. It's this content profile I need to run tests on.

I am aware of the .fill() functionality but this does not apply as I do not want to do file uploads through the UI. Is there any way this can be achieved via CasperJS or javascript or can anyone point me to documentation that might help me?

like image 901
PhysX Avatar asked Jan 13 '23 23:01

PhysX


1 Answers

As far as I have read the documentations of both casperjs and phantomjs, direct file submissions are not allowed. You may use curl like below:

curl http://some.testserver.com/post.php \
   -F file_input=@/path/to/my/file.txt \
   -F "text_field=Some Text Here" \
   -F some_number=1234

You can however open a POST request on casperjs:

casper.start();

casper.open('http://some.testserver.com/post.php', {
    method: 'post',
    data:   {
        'title': 'Plop',
        'body':  'Wow.'
    },
    headers: {
        'Content-type': 'multipart/form-data'
    }
});

casper.then(function() {
    this.echo('POSTED it.');
});

casper.run();

Here is the relevant documentation:

http://docs.casperjs.org/en/latest/modules/casper.html#open

like image 131
Sina Avatar answered Jan 31 '23 02:01

Sina