how do you detect when JSON has been loaded?

37 views
Skip to first unread message

Robert Poor

unread,
Dec 9, 2015, 1:38:29 AM12/9/15
to CasperJS
When I'm opening and clicking on things that fetch HTML, it's usually clear how to detect when the desired page has been loaded using waitForSelector() or one of its cousins.  For example, a common pattern I use might look like:

    casper.then(function a04() {
        casper.click(downloader.SUBMIT_BUTTON_CSS);
        casper.log('awaiting login');
    });
    casper.then(function a05() {
        casper.waitForSelector(MY_ACCOUNT_CSS,
                               function() { casper.log('successfully logged in'; },
                               function() { casper.die('login failed); });
    });

But what's the corresponding thing when I'm fetching JSON instead?  I can't use waitForSelector.  I could use just waitFor(), but what kind of things would I check for in the test function?  In other words, complete this snippet:

    casper.then(function a08() {
        casper.open('https://example.com/api/account/getInvoices', {
            method: 'GET',
            headers:  { 'Accept': 'application/json, text/plain, */*' }
        });
    });
    casper.then(function a09() {
      casper.waitFor(function check() { ... what goes here? ... },
                     function() { casper.log('loaded invoice data'; },
                     function() { casper.die('failed to load invoice data'; });
    });

Is it reasonable to use casper.getPageContent() inside my test function?  Or is there a more lightweight method I can use to see when the JSON has loaded?



Tim Scott

unread,
Dec 9, 2015, 10:10:44 AM12/9/15
to casp...@googlegroups.com
Neglected to CC the group.

On December 9, 2015 at 9:08:22 AM, Tim Scott (tsc...@lunaversesoftware.com) wrote:

Edit to my second example:

casper.thenOpen casper.cli.get(’some_url’),
  data = null
  @waitFor ->
    try
      data = JSON.parse @getPageContent()
      data.someExpectedElelment != null
    catch
      false
  , ->
    #do stuff with data


On December 9, 2015 at 9:06:28 AM, Tim Scott (tsc...@lunaversesoftware.com) wrote:

Richard,

In my experience, casperjs handles this well synchronously. Whenever I get json, my code looks like this (in coffeescript):

casper.thenOpen casper.cli.get(’some_url’), ->
  data = JSON.parse @getPageContent()
  # do stuff with data

Unless you need to wait for things async to the main request to happen, such as client side rendering or secondary resource requests, waiting should not be necessary in my experience.

However, you could do something like this:

data = null
casper.thenOpen casper.cli.get(’some_url’),
  @waitFor ->
    try
      data = JSON.parse @getPageContent()
      data.someExpectedElelment != null
    catch
      false
, ->
  #do stuff with data

-- 
Tim Scott


P.S. I know a guy named Lando B who knows a Robert Poor. Is that you?
--
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.

Robert Poor

unread,
Dec 9, 2015, 10:55:39 AM12/9/15
to CasperJS
Tim:

> waiting [for a JSON response] should not be necessary in my experience.

That's been my experience as well, but I wanted to make sure.

> (write a waitFor method that attempts to parse the JSON and catches failures)

That would be a necessary part of the getPageContent() approach I mentioned, but it's certainly not "lightweight".  But unless there's a better way, that's the approach I'll use.

Thanks.

- Rob

Reply all
Reply to author
Forward
0 new messages