From ccbc7330fc1fa339a4816b1f6872afb1bd63716d Mon Sep 17 00:00:00 2001 From: RaphaelRheault Date: Wed, 23 Oct 2013 13:29:12 -0400 Subject: [PATCH 1/3] Added page.post(...) function Added page.post function to allow a render through a post action. page.post(url, data[, callback]) url : where to post data : html encoded string with the body of the request callback : callback function (optional) Example : var phantom=require('node-phantom'); phantom.create(function(error,ph){ ph.createPage(function(err,page){ var data = "email=test@domain.com&pass=my_pass"; page.post('http://domain.com/login',data, function(err,status){ //Do stuff here }); }); }); --- node-phantom.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/node-phantom.js b/node-phantom.js index 8d8f31d..0091d54 100644 --- a/node-phantom.js +++ b/node-phantom.js @@ -92,6 +92,13 @@ module.exports={ request(socket, [id, 'pageOpenWithCallback', url], callback); } }, + post:function(url, data, callback){ + if(callback === undefined){ + request(socket, [id, 'pagePost', url, data]); + }else{ + request(socket, [id, 'pagePostWithCallback', url, data], callback); + } + }, close:function(callback){ request(socket,[id,'pageClose'],callbackOrDummy(callback)); }, From aa0a6efdf548470e399124cf9b9cb1336c21b40f Mon Sep 17 00:00:00 2001 From: RaphaelRheault Date: Wed, 23 Oct 2013 13:32:47 -0400 Subject: [PATCH 2/3] Added the cases used by the page.post function Added two cases : pagePost and pagePostWithCallback Both of them calls Phantom's open(url,'post',data,callback) --- bridge.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bridge.js b/bridge.js index f48f22e..aab26d5 100644 --- a/bridge.js +++ b/bridge.js @@ -74,6 +74,14 @@ controlpage.onAlert=function(msg){ respond([id, cmdId, 'pageOpened', status]); }); break; + case 'pagePost': + page.open(request[3], 'post', request[4]); + break; + case 'pagePostWithCallback': + page.open(request[3], 'post', request[4], function(status){ + respond([id, cmdId, 'pageOpened', status]); + }); + break; case 'pageClose': page.close(); respond([id,cmdId,'pageClosed']); From 3e9d3cce1d700d9cd300bfbff3a68468cb19754c Mon Sep 17 00:00:00 2001 From: RaphaelRheault Date: Wed, 23 Oct 2013 13:35:11 -0400 Subject: [PATCH 3/3] Test for the page.post function Function to test the page.post function. Actually, it's only a copy of testpageopen.js that calls page.post instead. --- test/testpagepost.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/testpagepost.js diff --git a/test/testpagepost.js b/test/testpagepost.js new file mode 100644 index 0000000..34c63f8 --- /dev/null +++ b/test/testpagepost.js @@ -0,0 +1,23 @@ +var http=require('http'); +var phantom=require('../node-phantom'); + +var server=http.createServer(function(request,response){ + response.writeHead(200,{"Content-Type": "text/html"}); + response.end('Hello World'); +}).listen(); + +exports.testPhantomPageOpen=function(beforeExit,assert){ + phantom.create(function(error,ph){ + assert.ifError(error); + ph.createPage(function(err,page){ + assert.ifError(err); + var data = "test=test"; + page.post('http://localhost:'+server.address().port,data,function(err,status){ + assert.ifError(err); + assert.equal(status,'success'); + server.close(); + ph.exit(); + }); + }); + }); +};