vbox-robot
Advanced tools
Comparing version 0.0.6 to 0.1.0
{ | ||
"name": "vbox-robot", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"description": "Tool allowing JavaScripts scripts running in a browser to send keyboard and mouse events to the Virtual Box machine they run in, especially for automated tests.", | ||
@@ -13,3 +13,3 @@ "repository": { | ||
}, | ||
"author": "ariatemplates <contact@ariatemplates.com> (http://github.com/ariatemplates)", | ||
"author": "ariatemplates <ariatemplates@amadeus.com> (http://github.com/ariatemplates)", | ||
"license": "Apache-2.0", | ||
@@ -21,2 +21,3 @@ "bugs": { | ||
"dependencies": { | ||
"@zxing/library": "^0.18.6", | ||
"basic-auth": "^1.0.3", | ||
@@ -42,4 +43,5 @@ "co": "^4.6.0", | ||
"eslint": "^1.10.3", | ||
"qrcode": "^1.4.4", | ||
"webpack": "^1.12.9" | ||
} | ||
} |
@@ -18,13 +18,45 @@ /* | ||
var QRCode = require("qrcode/lib/core/qrcode"); | ||
var Utils = require("qrcode/lib/renderer/utils"); | ||
var request = require("./request"); | ||
var qrCodePrefix = "vbox-robot://"; | ||
var outerMargin = 10; | ||
var innerMargin = 50; | ||
module.exports = function (callback) { | ||
var div = document.createElement("div"); | ||
var border = 30; | ||
div.style.cssText = "display:block;position:absolute;background-color:rgb(255, 0, 0);border:" + border | ||
+ "px solid rgb(100, 100, 100);left:0px;top:0px;right:0px;bottom:0px;cursor:none;z-index:999999;"; | ||
div.style.cssText = "display:block;position:absolute;background-color:white;left:0px;top:0px;right:0px;bottom:0px;cursor:none;z-index:999999;"; | ||
document.body.appendChild(div); | ||
var width = div.offsetWidth; | ||
var height = div.offsetHeight; | ||
var canvas = document.createElement("canvas"); | ||
canvas.width = width; | ||
canvas.height = height; | ||
canvas.style.cssText = "display:block;position:absolute;left:0px;top:0px;"; | ||
div.appendChild(canvas); | ||
var ctx = canvas.getContext("2d"); | ||
var y = outerMargin; | ||
var qrSize = 0; | ||
var opts = { | ||
errorCorrectionLevel: "H", | ||
margin: 0, | ||
scale: 1 | ||
}; | ||
while (y + qrSize + outerMargin < height) { | ||
var x = outerMargin; | ||
while (x + qrSize + outerMargin < width) { | ||
var qrData = QRCode.create(qrCodePrefix + x + "/" + y, opts); | ||
qrSize = Utils.getImageWidth(qrData.modules.size, opts); | ||
var image = ctx.createImageData(qrSize, qrSize); | ||
Utils.qrToImageData(image.data, qrData, Utils.getOptions(opts)); | ||
ctx.putImageData(image, x, y); | ||
x += qrSize + innerMargin; | ||
} | ||
y += qrSize + innerMargin; | ||
} | ||
// wait some time for the browser to display the element | ||
setTimeout(function () { | ||
request("calibrate", [div.offsetWidth - 2 * border, div.offsetHeight - 2 * border], function (response) { | ||
request("calibrate", [], function (response) { | ||
div.parentNode.removeChild(div); | ||
@@ -34,4 +66,4 @@ if (response.success) { | ||
response.result = { | ||
x : result.x - border, | ||
y : result.y - border | ||
x : result.x, | ||
y : result.y | ||
}; | ||
@@ -38,0 +70,0 @@ } |
@@ -36,3 +36,2 @@ /* | ||
createFunction("mouseWheel", 1); | ||
createFunction("calibrate", 2); | ||
createFunction("keyboardSendScancodes", 1); | ||
@@ -39,0 +38,0 @@ |
@@ -21,3 +21,3 @@ /* | ||
const PNG = require("pngjs").PNG; | ||
const findRectangle = require("./findRectangle"); | ||
const findViewport = require("./findViewport"); | ||
const getVboxDisplay = require("./getVBoxDisplay"); | ||
@@ -31,18 +31,13 @@ | ||
const parseImage = promisify(image.parse.bind(image)); | ||
const imageBuffer = new Buffer(screenShot, "base64"); | ||
const imageBuffer = Buffer.from(screenShot, "base64"); | ||
yield parseImage(imageBuffer); | ||
const rectangle = findRectangle(image, [255,0,0,255], task.expectedWidth, task.expectedHeight, 50); | ||
if (!rectangle) { | ||
try { | ||
return findViewport(image); | ||
} catch (error) { | ||
const fileName = task.failedCalibrationFileName; | ||
if (fileName) { | ||
writeFile(fileName, imageBuffer); // no yield to speed up the response | ||
throw new Error(`Calibration failed, screenshot recorded as ${fileName}`); | ||
} else { | ||
throw new Error("Calibration failed, screenshot was not saved."); | ||
} | ||
throw new Error(`Calibration failed, ${fileName ? `screenshot recorded as ${fileName}` : `screenshot was not saved` }.\n${error}`); | ||
} | ||
return { | ||
x: rectangle.x, | ||
y: rectangle.y | ||
}; | ||
}); |
@@ -21,3 +21,2 @@ /* | ||
const co = require("co"); | ||
const checkInt = require("../checkInt"); | ||
const createId = require("../createId"); | ||
@@ -101,4 +100,2 @@ const closeVM = require("../vmManageRoutes").closeVM; | ||
const task = { | ||
expectedWidth: checkInt(ctx.data[0]), | ||
expectedHeight: checkInt(ctx.data[1]), | ||
failedCalibrationFileName: screenshotsFolder ? path.join(screenshotsFolder, `${createId()}.png`) : null, | ||
@@ -117,3 +114,14 @@ vboxServer: config.vboxwebsrv, | ||
try { | ||
ctx.body = yield executeTask(task); | ||
const result = yield executeTask(task); | ||
// click on the QR code to make sure the window is focused: | ||
yield vm.mouseMove(result.qrCodeX, result.qrCodeY); | ||
yield wait(100); | ||
yield vm.vboxMouse.putMouseEvent(0, 0, 0, 0, 1); | ||
yield wait(50); | ||
yield vm.vboxMouse.putMouseEvent(0, 0, 0, 0, 0); | ||
yield wait(100); | ||
// after clicking, move the mouse back to 0, 0 | ||
yield vm.mouseMove(0, 0); | ||
yield wait(100); | ||
ctx.body = result; | ||
} catch (e) { | ||
@@ -120,0 +128,0 @@ if (vm.closeOnFailedCalibration) { |
Sorry, the diff of this file is too big to display
171384
2027
10
3
+ Added@zxing/library@^0.18.6
+ Added@zxing/library@0.18.6(transitive)
+ Added@zxing/text-encoding@0.9.0(transitive)
+ Addedts-custom-error@3.3.1(transitive)