Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving raw HTTP responses #21

Open
mobydick123 opened this issue Feb 7, 2016 · 0 comments
Open

Retrieving raw HTTP responses #21

mobydick123 opened this issue Feb 7, 2016 · 0 comments

Comments

@mobydick123
Copy link

Hello Travis,

really interesting work you have been doing with jquery.go.js.
I am quite new to Jquery, node.js, Phantom.js and JavaScript in general and having a few problems wrapping my head around all of it.

I was able to successfully reproduce the login functionality for the site I'm testing and am now stuck at the next step, in which I want to retrieve the raw HTTP response of my next request, which will be JSON only.

What I have right now is based on makemeasandwich.js.

var async =   require('async'),
$ =       require('jquerygo'),
path =    require('path'),
fs =      require('fs');

// Add some default configs.
$.config.site = 'https://host.com';
$.config.addJQuery = false;

var login = function(done) {
  async.series([
    $.go(false, 'visit', '/login'),
    $.go(false, 'waitForElement', '#email'),
    debugCapture('login1'),
    $('#email').go('val', '[email protected]'),
    $('#password').go('val', 'securepassword'),
    debugCapture('login2'),
    $("form").go('attr','id','validForm'),
    $('#validForm').go('submit'),
    sleep(3000),
    debugCapture('login3'),
    print('Successfully logged in.'),
    sleep(3000),
  ], done);
};

var viewJournalEntries = function(done){

  $.config.addJQuery = true;
  async.series([
    $.go(false, 'visit', '/api/journals/show/546'),
    $.getPage(function(page) {
      // Is this right, what do I need to do here??
    }),
    debugCapture('step2'),
    ],done);
}

var debugCapture = function(fileName) {
  // if (config.get('debug')) {
    return capture(fileName);
  // }
  return function(done) { done(); }
};

/**
 * Method to capture and ensure the screenshots directory exists.
 */
var capture = function(fileName) {
  // Return the async function.
  return function(done) {

    // The directory to store the screenshots.
    var dir = __dirname + '/screenshots-ls';

    // Check that the directory exists.
    fs.exists(dir, function(exists) {
      if (exists) {
        $.capture(dir + '/' + fileName + '.png', done);
      }
      else {
        fs.mkdir(dir, function(err) {
          if (err) return done(err);
          $.capture(dir + '/' + fileName + '.png', done);
        });
      }
    });
  }
}

/**
 * Helper to print something when it is executed.
 *
 * @param {type} text
 * @returns {unresolved}
 */
var print = function(text) {
  return function(done) {
    console.log(text);
    done();
  };
};

/**
 * Helper function to take a pause...
 *
 * @param {type} time
 * @returns {unresolved}
 */
var sleep = function(time) {
  return function(done) {
    setTimeout(done, time);
  };
};

async.series([
  login,
  viewJournalEntries,
], function() {
  $.close();
});

Again, the login works like a charm and I can call the viewJournalEntries function which calls the url /api/journals/show/546 that returns a typical JSON string like this:
{"data":{"id":546,"user_id":1,[etc...]
The screen capture proofs that it works, but I am just not sure how to proceed to get the JSON into a variable that I can parse.

Any help/directions are appreciated.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant