Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added callback and tests for injectJs (ref amir20#17)
  • Loading branch information
sgentle committed Jan 25, 2012
1 parent bf0beaf commit 5f36ea4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"name": "phantom",
"description": "PhantomJS wrapper for Node",
"homepage": "https://github.com/sgentle/phantomjs-node",
"version": "0.3.2",
"version": "0.3.3",
"repository": {
"type": "git",
"url": "git://github.com/sgentle/phantomjs-node.git"
Expand Down
6 changes: 4 additions & 2 deletions shim.coffee
Expand Up @@ -47,12 +47,14 @@ mkwrap = (src, pass=[], special={}) ->
obj

pageWrap = (page) -> mkwrap page,
['open','includeJs','injectJs','sendEvent','release','uploadFile']
['open','includeJs','sendEvent','release','uploadFile']
injectJs: (js, cb=->) -> cb page.injectJs js
evaluate: (fn, cb=->) -> cb page.evaluate fn
render: (file, cb=->) -> page.render file; cb()

_phantom = mkwrap phantom,
['exit', 'injectJs'],
['exit'],
injectJs: (js, cb=->) -> cb phantom.injectJs js
createPage: (cb) -> cb pageWrap webpage.create()


Expand Down
12 changes: 10 additions & 2 deletions shim.js
Expand Up @@ -1636,7 +1636,11 @@ require.define("/shim.coffee", function (require, module, exports, __dirname, __
};

pageWrap = function(page) {
return mkwrap(page, ['open', 'includeJs', 'injectJs', 'sendEvent', 'release', 'uploadFile'], {
return mkwrap(page, ['open', 'includeJs', 'sendEvent', 'release', 'uploadFile'], {
injectJs: function(js, cb) {
if (cb == null) cb = function() {};
return cb(page.injectJs(js));
},
evaluate: function(fn, cb) {
if (cb == null) cb = function() {};
return cb(page.evaluate(fn));
Expand All @@ -1649,7 +1653,11 @@ require.define("/shim.coffee", function (require, module, exports, __dirname, __
});
};

_phantom = mkwrap(phantom, ['exit', 'injectJs'], {
_phantom = mkwrap(phantom, ['exit'], {
injectJs: function(js, cb) {
if (cb == null) cb = function() {};
return cb(phantom.injectJs(js));
},
createPage: function(cb) {
return cb(pageWrap(webpage.create()));
}
Expand Down
8 changes: 8 additions & 0 deletions test/basic.coffee
Expand Up @@ -30,6 +30,14 @@ describe "The phantom module"
assert.ok ver.minor >= 3, "minor version too low"


"which can inject Javascript from a file":
topic: t (p) ->
p.injectJs 'test/inject.js', (success) =>
@callback null, (success)

"and succeed": (success) ->
assert.ok success, "Injection should return true"

"which can create a page":
topic: t (p) -> p.createPage (page) => @callback null, page

Expand Down
2 changes: 2 additions & 0 deletions test/inject.js
@@ -0,0 +1,2 @@
//This isn't really a very good test
//(function(){var foo = 3;})();
9 changes: 9 additions & 0 deletions test/page.coffee
Expand Up @@ -64,6 +64,15 @@ describe "Pages"

"which is correct": (title) ->
assert.equal title, "Test page title"

"can inject Javascript from a file":
topic: t (page) ->
page.injectJs 'test/inject.js', (success) =>
@callback null, (success)

"and succeed": (success) ->
assert.ok success, "Injection should return true"


"can evaluate DOM nodes":
topic: t (page) ->
Expand Down

0 comments on commit 5f36ea4

Please sign in to comment.