Okay... good news. I've manage to access the local storage by digging a little deeper, and making sure I do the handy work in the callback function passed. So, the working (but unoptimised) code looks like this:
exports.command = function(session, callback) {
session = window.localStorage.getItem('__ef__');
return session;
},
// allows for use of callbacks with custom function
function(result) {
if (typeof callback === 'function') {
callback.call(self, result);
}
});
// allows command to be chained
return this;
};
And the calling test looks like this:
function hweAccLogin(client, user, pass) {
var eFormEmail = '#email';
var eFormPass = '#password';
var eButton = 'user-login .button';
var eLogout = 'app-navigation a[href="/logout"]';
var session = {};
client
.waitForElementVisible(eFormEmail, 1000, 'Email field %s visible after %d ms')
.setValue(eFormEmail, user)
.waitForElementVisible(eFormPass, 1000, 'Password field %s visible after %d ms')
.setValue(eFormPass, pass)
.waitForElementVisible(eButton, 1000, 'Login button %s visible after %d ms')
.click(eButton)
.waitForElementVisible(eLogout, 1000, 'Logout link present - user logged in successfully')
.setAuth(session, function showSession() {
client.globals.session = session;
console.log(client.globals.session);
});
}
This function is called from the actual test file, using the my globals.js file (which itself requires the .js file containing the log in function, and then exports it).
It's complex but allows for recycling some code while still forcing me to write everything async friendly. And, it works!
My problem now is that I thought I would be able to simply call client.setAuth as follows, without the optional callback parameter:
client.setAuth(client.globals.session);
But this doesn't work. It's definitely in how I've structured the custom command - but any time I change something I somehow break the command (have tried a bunch of things) - and now I'm afraid that the only way for me to do this is to always pass a callback, which is wasteful. I only really wanted to follow the Nightwatch custom command guidelines and keep it nice and modular / chainable / etc.
Thanks for your time!