Question

I'm trying to test a web page that has basic authentication.

This is what I have so far:

var casper = require('casper').create({
    verbose: true,
    logLevel: 'debug'
});

casper.options.viewportSize = {width: 1366, height: 667};

casper.start();

casper.open( 'http://internal/page.php', {
    method: 'get',
    headers: {
        'Authorization': 'Basic '+btoa('username:password')
    }
}).then(function(response) {
    this.fill('form[action="page2.php"]', {
        cattype: 'BLAH2',
        needdate: '04/21/2014'
    }, true);
});
casper.then(function() {
    this.capture('screenshot-form-entry.png');
});
casper.run(function() {
    this.exit();
});

The page opens properly (no authentication problem).

The form fields are set properly.

The problem is the form submission.

It says status=fail.

I look at the server logs and it gives a 401.

Why are the credentials not passed along to the form submission? Or maybe the question should be, how can a I get the credentials passed along?

Was it helpful?

Solution

After a couple of days of digging I'll answer my own question with what I found.

I'm not sure if this is a bug or the way it's supposed to work.

For basic authorization, you have to set username/password in the pageSettings.

Here is the changed code.

var casper = require('casper').create({
    verbose: true,
    logLevel: 'debug',
    pageSettings: {
       userName: 'uid',
       password: 'pwd'
    }
});

casper.start();

casper.open( 'http://internal/page.php').then(function(response) {
    this.fill('form[action="page2.php"]', {
        cattype: 'BLAH2',
        needdate: '04/21/2014'
    }, true);
});
casper.then(function() {
    this.capture('screenshot-form-entry.png');
});
casper.run(function() {
    this.exit();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top