Javier Sega
Javier Sega

Reputation: 93

How to set a value for wait() in CasperJS?

This is my complete code .. What I want is that the casper.wait have a random waiting time of 1-3 seconds. If I put " casper.wait (1000, function () {" entering a numeric value , if it works, however casper.wait (time , function () { entering the variable value is not working.

casper.then(function() {

  this.echo('Looking random number.....');
  rrandom = Math.round(Math.random() * 3);

  if (rrandom == 1) {
    time = 1000
  }
  if (rrandom == 2) {
    time = 2000
  }
  if (rrandom == 3) {
    time = 3000
  }
});

casper.wait(time, function() {
  this.echo('result');
});


casper.run();

Upvotes: 0

Views: 755

Answers (1)

Vaviloff
Vaviloff

Reputation: 16838

In your sample rrandom sometimes will be equal to 0, because Math.round() rounds values of < 0.49 to zero. Thus time will be sometimes undefined, breaking the script.

I would suggest something like this:

var time;
casper.then(function() {
  var maxSecTimeout = 3;

  this.echo('Pausing for ' + maxSecTimeout + ' seconds');

  time = Math.ceil(Math.random() * maxSecTimeout) * 1000;  
});

casper.wait(time, function() {
  this.echo('result');
});

casper.run();

Upvotes: 1

Related Questions