Suggestions on Testing for the Value of a Auth Cookie?

27 views
Skip to first unread message

Neha Chriss

unread,
Nov 16, 2015, 6:39:06 PM11/16/15
to casp...@googlegroups.com
Hello,

I'd like to write a script that tests for whether an authenticated
session is present, and if not, to navigate to a sign-in page, fill
form, and submit. How do I test for the presence of a cookie for the
current running casperjs process? If the cookie parameter is present,
then the session has been authenticated. If not, then we go on to
auth. Basically, I want to test for the presence of a cookie
parameter.

Is this possible to do without mucking with reading/writing cookie
files/cookie loading? I'm reviewing the most recent issues from 2013
related to the '--cookies' option, and the related work arounds.. I'm
preparing myself to venture down this rabbit hole, but maybe there are
better ideas.

Any suggestions on how to do this?

Tim Scott

unread,
Nov 18, 2015, 12:50:02 AM11/18/15
to casp...@googlegroups.com, Neha Chriss
I do this all the time...but in reverse. That is, I log in, then check that I am logged in, sometimes with a cookie. Why would you expect a user to logged in if your script did not log them in? How could that happen?

Here’s an example:

get_cookie = (name) ->
  cookie = casper.evaluate -> document.cookie
  value = "; #{cookie}"
  parts = value.split "; #{name}="
  parts.pop().split(';').shift() if parts.length == 2

casper.thenOpen login_url ->
  @sendKeys "form.login#username", username
  @sendKeys "form.login#password”, password
  @click ’form.login input[type=submit]'
  @waitFor ->
    get_cookie(‘account_info') != undefined
  , ->
    # woo hoo, logged in!
  , ->
    # dang, login failed
  , 30000
--
CasperJS homepage & documentation: http://casperjs.org/
CasperJS @github: https://github.com/n1k0/casperjs

You received this message because you are subscribed to the Google Groups "casperjs" group.
Visit this group at http://groups.google.com/group/casperjs?hl=en.
---
You received this message because you are subscribed to the Google Groups "CasperJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casperjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Neha Chriss

unread,
Nov 19, 2015, 10:16:15 AM11/19/15
to casp...@googlegroups.com
Thanks Tim, that's a reasonable question - I'm using wraith to capture
screen shots, and the tool requires additional scripting to handle a
user login. The problem is that it spawns separate casperjs processes
to capture specific selectors. I have a global script that immediately
authenticates via form login, but there were other issues preventing
me from knowing whether that auth info was shared for specific
'component' captures. In wraith, one can specify a selector for a
page, and it handles the capturing/image diffing of the page element.

This means that while my initial script may auth correctly, subsequent
captures may have not - I wanted to check for the presence of a cookie
parameter in my global script to verify this, and on failure, execute
an explicit script to login. The risk without that check, is that the
global script may *always* execute a form fill/login, waisting
resources. The second objective was to store cookie info from the
initial auth routine, and direct newer casper sessions to use that for
the duration of the entire wraith session.

I'm using JS, but thanks for the coffeescript, I believe I can make
sense from this.

Best,
Neha

Tim Scott

unread,
Nov 19, 2015, 10:21:06 AM11/19/15
to casp...@googlegroups.com
http://js2coffee.com works both ways.
Reply all
Reply to author
Forward
0 new messages