moonlight-coding / casper-nodejs

Control CasperJS from your nodejs application.
MIT License
15 stars 5 forks source link

How to chain? #2

Closed nikkwong closed 8 years ago

nikkwong commented 8 years ago

How can we chain casper calls (i.e. for filling out forms and getting to the results from returned page)?

pelallemant commented 8 years ago

You can call casper.then in the second function of a previous casper.then call.

Example here: https://github.com/moonlight-coding/casper-nodejs/blob/master/examples/01-load-page/index2.js#L37

It allows me to use pagination dynamically (get the count of pages, and use casper.then until the last page is loaded).

nikkwong commented 8 years ago

How can you do this? Does this module have the ability to expose casper.fill (or similar functionality) to trigger the request to a new page? Won't casper.then only load the url passed to casper_nodejs.create?

Thanks :-)

pelallemant commented 8 years ago

casper-nodejs is just a library which run a casperjs server on your computer (a program which starts casperjs and doesn't let it die by calling casper.wait().), and allows to give orders inside your scripts to that casperjs server.

You can call this.fill in the first function, which is called inside a real 'casper.then', not the 'casper.then' defined by my library. You have access to any of the CasperJS methods inside the first callback parameter, this callback is sent to the CasperJS client then executed, but I'm not sure i handle the case where there is callbacks in parameter (this.wait etc..). I will check / test this in the evening.

https://github.com/moonlight-coding/casper-nodejs/blob/master/examples/02-casper-official-examples/00-google-clean.js#L8

Can you give me an example of page you wanna scrap (from another website but with the same pagination system or search menu that the one you wanna scrap) in order I write the script then put it in the examples ?

nikkwong commented 8 years ago

Awesome! You've been super helpful. In one of my native casper scripts I navigate through page after page by switching between calls to casper.thenEvaluate, which triggers a pageload that casper.waitFor() confirms, then calling casper.thenEvaluate to evaluate actions on that subsequent page, etc. I just don't see how to do that with this module since we don't have casper after the first callback.

Anyways, thanks! Great work.

pelallemant commented 8 years ago

I created a new branch, I'll add the support for casper.thenEvaluate and casper.waitFor in both callbacks during this week.

pelallemant commented 8 years ago

I spent a bit of time to remind about the project and see how it could be done.

I'll add an alias for casper.thenEvaluate, but in fact you can do it another way :)

casper.then([function executed_in_casperjs_context(param) {
  // here you can write the code to evaluate
  return {'test' : 45, 'param': param};
}, ['Param !']], X);

With X = function executed_after_in_this_context(ret_from_execution) { } or null. If you don't put a parameter, i assume only the first method must be executed and not in casperjs but in the current context.

For casper.waitFor I really have to add the action.

nikkwong commented 8 years ago

Cool man this is really awesome—great work, would love to see a write up on how you thought it through.

Thx for the help.

pelallemant commented 8 years ago

The only motivations of the projects are:

The core corresponds to 1 program and a library:

Now to correspond to CasperJS, i have to wrap certain functionnalities in order to use this library like you would use CasperJS. I'll do that before the end of the week.