Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomFreudenberg/994f107a029d152049eb to your computer and use it in GitHub Desktop.
Save TomFreudenberg/994f107a029d152049eb to your computer and use it in GitHub Desktop.

This is a very simple casperjs example, to show how to simulate a user accessing a meteor application like the todos example.

The casper script will do:

  1. connect to the todos example app
  2. add a new list
  3. iterate through all existing lists and open/load each once

Between each step there is a small random time, so that the "loads" are not aligned.

You can also enable casper debug to see a lot more debugging output that is very usefull for usability checks.

If you open your browser in parallel you will notice the growing number of the lists.

This was made and tested on MAC OSX with node@0.10.38, npm@1.4.28 and meteor@1.1.0.2.

First create a simple new example to use this demo script.

I expect, that the meteor server will run on default URL (localhost:3000)

# create a new meteor example

cd /tmp
meteor create --example todos
cd todos
meteor

Open an additional terminal to run the casper "user"

# install npms on first time
 
npm -g install casperjs
npm -g install phantomjs
npm -g install sugar

# get the casper script

cd /tmp
curl -L -O https://gist.githubusercontent.com/TomFreudenberg/994f107a029d152049eb/raw/48216827c9ab8e4170a404ed348930898ffe3afc/simple-actions-on-todos.casper.js

# run the magic

casperjs simple-actions-on-todos.casper.js

If you want to start a number of simulation processes in parallel, you can use bash technic like:

for ((i=1; i<=5; i++)) do casperjs simple-actions-on-todos.casper.js & done

Remark: I have added a number of requires that are not necessary on this casper script but I advise you to use them, cause those will help on more complex scripts.


  1. Casperjs module documentation
  2. Casperjs hompage
  3. Phantomjs homepage
  4. Sugarjs Features
/*==============================================================================*/
/* Casper to run some parts of todos app
/*==============================================================================*/
var _debug = true;
var _debug_casper = _debug && false;
var http_url = "http://localhost:3000";
var system = require('system');
var execv = require("child_process").execFile;
var utils = require('utils');
var sugar = require('sugar');
var casper = require('casper').create();
casper.options.timeout = 180000;
casper.options.verbose = _debug;
casper.options.logLevel = (_debug_casper) ? "debug" : "error";
casper.options.viewportSize = { width: 1280, height: 1400 };
casper.options.pageSettings = { userAgent: "Mozilla/5.0", loadImages: false, loadPlugins: false };
casper.options.onTimeout = function(timeout){
this.die("Overall TIMEOUT for script has reached, so going down!");
}
// will generate waitTime by random with waiting steps of 0,1 sec
function RandomTime(minWait, maxWait){
var range = Math.floor((maxWait - minWait) / 100) + 1;
var rnd = Math.floor(Math.random() * range) * 100;
return rnd + minWait;
}
casper.start(http_url, function(response){
var _status = response.status;
if (_status == '200') {
_debug && this.echo("Page: " + http_url + " loaded.");
} else {
this.die("Page not loaded! [" + _status + "]", 1);
}
});
casper.then(function(){
_debug && this.echo("Waiting random after page loaded.");
this.wait(RandomTime(100, 1000), function(){
_debug && this.echo("ok");
});
});
casper.then(function(){
_debug && this.echo("Append new List...");
this.click('#menu div.list-todos a.js-new-list');
this.wait(RandomTime(100, 500), function(){
_debug && this.echo("done");
});
});
casper.then(function(){
_debug && this.echo("Iterate through Lists...");
var lists = this.getElementsInfo('#menu div.list-todos a.list-todo');
this.eachThen(lists, function(response){
_debug && this.echo("Open List: " + response.data.attributes.title);
this.open(http_url + response.data.attributes.href).then(function(){
_debug && this.echo("ok, loaded!");
var waitTime = RandomTime(750, 2500);
_debug && this.echo("Waiting random ms: " + waitTime);
this.wait(waitTime, function(){
_debug && this.echo("done");
});
});
});
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment