在表单提交CasperJS中发送http标头

qui*_*qui 4 javascript form-submit http-headers phantomjs casperjs

我有一个CasperJS的测试步骤,它执行以下操作:

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

我希望能够发送HTTP标头.如果没有"手动"发布表单,我似乎无法找到这样做的方法,这不是我想要的那种测试.

Art*_* B. 6

有一种比下面更简单的方法.您可以访问casper.page.customHeadersPhantomJS的网页实例的属性.

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

casper.then(function() {
    casper.page.customHeaders = {}; // reset headers
});
Run Code Online (Sandbox Code Playgroud)

问题是此方法会将您的自定义标头添加到每个请求,直到您再次禁用它.您可以尝试根据setTimeout或直接在后面重置标头fillSelectors.


无法在浏览器中为表单提交设置标头.__utils__.sendAJAX也不提供自定义标头.

您需要使用XMLHttpRequest来发送请求.以下函数替换表单的提交处理程序以发送Ajax调用,并等待:

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;
        });
    });
};
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样调用:

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
});
Run Code Online (Sandbox Code Playgroud)

如果您不需要等待XHR完成,则可以减少代码:

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);
};
Run Code Online (Sandbox Code Playgroud)