The easiest way to fill forms from Node.JS
Sometimes you need to automate your form filling. However, the website that hosts the form, seeds some security tokens in the form. The security tokens are random and attached to your current session.
You need to scrape the form first, including all the default values. Afterwards, you'll need to fill the details and submit the form.
form-scraper
automates all the process.
$ npm install form-scraper
or add form-scraper
to your package.json
dependencies.
You can use form-scraper
both in functional and object oriented way.
Fetches a form with formId
from the specified url.
formId
- theid
property of a form in a given html. Should be prefixed with "#".url
- The url that hosts the form.promisifiedRequest
- Therequest
object (which returns promise) that performs the request.
The return value is a Promise
which eventually should resolve to a JSON object with the following properties:
action
- The url where the form should be submitted to.data
- A JSON hash object where thekeys
are thename
properties of the form elemnts and thevalue
is the default value.
Creates an object that implements the provideForm
method.
promiseForForm
- a JSON object that represents the form.
The return value is an object that implements the provideForm
method.
Submits the form...
formValues
- a JSON hash object where thekeys
represent the property names and thevalues
is the data to post.formProvider
- An object that implements theprovideForm()
method and returns a Promise for form.promisifiedRequest
- Therequest
object (which returns promise) that performs the request.
The return value is a Promise
which eventually should resolve to the response
of the submission of the form.
var pRequest = require("promisified-request").create();
var fScraper = require("form-scraper");
var formStructure = fScraper.fetchForm("#login", "http://www.someurl.com", pRequest);
var loginDetails = { user: "my user", password: "my password" };
fScraper.submitForm(loginDetails, fScraper.provideForm(formStructure), pRequest).then( function (response) {
console.log(response.body);
});
As of today, the module consists of two classes:
form-scraper.ScrapingFormProvider
- Scrapes the form.form-scraper.FormSubmitter
- Submits the form.
Extends the current options. The default value is undefined, so it's important to inject the dependencies using this method.
Returns itself.
The options
argument, should have the following values:
formId
- Theid
of the Dom element that represents the form to scrape.url
- The url that hosts the form.promisifiedRequest
- Therequest
object that will perform the scraping.
Returns a promise that eventually will be resolved to the form structure.
Extends the current options. The default value is undefined, so it's important to inject the dependencies using this method.
Returns itself.
The options
argument, should have the following values:
formProvider
- An object that implementsprovideForm()
and returns a promise for a form.promisifiedRequest
- Therequest
object that will perform the submission.
Submits the form with formValues
.
formValues
is a hash JSON object with values to post.
It returns a promise that eventually will be resolved to the response
object that comes out of the request
object.
var pRequest = require("promisified-request").create();
var fScraper = require("form-scraper");
var loginDetails = { user: "my user", password: "my password" };
var formProvider = new fScraper.ScrapingFormProvider();
var formSubmitter = new fScraper.FormSubmitter();
formProvider.updateOptions({
formId: "#login",
url: "http://www.somedomain.com",
promisifiedRequest: pRequest
});
formSubmitter
.updateOptions({
formProvider: formProvider,
promisifiedRequest: pRequest
})
.submitForm(loginDetails)
.then(function(response) {
console.log(response.body);
});