1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164export interface IAutomationDriver {
createScreenshot(rect?: Rect): Promise<Image>;
getUINode(): Promise<UINode>;
clickElement(node: UINode): Promise<void>;
revealElement(node: UINode): Promise<void>;
sendKey(key: string): Promise<void>;
sendText(text: string): Promise<void>;
createProcess(executablePath: string, args?: string[]): Promise<IProcess>;
findRootProcesses(options: { executablePath?: string; executableName?: string }): Promise<ProcessTree[]>;
}
export class ProcessTree {
constructor(
readonly process: IProcess,
readonly children: readonly ProcessTree[],
readonly windows: readonly UINode[],
) { }
public getAllWindows(): { window: UINode, process: IProcess }[] {
const result: { window: UINode, process: IProcess }[] = [];
function addFromProcessTree(tree: ProcessTree) {
for (const window of tree.windows) {
result.push({ window, process: tree.process });
}
for (const child of tree.children) {
addFromProcessTree(child);
}
}
addFromProcessTree(this);
return result;
}
}
export interface IProcess {
id: number;
kill(): Promise<void>;
waitForExit(): Promise<number>;
getUINode(): Promise<UINode>;
waitForUINode(predicate: (node: UINode) => boolean, options?: IWaitOptions): Promise<UINode>;
}
export interface IWaitOptions {
timeoutMs?: number;
}
export class Image {
constructor(
public readonly base64Png: string
) { }
}
export class UINode {
constructor(
public readonly parent: UINode | undefined,
public readonly id: string,
public readonly type: string,
public readonly text: string | undefined,
public readonly rect: Rect | undefined,
public readonly props: Record<string, unknown>,
public readonly children: UINode[],
) { }
public previousSibling(): UINode | undefined {
if (!this.parent) {
return undefined;
}
const index = this.parent.children.indexOf(this);
if (index > 0) {
return this.parent.children[index - 1];
}
}
public nextSibling(): UINode | undefined {
if (!this.parent) {
return undefined;
}
const index = this.parent.children.indexOf(this);
if (index < this.parent.children.length - 1) {
return this.parent.children[index + 1];
}
}
toString(recursive: true): string {
if (!recursive) {
return `UINode(${this.id}, ${this.type}, ${this.text}, ${this.rect?.toString()})`;
}
function formatNode(node: UINode, indent: string): string {
const childrenStr = node.children.map(child => formatNode(child, indent + ' ')).join('\n');
return `${indent}UINode(${node.id}, ${node.type}, ${node.text}, ${node.rect?.toString()})\n${childrenStr}`;
}
return formatNode(this, '');
}
find(predicate: (node: UINode) => boolean): UINode | undefined {
if (predicate(this)) {
return this;
}
for (const child of this.children) {
const result = child.find(predicate);
if (result) {
return result;
}
}
}
findLast(predicate: (node: UINode) => boolean): UINode | undefined {
if (predicate(this)) {
return this;
}
for (let i = this.children.length - 1; i >= 0; i--) {
const child = this.children[i];
const result = child.findLast(predicate);
if (result) {
return result;
}
}
}
toJson(): unknown {
return {
id: this.id,
type: this.type,
text: this.text,
rect: this.rect,
props: this.props,
children: this.children.map(child => child.toJson()),
};
}
}
export class Rect {
constructor(
public readonly topLeft: Point,
public readonly bottomRight: Point
) { }
toString(): string {
return `Rect(${this.topLeft.toString()}, ${this.bottomRight.toString()})`;
}
}
export class Point {
constructor(
public readonly x: number,
public readonly y: number,
) { }
toString(): string {
return `(${this.x}, ${this.y})`;
}
}