segment-boneyard / nightmare

A high-level browser automation library.
https://open.segment.com
19.55k stars 1.08k forks source link

POST formData ? #779

Closed jgrancher closed 7 years ago

jgrancher commented 8 years ago

Thanks for this library, it's really awesome. I've had to deal with a new use case recently, and I didn't see any example related.

So my question is: Can I use Nightmare, somehow, to post some data through the HTTP body? Example of what I'd like, using the request library:

const request = require('co-request');

const opts = {
    method: 'POST',
    url: 'http://example.com',
    headers: { 'cache-control': 'no-cache' },
    formData: { // My data object I want to send
        id_stuff: '160',
        date: '10/12/2016',
   },
};

const test = yield request(opts);
rosshinkley commented 8 years ago

Depends on what you mean. If you're trying to issue a POST from the client, you could use .evaluate() and XMLHttpRequest (or use/.inject() your tool of choice) to issue the request.

Could you provide more information about your use case?

jgrancher commented 8 years ago

@rosshinkley I've got two options:

The second option would be quicker as I would skip the first page. I wondered is such a thing is possible with Nightmare. Something like:

const url = 'http://url-page-b.com';
const headers = { method: 'POST', 'cache-control': 'no-cache' };
const data = { id_stuff: '160', ... };

nightmare.goto(url, headers, data);

Hope that's clear enough! Thanks :)

gregrafferty commented 8 years ago

I have a similar problem, have to write a bunch of tests to cover some functionality of a project we're working on and being able to do this would make the login step much faster, particularly given that we want to run every test in as isolated a fashion as possible.

kmacpher67 commented 7 years ago

method post is not part of "header" the original question has it correctly. I suppose if one could figure out actions maybe some serious hackery could be done there. really needs to be a new verb method in Nightmare class itself. Change or create new methods for .GET .POST .DELETE .PUT .SEARCH implement accordingly. It seems like this is a core electron problem. https://discuss.atom.io/t/add-http-post-method-to-electron-webview/29702 It comes back to the metaphor of the software. It's a browser and it is impossible from a URL input to perform anything but a http GET from the URL address line. .goto is the same as typing into the browser a location, which is only ever going to be a .GET method. So running arbitrary js fun is the only way.

You might be able to send a POST manually through AJAX/fetch etc then replace the HTML of the page in the webview with the HTML returned by your manual POST. You can achieve this using .executeJavaScript() and/or Electron's IPC.

Is there an decent example for doing this?

casesandberg commented 7 years ago

You could also fire type events to fill in each of the input fields and then click the submit button.

I agree with @kmacpher67

It's a browser and it is impossible from a URL input to perform anything but a http GET from the URL address line.

nurettin commented 6 years ago

Would be really convenient we could do a client.post(url, headers, data) instead of client.evaluate(...) I guess we can make a nightmarejs action for that, any examples?