3

Can CasperJS (with phantomJS) be run directly in the browser and not via the command line ?

I would like to run an interactive test from the browser where i take inputs from the user and proceed accordingly. I would like to run casperJS as a normal javascript function which is executed in a browser and be able to produce the output of each command to the user, such that the user can decide what he wants to do next.

Sanat Vij
  • 207
  • 1
  • 4
  • 13
  • can you be more specific as to why you would like to run the test on the browser? if the user is interacting with the browser, then why do you need casper to repeat the same interaction? – Saad Aug 15 '13 at 17:45
  • My goal is to create a scripting session, where the user does not interact directly with the target web page, and is yet able to ascertain the results of specific interactions with the web page in question. – Sanat Vij Aug 15 '13 at 18:53
  • I think you might be looking for js to do guided tours, https://github.com/jeff-optimizely/Guiders-JS is a good example, there are also other frameworks that allow for this. But if you want to do assertions on a visible session, casperjs is not the tool for the job. Also, if you are using the users browser as the platform for testing, you dont really need phantomJS either, you may need to combine customjs (or open source js) code that handles navigations with some sort of assertion library, though I don't really see the point of this. – Saad Aug 15 '13 at 20:06

1 Answers1

4

The short answer is: No, not as you've described it. CasperJS runs on, and depends on, PhantomJS, which is a browser - it can't run in a user's browser.

If you want to offer an interactive CasperJS session through a webpage, you'd need to run Casper/Phantom on a server, and then proxy the input/ouput through that server. Basically any operation that requires fetching other webpages and scraping or interacting with them is likely to require a server-side mechanism, because the Same Origin Policy makes this quite difficult to implement using client-side JavaScript (though a browser extension might be able to do it).

nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
  • I am looking to do what you mentioned in your second paragraph. Can you explain that a bit more detail on http://stackoverflow.com/questions/18327862/run-execute-casperjs-script-by-clicking-a-button-on-webpage – user2129794 Aug 20 '13 at 05:59
  • 1
    Perfect ! I implemented the second paragraph by running the embedded mongose web server module (part of the phantomjs distributable). Then it was simply a matter of passing commands to the web server using jsonp, running them using js eval and returning the result back to the calling page ! – Sanat Vij Aug 23 '13 at 10:31
  • 1
    Sounds good - note that using `eval` here as you describe is really quite dangerous if run in the PhantomJS context, since anyone accessing the site can write JS that can access and write to server files, effectively giving them an open door to the server. Just FYI. – nrabinowitz Aug 23 '13 at 16:27
  • Noted. The current setup is just being used as a POC. In the production version, the user inputs would be routed through an application server, where they would be filtered, and the only the application server would be allowed to pass on the commands to the mongose web server, thereby preventing any malicious user instructions from being executed. – Sanat Vij Aug 25 '13 at 11:45