Injecting additional javascript libraries into a page with Geb

Once in a while, we might want to add an additional library into an existing page with Geb.

The easy way to do so is to add a helper method called injectLibrary as follows:


def injectLibrary( library ){

     js.exec("document.body.appendChild(document.createElement('script')).src='$library'");

}

Then, whenever you want to add a new library, simply call

injectLibrary( 'http://sinonjs.org/releases/sinon-1.4.2.js' )

This would add the javascript library you have specified in your link into your page.

There are a few scenarios where I have found this to be useful:

  • Adding additional libraries in regression tests of your actual site.
  • Getting better drag and drop support using Syn.js.
  • Injecting Sinon.js to use the fakeTimer functionality when scraping websites.
  • Adding jQuery to a page to manipulate document elements to make them easier to find for scraping.

One thought on “Injecting additional javascript libraries into a page with Geb

  1. Pingback: Injecting javascript | Jlaura

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s