Skip to content

Instantly share code, notes, and snippets.

@RHeijnen
Created January 21, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RHeijnen/73c3a6c8c366cd235e6bd7b02fd98e47 to your computer and use it in GitHub Desktop.
Save RHeijnen/73c3a6c8c366cd235e6bd7b02fd98e47 to your computer and use it in GitHub Desktop.
/*
* RobotJS supporter functions for end2end testing
* see specs/_tests/robotModule/robotModuleTest.js for unit tests
* ************* REQUIRES WINDOW FOCUS *************
* typeString - Function for simulating a set of/or single keyboard strokes
* - Example: typeString("Hello World"); //\\ typeString("A"); //\\ typeString("123");
* pressTab - Simulates an Tab Arrow button tap
* - Example: pressTab();
* pressEnter - Simulates an Enter button tap
* - Example: pressEnter();
* pressDown - Simulates an Down Arrow button tap
* - Example: pressDown();
* pressUp - Simulates an Up Arrow button tap
* - Example: pressUp();
* pressGenericButton - Function for undescribed keys. find more on: https://github.com/octalmage/robotjs/wiki/Syntax
* - Example: pressGenericButton("home");
* mousePosition - sets the mouse pointer to the X Y location instantly
* - Example: mousePosition(250,250);
* mouseMove - moves the mouse humanlike to the X Y location
* - Example: mouseMove(250,250);
* getPixelColor - returns pixel color of mouse location
* - Example: getPixelColor(250,250);
* downloadChromeFile - Simulates a user filling out a chrome download prompt, use in promise
* - Example : pressDownloadButton().then... downloadChromeFile(C:\\....) [Assumes file overwrite]
* removeFile - Generic file remover function
* - Example removeFile(C:\\...)
*
*/
module.exports.robotModule = function () {
// import robotJS node module
var robot = require("robotjs");
/*
* typeString
* typeString("HelloWorld")
*/
this.typeString = function(content){
robot.typeStringDelayed(content,350);
}
/*
* Simulates Tab press
*
*/
this.pressTab = function(){
robot.keyTap("tab");
}
this.pressEnter = function(){
robot.keyTap("enter");
}
/*
* pressGenericButton
* pressGenericButton("home")
*/
this.pressGenericButton = function(button){
robot.keyTap(button);
}
/*
* Simulates Arrow Down Button
*
*/
this.pressDown = function(){
robot.keyTap("down");
}
/*
* Simulates Arrow Up Button
*
*/
this.pressUp = function(){
robot.keyTap("up");
}
/*
* Sets the mouse position instantly on X Y pixels
*
*/
this.mousePosition = function(x,y){
robot.moveMouse(x, y);
}
/*
*
* Drags the mouse FROM StartX Start Y Towards End X end Y
*/
this.mouseDrag = function(startX,startY,endX,endY){
robot.moveMouse(startX, startY);
robot.mouseToggle("down");
robot.dragMouse(endX, endY);
robot.mouseToggle("up");
}
/*
* Moves the mouse in a fluid motion towards X Y pixels
*
*/
this.mouseMove = function(x,y){
robot.moveMouseSmooth(x, y);
}
/*
* Simulates left mouse button click
*
*/
this.mouseClickLeft = function(){
robot.mouseClick("left");
}
/*
* Simulates right mouse button click
*
*/
this.mouseClickRight = function(){
robot.mouseClick("right");
}
/*
* Returns the pixel color of location X Y
*
*/
this.getPixelColor = function(x,y){
return robot.getPixelColor(x,y)
}
/*
* Simulates the flow of downloading a file in a chrome download prompt.
*
* Tested in this scenario
[...].then(function(){ // promise
robotModule.downloadChromeFile("C:\\_dev\\_final\\downloads\\test.pdf"); // Function call
global.wait(1500); // Give RobotJs enough time
})
*/
this.downloadChromeFile = function(path){
robot.typeStringDelayed(path,600);
robot.keyTap("enter"); // saves file
// pop up opens with file already exists...
// Safe if not [Only 'beeps' if file does not already exists]
robot.keyTap("tab"); // tab to overwite
robot.keyTap("enter"); // enter to confirm
}
/*
* Non robotJS Function for deleting generic files that have been downloaded.
*
*/
this.removeFile = function(path){
var fs = require('fs');
fs.unlinkSync(path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment