Robonut 服務
描述
wdio-robonut-service 是一個跨平台(windows、darwin、linux)服務,可以使用 nutjs 存取系統滑鼠、鍵盤、剪貼簿、螢幕,以及在可見螢幕上搜尋影像範本。透過此服務,可以執行系統移動、點擊、複製、貼上、輸入等等。可以檢查影像範本是否存在,並取得其座標,拖曳和放置等等。
安裝
npm install wdio-robonut-service
設定
interface RobotConfig {
mouseConfig?: { autoDelayMs: number; mouseSpeed: number };
screenConfig?: { confidence: number; autoHighlight: boolean; highlightDurationMs: number; highlightOpacity: number; resourceDirectory: string };
keyboardConfig?: { autoDelayMs: number };
imageFinder?: {
confidence?: number;
providerData?: {
methodType?: MethodNameType;
scaleSteps?: Array<number>;
searchMultipleScales?: boolean;
isRotation: boolean,
rotationOption?: { range?: number; overLap?: number; minDstLength?: number };
debug?: boolean;
roi?: Region;
};
};
}
像 wdio.conf.ts 中的 wdio 服務一樣
import RobotService from 'wdio-robonut-service';
const robotConfig: RobotConfig = {};
export const config: Options.Testrunner = {
//-
services: [[RobotService, robotConfig]],
//-
}
適用於 wdio 獨立/遠端
在瀏覽器初始化後執行一次
import RobotCommands from 'wdio-robonut-service';
const robotConfig: RobotConfig = {};
new RobotCommands(browser, robotConfig).addCommands()
獨立
const robotConfig: RobotConfig = {};
new RobotDirect(robotConfig).instance()
使用方式
主要存取點
browser.robot
範例
async function dragAndDropImage(imageDrag: ImageElement,imageDrop: ImageElement, timeout: number = 10000) {
await browser.robot.image.dragAndDrop(
{ pathToImage: imageDrag.pathToImage},
{ pathToImage: imageDrop.pathToImage},
{ highLight: timeout/10 , waitTimeout:timeout });
}
async function dragAndDropImageWithNestedImage(imageDrag: ImageElement,imageDrop: ImageElement, timeout: number = 10000 ) {
await browser.robot.image.dragAndDrop(
{ pathToImage: imageDrag.pathToImage, pathToNestedImage: imageDrag.pathToNestedImage },
{ pathToImage: imageDrop.pathToImage, pathToNestedImage: imageDrop.pathToNestedImage },
{ highLight: timeout/10 , waitTimeout:timeout },
);
}
async function clickImage(image: ImageElement,
options: WaitUntilOptions = { interval: 2500, timeout: 10000 }) {
await browser.robot.image.waitForImageDisplayed(image, options);
const location = await browser.robot.image.finder.findMatch({ needle: image.pathToImage });
const point = await browser.robot.rect.centerOf(location.location);
await browser.robot.mouse.move(await browser.robot.rect.straightTo(point));
await browser.robot.mouse.click(Button.LEFT);
}
async function isWaitForImageDisplayed(image: ImageElement,
options: WaitUntilOptions = { interval: 2500, timeout: 10000 }) {
try {
return (await browser.waitUntil(
async () => {
return !!(await browser.robot.image.finder.findMatch({ needle: image.pathToImage })).location;
}, options
)) as true;
} catch {
return false;
}
}
API
interface Browser {
robot: {
rect: {
straightTo: (target: Point | Promise<Point>) => Promise<Point[]>;
centerOf: (target: Region | Promise<Region>) => Promise<Point>;
randomPointIn: (target: Region | Promise<Region>) => Promise<Point>;
};
image: {
finder: TemplateMatchingFinder;
reader: { imageResource: (fileName: string) => Promise<Image>; loadImage: (parameters: string) => Promise<Image>; saveImage: (parameters: ImageWriterParameters) => Promise<void> };
clickImage: (image: ImageElement, options: WaitUntilOptions) => Promise<void>;
isWaitForImageDisplayed: (image: ImageElement, options?: WaitUntilOptions) => Promise<boolean>;
waitForImageDisplayed: (image: ImageElement, options?: WaitUntilOptions) => Promise<true | void>;
highlightDisplayedImage: (image: ImageElement, options?: WaitUntilOptions & { highLight?: number }) => Promise<void>;
dragAndDrop: (dragImage: ImageElement, dropImage: ImageElement, options?: RobotDragAndDropType) => Promise<void>;
};
mouse: MouseClass;
screen: ScreenClass;
keyboard: KeyboardClass;
windowApiProvider: WindowProviderInterface;
clipboard: { sys: SysClipboard; virt: ClipboardClass };
};
}
功能
- 機器人介面
- 影像機器人
- Dom 元素機器人(依據定位器)
限制
- 使用可見顯示(非無頭模式)
- 在單一執行緒/實例中運作;