How to use HTTP basic auth in SpookyJS

396 Views Asked by At

I wrote a little bit code to go to my Wordpress page with CasperJS with a htaccess protection. The script should login an then later update the plugins. At this time I can login and create a screenshot from the plugins who should updated. (also the CasperJS works fine)

Now I want that this snippet work on a server. So I use

express, spooky, node

And I found this spooky snippet and tried to insert my CasperJS code into it. But now I cannot overcome the htaccess protection with SpookyJS. Any idea?

Also this code below should work on a nodejs server with spooky.

var casper = require('casper').create({
    verbose: true,
    logLevel: 'debug',
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
    pageSettings: {
        loadImages:  false,         
        loadPlugins: false        
    },
    viewportSize: {
        width: 1900,
        height: 1200
    }

});
var x = require('casper').selectXPath;
var user_email = '<login-name>';
var user_pass = '<login-password';
var selector = '.update-link';

casper.start();

casper.setHttpAuth('<username>', '<password>');

casper.thenOpen('<wordpress url with htaccess', function() {

});

// wordpress login
casper.then(function() {
    this.page.evaluate(function(a,b) {
        document.querySelector("input[name='log']").value = a
        document.querySelector("input[name='pwd']").value = b;
        document.querySelector("#wp-submit").submit(); //nothing happened
    }, user_email, user_pass);
}).then(function(){

});

casper.thenClick(x('//*[@id="wp-submit"]'), function () {
    console.log("clicked login")
});

// An example to mark all plugins to update
casper.thenOpen('<wordpress site>/wp-admin/plugins.php', function() {
    this.waitForSelector(selector, function then() {
        this.evaluate(function (sel) {
            var x =document.querySelectorAll(sel);

            for (i = 0; i < x.length; i++) {
                x[i].style.backgroundColor = "red";
            }
        }, selector);
    });
});



casper.then(function () {
    casper.capture('update_me.png');
})

casper.run();
1

There are 1 best solutions below

1
On BEST ANSWER

SpookyJS doesn't seem to provide the equivalent function to CasperJS' setHttpAuth(), but you can still pass the username and password during creation through the pageSettings property:

var spooky = new Spooky({
    child: {
        transport: 'http'
    },
    casper: {
        logLevel: 'debug',
        verbose: true,
        pageSettings: {
            userName: 'some username',
            password: 'some password'
        }
    }
}, function (err) {
    // your script here
});