Skip to content

Instantly share code, notes, and snippets.

@brianmhunt
Last active December 10, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmhunt/4365815 to your computer and use it in GitHub Desktop.
Save brianmhunt/4365815 to your computer and use it in GitHub Desktop.
A CasperJS wrapper for remote jQuery calls (untested)
class jQueryCasperWrapper
constructor: (@selector) ->
wrappedMethods = ['data', 'visible'] # ... plus all the other jQuery functions being wrapped
_.each(wrappedMethods, (method) => # lodash/underscore foreach -- just from my own preferences
wrapper = (args...) => @RemotejQuery(method, args)
@[method] = wrapper
)
RemotejQuery: (methodName, args) ->
remote_fn = (_selector, _methodName, _args) ->
jobj = jQuery(_selector)
return jobj[_methodName].apply(jobj, _args)
casper.evaluate(remote_fn,
_selector: @selector
_methodName: methodName
_args: args
)
jQueryInCasper = (selector) ->
return new jQueryCasperWrapper(selector)
casper.$ = jQueryInCasper
# now we can do casper.$("hello").data() or casper.$("#header").text()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment