WWW-Mechanize-PhantomJS
view release on metacpan or search on metacpan
lib/WWW/Mechanize/PhantomJS/ghostdriver/request_handlers/session_request_handler.js view on Meta::CPAN
},
_executeAsyncCommand = function(req, res) {
var postObj = JSON.parse(req.post);
_log.debug("_executeAsyncCommand", JSON.stringify(postObj));
if (typeof(postObj) === "object" && postObj.script && postObj.args) {
_protoParent.getSessionCurrWindow.call(this, _session, req).setOneShotCallback("onCallback", function() {
_log.debug("_executeAsyncCommand.callbackArguments", JSON.stringify(arguments));
res.respondBasedOnResult(_session, req, arguments[0]);
});
_protoParent.getSessionCurrWindow.call(this, _session, req).evaluate(
"function(script, args, timeout) { " +
"return (" + require("./webdriver_atoms.js").get("execute_async_script") + ")" +
"(script, args, timeout, callPhantom, true); " +
"}",
postObj.script,
postObj.args,
_session.getScriptTimeout());
} else {
throw _errors.createInvalidReqMissingCommandParameterEH(req);
}
},
_getWindowHandle = function (req, res) {
var handle;
// Get current window handle
handle = _session.getCurrentWindowHandle();
if (handle !== null) {
res.success(_session.getId(), handle);
} else {
throw _errors.createFailedCommandEH(_errors.FAILED_CMD_STATUS_CODES.NoSuchWindow,
"Current window handle invalid (closed?)",
req,
_session);
}
},
_getWindowHandles = function(req, res) {
res.success(_session.getId(), _session.getWindowHandles());
},
_getScreenshotCommand = function(req, res) {
var rendering = _protoParent.getSessionCurrWindow.call(this, _session, req).renderBase64("png");
res.success(_session.getId(), rendering);
},
_getUrlCommand = function(req, res) {
// Get the URL at which the Page currently is
var result = _protoParent.getSessionCurrWindow.call(this, _session, req).url;
res.respondBasedOnResult(_session, res, {status: 0, value: result});
},
_canonicalURL= function( url ) {
var URL= require("./third_party/parseuri.js");
//console.log(''+url + " => " + JSON.stringify(URL.parse(url)));
var canonicalURL= URL.parse(url);
if( !canonicalURL.path || 0==canonicalURL.path.length ) {
// Add the trailing slash
canonicalURL.path='/';
};
var s= canonicalURL.protocol + '://'
+ (canonicalURL.authority ? (canonicalURL.authority) : '')
+ (canonicalURL.path)
+ (canonicalURL.query ? '?' + canonicalURL.query : '')
+ (canonicalURL.anchor ? '#' + canonicalURL.anchor : '');
return s
},
_postUrlCommand = function(req, res) {
// Load the given URL in the Page
var postObj = JSON.parse(req.post),
currWindow = _protoParent.getSessionCurrWindow.call(this, _session, req);
_log.debug("_postUrlCommand", "Session '"+ _session.getId() +"' is about to load URL: " + postObj.url);
if (typeof(postObj) === "object" && postObj.url) {
// Switch to the main frame first
currWindow.switchToMainFrame();
var canonicalURL= _canonicalURL(postObj.url);
// Create our placeholder for the response
var response= {
requestedURL: postObj.url,
finalURL: canonicalURL,
status: undefined, // Well, if we don't know any better...
};
// Load URL and wait for load to finish (or timeout)
currWindow.execFuncAndWaitForLoad(
function() {
currWindow.onResourceError = function(res) {
var url= _canonicalURL(res.url);
if (3 == res.errorCode && url == response.finalURL ) { // DNS error
var passthrough= ["status", "headers", "statusText", "contentType", "time","url"];
for( var i in passthrough ) {
response[passthrough[i]] = res[passthrough[i]];
};
response.status= 500; // Internal error, resp. DNS error
response.statusText= res.errorString;
};
};
currWindow.onResourceReceived = function(res) {
var url= _canonicalURL(res.url);
if ('start' == res.stage && url == response.finalURL && 400 <= res.status ) {
var passthrough= ["status", "headers", "statusText", "contentType", "time","url"];
for( var i in passthrough ) {
response[passthrough[i]] = res[passthrough[i]];
};
};
if ('end' == res.stage && url == response.finalURL && !response.status) {
if( 300 <= res.status && res.status <= 399 ) {
// Update where we will end up next
response.finalURL= _canonicalURL(res.redirectURL);
} else {
lib/WWW/Mechanize/PhantomJS/ghostdriver/request_handlers/session_request_handler.js view on Meta::CPAN
} else {
// Neither "element" nor "xoffset/yoffset" were provided
throw _errors.createInvalidReqMissingCommandParameterEH(req);
}
},
_postMouseClickCommand = function(req, res, clickType) {
var postObj = {},
mouseButton = "left";
// normalize click
clickType = clickType || "click";
// The protocol allows language bindings to send an empty string (or no data at all)
if (req.post && req.post.length > 0) {
postObj = JSON.parse(req.post);
}
// Check that either an Element ID or an X-Y Offset was provided
if (typeof(postObj) === "object") {
// Determine which button to click
if (typeof(postObj.button) === "number") {
// 0 is left, 1 is middle, 2 is right
mouseButton = (postObj.button === 2) ? "right" : (postObj.button === 1) ? "middle" : "left";
}
// Send the Mouse Click as native event
_session.inputs.mouseButtonClick(_session, clickType, mouseButton);
res.success(_session.getId());
} else {
// Neither "element" nor "xoffset/yoffset" were provided
throw _errors.createInvalidReqMissingCommandParameterEH(req);
}
},
_postCookieCommand = function(req, res) {
var postObj = JSON.parse(req.post || "{}"),
currWindow = _protoParent.getSessionCurrWindow.call(this, _session, req);
// If the page has not loaded anything yet, setting cookies is forbidden
if (currWindow.url.indexOf("about:blank") === 0) {
// Something else went wrong
_errors.handleFailedCommandEH(_errors.FAILED_CMD_STATUS_CODES.UnableToSetCookie,
"Unable to set Cookie: no URL has been loaded yet",
req,
res,
_session);
return;
}
if (postObj.cookie) {
// set default values
if (!postObj.cookie.path) {
postObj.cookie.path = "/";
}
if (!postObj.cookie.secure) {
postObj.cookie.secure = false;
}
if (!postObj.cookie.domain) {
postObj.cookie.domain = require("./third_party/parseuri.js").parse(currWindow.url).host;
}
if (postObj.cookie.hasOwnProperty('httpOnly')) {
postObj.cookie.httponly = postObj.cookie.httpOnly;
delete postObj.cookie['httpOnly'];
} else {
postObj.cookie.httponly = false;
}
// JavaScript deals with Timestamps in "milliseconds since epoch": normalize!
if (postObj.cookie.expiry) {
postObj.cookie.expiry *= 1000;
}
if (!postObj.cookie.expiry) {
// 24*60*60*365*20*1000 = 630720000 number of milliseconds in 20 years
postObj.cookie.expiry = Date.now() + 630720000000;
}
// If the cookie is expired OR if it was successfully added
if ((postObj.cookie.expiry && postObj.cookie.expiry <= new Date().getTime()) ||
currWindow.addCookie(postObj.cookie)) {
// Notify success
res.success(_session.getId());
} else {
// Something went wrong while trying to set the cookie
if (currWindow.url.indexOf(postObj.cookie.domain) < 0) {
// Domain mismatch
_errors.handleFailedCommandEH(_errors.FAILED_CMD_STATUS_CODES.InvalidCookieDomain,
"Can only set Cookies for the current domain",
req,
res,
_session);
} else {
// Something else went wrong
_errors.handleFailedCommandEH(_errors.FAILED_CMD_STATUS_CODES.UnableToSetCookie,
"Unable to set Cookie",
req,
res,
_session);
}
}
} else {
throw _errors.createInvalidReqMissingCommandParameterEH(req);
}
},
_getCookieCommand = function(req, res) {
// Get all the cookies the session at current URL can see/access
res.success(
_session.getId(),
_protoParent.getSessionCurrWindow.call(this, _session, req).cookies);
},
_deleteCookieCommand = function(req, res) {
if (req.urlParsed.chunks.length === 2) {
// delete only 1 cookie among the one visible to this page
_protoParent.getSessionCurrWindow.call(this, _session, req).deleteCookie(req.urlParsed.chunks[1]);
} else {
// delete all the cookies visible to this page
( run in 0.531 second using v1.01-cache-2.11-cpan-39bf76dae61 )