4

I have a test step with CasperJS that does the following:

this.fillSelectors("#registration-form", {
    "#first-name": "Bob",
    "#last-name": "Smith",
    "#email-address": RANDOM_EMAIL,
    "#password": PASSWORD,
    "#password-confirm": PASSWORD
}, true);

I want to be able to send a HTTP header along with this. I can't seem to find a way of doing this without "manually" posting the form, which isn't the kind of test I want here.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Chris James
  • 11,571
  • 11
  • 61
  • 89

1 Answers1

6

There is an easier way than the one below. You can access the casper.page.customHeaders property of PhantomJS' Webpage instance.

casper.then(function() {
    casper.page.customHeaders = {
        "CuStOm": "Header"
    }; // set headers
    this.fillSelectors("#registration-form", { /*...*/ }, true);
});

casper.then(function() {
    casper.page.customHeaders = {}; // reset headers
});

The problem is that this method will add your custom header to every request until you disable it again. You can experiment with resetting headers based on setTimeout or directly behind fillSelectors.


You cannot set headers for a form submission in the browser. __utils__.sendAJAX also doesn't provide custom headers.

You will need to use XMLHttpRequest to send the request. The following function replaces the submit handler of the form to send the Ajax call and also waits:

casper.thenFormToXHR = function(formSelector, data, customHeaders){
    this.thenEvaluate(function(formSelector, customHeaders){
        // see https://stackoverflow.com/a/19838177/1816580
        function submitForm(oFormElement) {
            window.__requestDone = false;
            var xhr = new XMLHttpRequest();
            for(var header in customHeaders) {
                xhr.setRequestHeader(header, customHeaders[header]);
            }
            xhr.onload = function(){
                window.__requestDone = true;
            };
            xhr.open (oFormElement.method, oFormElement.action, true);
            xhr.send (new FormData(oFormElement));
            return false;
        }
        document.querySelector(formSelector).onsubmit = submitForm;
    }, formSelector, customHeaders);
    this.then(function(){
        this.fillSelectors(formSelector, data, true);
    });
    this.waitFor(function check(){
        return this.evaluate(function(){
            return window.__requestDone;
        });
    });
};

You can then invoke like this:

casper.thenFormToXHR("#registration-form", {
    "#first-name": "Bob",
    "#last-name": "Smith",
    "#email-address": RANDOM_EMAIL,
    "#password": PASSWORD,
    "#password-confirm": PASSWORD
}, {
    "X-Requested-With": "XMLHttpRequest" // here should be your custom headers
});

The code can be reduced, if you don't need to wait for the XHR completion:

casper.formToXHR = function(formSelector, data, customHeaders){
    this.evaluate(function(formSelector, customHeaders){
        // see https://stackoverflow.com/a/19838177/1816580
        function submitForm(oFormElement) {
            var xhr = new XMLHttpRequest();
            for(var header in customHeaders) {
                xhr.setRequestHeader(header, customHeaders[header]);
            }
            xhr.open (oFormElement.method, oFormElement.action, true);
            xhr.send (new FormData(oFormElement));
            return false;
        }
        document.querySelector(formSelector).onsubmit = submitForm;
    }, formSelector, customHeaders);
    this.fillSelectors(formSelector, data, true);
};
Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • That's good, but could we handle http request out of the browser by using something like `superagent`? – Sayakiss Apr 21 '16 at 08:51
  • @Sayakiss Since superagent is a browser library, no, you cannot use it out of the browser. However, there is a node.js server counterpart, but I don't know if it can be used to do requests. Either way, PhantomJS is not node.js. Most node.js modules cannot be used in PhantomJS/CasperJS. – Artjom B. Apr 21 '16 at 15:37
  • @ArtjomB. Why not make a pull request to make `__utils__.sendAJAX` support custom headers? I post a issue about that: [Issue #1560](https://github.com/casperjs/casperjs/issues/1560). – Sayakiss Apr 28 '16 at 07:55
  • @ArtjomB. I came out with another [question](http://stackoverflow.com/questions/37048190/how-to-synchronous-call-child-process-in-casperjs), could you please give me some advices – Sayakiss May 05 '16 at 11:49