Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CasperJS HTTP Authentication

cannot deal with simply HTTP authentication. Here's my code:

var x = require('casper').selectXPath;
var casper = require('casper').create({
    verbose: false,
    logLevel: 'debug'
});
casper.options.viewportSize = {width: 1366, height: 667};
casper.start('https://example.com/a/dashboard');
casper.setHttpAuth('username', 'password');

casper.then(function() {
    this.test.assertUrlMatch(/^https:\/\/example.com\/a\/dashboard$/);
});

casper.run(function() {this.test.renderResults(true);});

And error that I see in console:

casperjs mytest.js
FAIL Current url matches the provided pattern
#    type: assertUrlMatch
#    subject: false
#    currentUrl: "https://example.com/login"
#    pattern: "/^https:\\/\\/example.com\\/a\\/dashboard$/"
FAIL 1 tests executed in 2.428s, 0 passed, 1 failed.

Q: Why my current Url does't match with pattern? Thank you.

like image 709
Satevg Avatar asked Jan 13 '23 14:01

Satevg


1 Answers

I believe you need to call casper.setHttpAuth() before you load the page.

It also looks like you forgot to call casper.exit() at the end of casper.run().

var x = require('casper').selectXPath;
var casper = require('casper').create({
    verbose: false,
    logLevel: 'debug'
});
casper.options.viewportSize = {width: 1366, height: 667};

casper.start();
casper.setHttpAuth('here_i_type_username', 'here_password');

casper.thenOpen('https://mysite.com/a/dashboard', function() {
    this.test.assertUrlMatch(/^https:\/\/mysite.com\/a\/dashboard$/);
});

casper.run(function() {
    this.test.renderResults(true);
    this.exit();
});
like image 193
hexid Avatar answered Jan 16 '23 17:01

hexid