Skip to content

Instantly share code, notes, and snippets.

@saadtazi
Last active May 1, 2019 12:11
Show Gist options
  • Save saadtazi/671494f7cb83591a7f67 to your computer and use it in GitHub Desktop.
Save saadtazi/671494f7cb83591a7f67 to your computer and use it in GitHub Desktop.
Phantomjs login + nodejs requests (after getting cookies)
  1. npm install

  2. To test that phantomjs works as expected: ./node_modules/.bin/phantomjs phantom-script.js USERNAME PASSWD

  3. Then run it from nodejs: node phantom.js (the username and password are hardcoded in this file for now...)

The phantom.js script just proves that it works, by checking the "username" in the top right corner of the Premier homepage.

Note that it still takes about 6 seconds for phantomjs to login into QA.

axios package is used as a nodejs HTTP client... But request or any other nodjs http client can be used (that allows to send custom headers).

{
"name": "nightwatch-axios",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"dependencies": {
"axios": "^0.9.1",
"cheerio": "^0.20.0",
"nightwatch": "^0.8.16",
"phantomjs-prebuilt": "^2.1.4"
}
}
var page = require('webpage').create();
var system = require('system');
var args = system.args;
var username = args[1];
var pwd = args[2];
// console.log('args', JSON.stringify(args));
// console.log('The default user agent is ' + page.settings.userAgent);
var START_URL = 'http://premier.qa.shutterstock.com/login';
page.open(START_URL, function(status) {
// get csrf and form_url from page (using frontend js)
var info = page.evaluate(function() {
return {
csrf: document.querySelector('input[name="_csrf"]').value,
form_url: document.querySelector('form').action
};
});
// build data to post
var data = 'username=' + username + '&password=' + pwd + '&_csrf=' + info.csrf;
// send post data to login
page.open(info.form_url, 'post', data, function (status) {
if (status !== 'success') {
console.log('Unable to post!');
} else {
// get username from page
var username = page.evaluate(function () {
var usernameElement = document.querySelector('#navigation-username b');
return usernameElement && usernameElement.innerHTML
});
if (!username) {
console.error('username not found in Premier page');
phantom.exit(1);
}
console.log(JSON.stringify({
username: username,
cookies: phantom.cookies
// cookies: phantom.cookies.filter(function (cookie) {
// return cookie.domain === '.qa.shutterstock.com';
// })
}));
// Uncomment and search for PremierTestMusic to prove that you are logged in
// console.log(page.content);
}
phantom.exit();
});
});
var path = require('path');
var childProcess = require('child_process');
var phantomjs = require('phantomjs-prebuilt');
var binPath = phantomjs.path;
var axios = require('axios');
var cheerio = require('cheerio');
var childArgs = [
path.join(__dirname, 'phantom-script.js'),
'PremierTestMusic',
'Stock123'
];
var start = Date.now();
// === use phantom to login and get session cookie
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
// handle results
var end = Date.now();
console.log('>>> phantomjs time:', ((end - start ) / 1000) | 0, 'seconds');
if (err) {
console.error('An error occured', err);
process.exit();
}
// stdout corresponds to the `console.log()` done in `phantom-script.js`
// I made sure to only have one console.log that outputs:
// { username: 'USERNAME', cookies: [{name: '...', value: '...'}, ...]}
var res = JSON.parse(stdout);
// === axios call to prove that it works
// 1. build cookie header
var cookieString = res.cookies.reduce(function (memo, cookie) {
// we can probably filter some cookies...
memo.push([cookie.name, cookie.value].join('='));
return memo;
}, []).join('; ');
// console.log('>>> cookieString', cookieString);
return axios.get('http://premier.qa.shutterstock.com/', {
headers: {
Cookie: cookieString,
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36'
}
}).then(function (res) {
$ = cheerio.load(res.data);
console.log($('#navigation-username').text());
});
});
@lloydpresly
Copy link

I followed the steps but didn't work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment