How do I inject JS with CasperJS?

108 Views Asked by At

could you tell me please how do I inject js into a page with CasperJS?

I want this after page has been loaded:

  • inject alert(Hello, World!') or/and
  • inject window.location=stackoverflow.com make screenshot.

Pretty simple.

1

There are 1 best solutions below

0
Mario Nikolaus On

To switch context from Casper's one to browser simply call casper's evaluate method. It enables to evaluate your code in Browser context.

So calling alert should look something like this:

casper.evaluate(function() {
  alert('Hello, World!');
})

You can read documentation for evaluate method here

As this answers both of your questions I would just like you to know that navigating to other web can be more easily done with Casper open method as your code will be much easier to read and maintain.