Bug 941148 - Rename screenShot to takeScreenshot for webdriver compliance. r=dburns

This commit is contained in:
Andreas Tolfsen 2014-01-24 08:45:58 -05:00
parent cbdf066eef
commit bd32d26c75
3 changed files with 71 additions and 38 deletions

View File

@ -1349,20 +1349,29 @@ class Marionette(object):
return ApplicationCache(self)
def screenshot(self, element=None, highlights=None):
'''
Creates a base64-encoded screenshot of the element, or the current frame if no element is specified.
"""Takes a screenshot of a web element or the current frame.
:param element: The element to take a screenshot of. If None, will
take a screenshot of the current frame.
:param highlights: A list of HTMLElement objects to draw a red box around in the
returned screenshot.
'''
if element is not None:
The screen capture is returned as a lossless PNG image encoded
as a base 64 string. If the `element` argument is defined the
capture area will be limited to the bounding box of that
element. Otherwise, the capture area will be the bounding box
of the current frame.
:param element: The element to take a screenshot of. If None, will
take a screenshot of the current frame.
:param highlights: A list of HTMLElement objects to draw a red
box around in the returned screenshot.
"""
if element:
element = element.id
lights = None
if highlights is not None:
lights = [highlight.id for highlight in highlights if highlights]
return self._send_message("screenShot", 'value', id=element, highlights=lights)
if highlights:
lights = [highlight.id for highlight in highlights]
return self._send_message("takeScreenshot", "value",
id=element, highlights=lights)
@property
def orientation(self):

View File

@ -160,7 +160,7 @@ function startListeners() {
addMessageListenerId("Marionette:importScript", importScript);
addMessageListenerId("Marionette:getAppCacheStatus", getAppCacheStatus);
addMessageListenerId("Marionette:setTestName", setTestName);
addMessageListenerId("Marionette:screenShot", screenShot);
addMessageListenerId("Marionette:takeScreenshot", takeScreenshot);
addMessageListenerId("Marionette:addCookie", addCookie);
addMessageListenerId("Marionette:getCookies", getCookies);
addMessageListenerId("Marionette:deleteAllCookies", deleteAllCookies);
@ -260,7 +260,7 @@ function deleteSession(msg) {
removeMessageListenerId("Marionette:importScript", importScript);
removeMessageListenerId("Marionette:getAppCacheStatus", getAppCacheStatus);
removeMessageListenerId("Marionette:setTestName", setTestName);
removeMessageListenerId("Marionette:screenShot", screenShot);
removeMessageListenerId("Marionette:takeScreenshot", takeScreenshot);
removeMessageListenerId("Marionette:addCookie", addCookie);
removeMessageListenerId("Marionette:getCookies", getCookies);
removeMessageListenerId("Marionette:deleteAllCookies", deleteAllCookies);
@ -2061,9 +2061,15 @@ function importScript(msg) {
}
/**
* Saves a screenshot and returns a Base64 string
* Takes a screen capture of the given web element if <code>id</code>
* property exists in the message's JSON object, or if null captures
* the bounding box of the current frame.
*
* If given an array of web element references in
* <code>msg.json.highlights</code>, a red box will be painted around
* them to highlight their position.
*/
function screenShot(msg) {
function takeScreenshot(msg) {
let node = null;
if (msg.json.id) {
try {
@ -2075,7 +2081,7 @@ function screenShot(msg) {
}
}
else {
node = curFrame;
node = curFrame;
}
let highlights = msg.json.highlights;
@ -2100,36 +2106,42 @@ function screenShot(msg) {
left = rect.left;
}
var canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
var canvas = document.createElementNS("http://www.w3.org/1999/xhtml",
"canvas");
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
// Draws the DOM contents of the window to the canvas
ctx.drawWindow(win, left, top, width, height, 'rgb(255,255,255)');
ctx.drawWindow(win, left, top, width, height, "rgb(255,255,255)");
// This section is for drawing a red rectangle around each element passed in via the highlights array
// This section is for drawing a red rectangle around each element
// passed in via the highlights array
if (highlights) {
ctx.lineWidth = "2";
ctx.strokeStyle = "red";
ctx.save();
for (var i = 0; i < highlights.length; ++i) {
var elem = elementManager.getKnownElement(highlights[i], curFrame)
var elem = elementManager.getKnownElement(highlights[i], curFrame);
rect = elem.getBoundingClientRect();
var offsetY = -top;
var offsetX = -left;
// Draw the rectangle
ctx.strokeRect(rect.left + offsetX, rect.top + offsetY, rect.width, rect.height);
ctx.strokeRect(rect.left + offsetX,
rect.top + offsetY,
rect.width,
rect.height);
}
}
// Return the Base64 String back to the client bindings and they can manage
// saving the file to disk if it is required
var data_url = canvas.toDataURL("image/png","");
sendResponse({value: data_url.substring(data_url.indexOf(",") + 1)}, msg.json.command_id);
// Return the Base64 encoded string back to the client so that it
// can save the file to disk if it is required
var dataUrl = canvas.toDataURL("image/png", "");
var data = dataUrl.substring(dataUrl.indexOf(",") + 1);
sendResponse({value: data}, msg.json.command_id);
}
//call register self when we get loaded
// Call register self when we get loaded
registerSelf();

View File

@ -2208,16 +2208,25 @@ MarionetteServerConnection.prototype = {
},
/**
* Takes a screenshot of a DOM node. If there is no node given a screenshot
* of the window will be taken.
*/
screenShot: function MDA_saveScreenshot(aRequest) {
* Takes a screenshot of a web element or the current frame.
*
* The screen capture is returned as a lossless PNG image encoded as
* a base 64 string. If the <code>id</code> argument is not null
* and refers to a present and visible web element's ID, the capture
* area will be limited to the bounding box of that element.
* Otherwise, the capture area will be the bounding box of the
* current frame.
*
* @param id an optional reference to a web element
* @param highlights an optional list of web elements to draw a red
* box around in the returned capture
* @return PNG image encoded as base 64 string
*/
takeScreenshot: function MDA_takeScreenshot(aRequest) {
this.command_id = this.getCommandId();
this.sendAsync("screenShot",
{
id: aRequest.parameters.id,
highlights: aRequest.parameters.highlights
},
this.sendAsync("takeScreenshot",
{id: aRequest.parameters.id,
highlights: aRequest.parameters.highlights},
this.command_id);
},
@ -2256,13 +2265,15 @@ MarionetteServerConnection.prototype = {
let mozOr = or.toLowerCase();
if (ors.indexOf(mozOr) < 0) {
this.sendError("Unknown screen orientation: " + or, 500, null, this.command_id);
this.sendError("Unknown screen orientation: " + or, 500, null,
this.command_id);
return;
}
let curWindow = this.getCurrentWindow();
if (!curWindow.screen.mozLockOrientation(mozOr)) {
this.sendError("Unable to set screen orientation: " + or, 500, null, this.command_id);
this.sendError("Unable to set screen orientation: " + or, 500,
null, this.command_id);
}
this.sendOk(this.command_id);
},
@ -2445,7 +2456,8 @@ MarionetteServerConnection.prototype.requestTypes = {
"close": MarionetteServerConnection.prototype.close,
"closeWindow": MarionetteServerConnection.prototype.close, // deprecated
"setTestName": MarionetteServerConnection.prototype.setTestName,
"screenShot": MarionetteServerConnection.prototype.screenShot,
"takeScreenshot": MarionetteServerConnection.prototype.takeScreenshot,
"screenShot": MarionetteServerConnection.prototype.takeScreenshot, // deprecated
"addCookie": MarionetteServerConnection.prototype.addCookie,
"getCookies": MarionetteServerConnection.prototype.getCookies,
"getAllCookies": MarionetteServerConnection.prototype.getCookies, // deprecated