Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("should call `screen.capture` with timestamp and custom path", async () => {
// GIVEN
const screenShotPath = join("test", "path", "to");
const screenShotFileName = "screenshot";
const screenShotFileExt = ".png";
screen.capture = jest.fn(() => Promise.resolve("filename"));
// WHEN
await ScreenApi.takeScreenshotWithTimestamp(`${join(screenShotPath, screenShotFileName)}${screenShotFileExt}`);
// THEN
expect(screen.capture).toBeCalledTimes(1);
expect(screen.capture).toBeCalledWith(screenShotFileName, FileType.PNG, screenShotPath, expect.stringMatching(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}_/), "");
});
});
it("should call `screen.capture` with fallback `cwd()`", async () => {
// GIVEN
const screenShotFileName = "screenshot";
const screenShotFileExt = ".png";
screen.capture = jest.fn(() => Promise.resolve("filename"));
// WHEN
await ScreenApi.takeScreenshot(`${screenShotFileName}${screenShotFileExt}`);
// THEN
expect(screen.capture).toBeCalledTimes(1);
expect(screen.capture).toBeCalledWith(screenShotFileName, FileType.PNG, cwd());
});
import {getTimestamp, ScreenApi} from "./screen.function";
import {createRegionClass} from "../region";
import {Project, TestExecutionContext} from "@sakuli/core";
import {SimpleLogger} from "@sakuli/commons";
import {FileType} from "@nut-tree/nut-js/dist/lib/file-type.enum";
import {cwd} from "process";
import {join} from "path";
let ctx: TestExecutionContext;
let project: Project;
const testFilename = "testfile.png";
const testPath = "/test/path";
const testConfidence = 0.99;
const testTimeout = 500;
const expectedRegion = new Region(0, 0, 100, 100);
beforeEach(() => {
ctx = new TestExecutionContext(new SimpleLogger());
project = new Project("");
});
describe("ScreenApi", () => {
it("should call `screen.find` with suitable Region", async () => {
// GIVEN
const TestRegion = createRegionClass(ctx, project);
const testRegion = new TestRegion(0, 0, 100, 100);
screen.find = jest.fn(() => Promise.resolve(expectedRegion));
// WHEN
await ScreenApi.find(testFilename, testPath, testConfidence, testRegion);
async takeScreenshot(filename: string): Promise {
const pathParts = parse(filename);
const outputDir = (pathParts.dir && pathParts.dir.length > 0) ? pathParts.dir : cwd();
return screen.capture(pathParts.name, FileType.PNG, outputDir);
},
async takeScreenshotWithTimestamp(filename: string): Promise {
async takeScreenshotWithTimestamp(filename: string): Promise {
const pathParts = parse(filename);
const outputDir = (pathParts.dir && pathParts.dir.length > 0) ? pathParts.dir : cwd();
return screen.capture(pathParts.name, FileType.PNG, outputDir, `${new Date().toISOString()}_`, "");
}
};
return new Promise(async (resolve, reject) => {
try {
screen.config.resourceDirectory = path;
const result = await screen.find(filename, {
confidence,
searchRegion: new NutRegion(
left,
top,
width,
height
)
});
resolve({
left: result.left,
top: result.top,
width: result.width,
height: result.height
});
} catch (e) {
reject(e);
}
});
const toNutRegion = async (region: Region): Promise => {
return new NutRegion(
await region.getX() || 0,
await region.getY() || 0,
await region.getW() || await ScreenApi.width(),
await region.getH() || await ScreenApi.height()
);
};
export const copyShortcut = () => {
return [getSuperKey(), Key.C];
};
static readonly SCROLL_LOCK = NutKey.ScrollLock;
static readonly PAUSE = NutKey.Pause;
static readonly CAPS_LOCK = NutKey.CapsLock;
static readonly NUM0 = NutKey.Num0;
static readonly NUM1 = NutKey.Num1;
static readonly NUM2 = NutKey.Num2;
static readonly NUM3 = NutKey.Num3;
static readonly NUM4 = NutKey.Num4;
static readonly NUM5 = NutKey.Num5;
static readonly NUM6 = NutKey.Num6;
static readonly NUM7 = NutKey.Num7;
static readonly NUM8 = NutKey.Num8;
static readonly NUM9 = NutKey.Num9;
static readonly A = NutKey.A;
static readonly B = NutKey.B;
static readonly C = NutKey.C;
static readonly D = NutKey.D;
static readonly E = NutKey.E;
static readonly F = NutKey.F;
static readonly G = NutKey.G;
static readonly H = NutKey.H;
static readonly I = NutKey.I;
static readonly J = NutKey.J;
static readonly K = NutKey.K;
static readonly L = NutKey.L;
static readonly M = NutKey.M;
static readonly N = NutKey.N;
static readonly O = NutKey.O;
static readonly P = NutKey.P;
static readonly Q = NutKey.Q;
static readonly R = NutKey.R;
static readonly S = NutKey.S;
static readonly F2 = NutKey.F2;
static readonly F3 = NutKey.F3;
static readonly F4 = NutKey.F4;
static readonly F5 = NutKey.F5;
static readonly F6 = NutKey.F6;
static readonly F7 = NutKey.F7;
static readonly F8 = NutKey.F8;
static readonly F9 = NutKey.F9;
static readonly F10 = NutKey.F10;
static readonly F11 = NutKey.F11;
static readonly F12 = NutKey.F12;
static readonly F13 = null;
static readonly F14 = null;
static readonly F15 = null;
static readonly SHIFT = NutKey.LeftShift;
static readonly CTRL = NutKey.LeftControl;
static readonly ALT = NutKey.LeftAlt;
static readonly ALTGR = NutKey.RightAlt;
static readonly META = NutKey.LeftSuper;
static readonly CMD = NutKey.LeftSuper;
static readonly WIN = NutKey.LeftSuper;
static readonly PRINTSCREEN = NutKey.Print;
static readonly SCROLL_LOCK = NutKey.ScrollLock;
static readonly PAUSE = NutKey.Pause;
static readonly CAPS_LOCK = NutKey.CapsLock;
static readonly NUM0 = NutKey.Num0;
static readonly NUM1 = NutKey.Num1;
static readonly NUM2 = NutKey.Num2;
static readonly NUM3 = NutKey.Num3;
static readonly NUM4 = NutKey.Num4;
static readonly NUM5 = NutKey.Num5;
static readonly NUM6 = NutKey.Num6;