Making thenOpen sync - PLEASE HELP, I REALLY NEED IT

50 views
Skip to first unread message

robin.o...@gmail.com

unread,
Mar 6, 2016, 7:37:34 PM3/6/16
to CasperJS
My Program logic is becoming a mess as the code gets bigger and I refactor it to functions. I loosing control over all the steps that are async when in fact, in my case are needed to be depended on one another. A simple example is the following: 

Let's say I have a function in my code that contains the step thenOpen which determines the return value. How can I make the function wait for the thenOpen to complete and only then return the value?


What is the best workaround for this scenario? I really need an expect advise

bruce

unread,
Mar 6, 2016, 8:37:57 PM3/6/16
to casp...@googlegroups.com
Hey Robin.

Post your code, give us some things to look at

--
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.

robin.o...@gmail.com

unread,
Mar 7, 2016, 8:54:25 AM3/7/16
to CasperJS
Please find two examples: First, with refactoring code in functions and the second as a long function (In real life, it quickly becomes spaghetti code)

Example ONE

var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});

function importantProcessing(){
this.log("Entering importantProcessing");
this.thenOpen("http://phantomjs.org/", function processPhantomjs(){
return "Processed Data";
});
this.log("Exiting importantProcessing");
}

function insertToDB(processedData){
this.log("Inserting the data "+processedData);
}

casper.start("http://casperjs.org/", function startStep(){
var processedData = null;
this.then(function importantProcessingStep() {
processedData = importantProcessing();
});
this.then(function insertToDBStep() {
insertToDB(processedData);
});
});

casper.run();

Prints the following in console:
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://casperjs.org/, HTTP GET
[debug] [phantom] Navigation requested: url=http://casperjs.org/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://casperjs.org/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step startStep 2/2 http://casperjs.org/ (HTTP 200)
[info] [phantom] Step startStep 2/2: done in 1835ms.
[info] [phantom] Step importantProcessingStep 3/4 http://casperjs.org/ (HTTP 200)
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"


Example TWO
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});

casper.start("http://casperjs.org/", function startStep(){
var processedData = null;
this.then(function importantProcessingStep() {
this.log("Entering importantProcessing");
this.thenOpen("http://phantomjs.org/", function processPhantomjs(){
processedData = "Processed Data";
});
this.log("Exiting importantProcessing");
});

this.then(function insertToDBStep() {
this.log("Inserting the data "+processedData);
});
});

casper.run();

Prints the following in console:
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://casperjs.org/, HTTP GET
[debug] [phantom] Navigation requested: url=http://casperjs.org/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://casperjs.org/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step startStep 2/2 http://casperjs.org/ (HTTP 200)
[info] [phantom] Step startStep 2/2: done in 2523ms.
[info] [phantom] Step importantProcessingStep 3/4 http://casperjs.org/ (HTTP 200)
[debug] [phantom] Entering importantProcessing
[debug] [phantom] Exiting importantProcessing
[info] [phantom] Step importantProcessingStep 3/4: done in 2543ms.
[debug] [phantom] opening url: http://phantomjs.org/, HTTP GET
[debug] [phantom] Navigation requested: url=http://phantomjs.org/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://phantomjs.org/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step processPhantomjs 5/6 http://phantomjs.org/ (HTTP 200)
[info] [phantom] Step processPhantomjs 5/6: done in 3647ms.
[info] [phantom] Step insertToDBStep 6/6 http://phantomjs.org/ (HTTP 200)
[debug] [phantom] Inserting the data Processed Data
[info] [phantom] Step insertToDBStep 6/6: done in 3670ms.
[info] [phantom] Done 6 steps in 3686ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"
Reply all
Reply to author
Forward
0 new messages