I have below code snippet.
var admin_url = "http://www.sampledomain.com/";
var casper = require('casper').create();
casper.start();
casper.thenOpen(
"http://www.test.com", {
method: "post",
data: {
param1: "some data"
}
},
function() {
what_i_want = this.getPageContent().split("[")[1].split("]")[0]
admin_url += what_i_want;
}
);
casper.thenOpen(admin_url, function() {
this.echo(this.getCurrentUrl());
});
casper.run();
I first post to http://www.test.com, then use the returned data, I try to modify the admin_url, which is my second URL that need to access. However, my testing shows that the second thenOpen
method still got the old admin_url
value and did not pick up the latest updated value (notice that I've updated it in the first thenOpen
method). What's the reason of this issue and how to fix it?
When you call
thenOpen
theadmin_url
is passed by value, butthenOpen
itself is executed asynchronously. You can see it this way: You add some steps to the queue and when you callrun
, the queue begins to execute with the initial steps and their accompanying data. So you need to add your lastthenOpen
step dynamically to the queue when theadmin_url
is complete:There is also the possiblity to break
thenOpen
into its sub-functions and useadmin_url
as a global variable:It reduces the nesting which might be a good thing based on your preference.