@mediar-ai/terminator 0.20.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +23 -0
- package/README.md +216 -0
- package/build.rs +4 -0
- package/index.d.ts +738 -0
- package/index.js +321 -0
- package/node_example.js +64 -0
- package/package.json +64 -0
- package/src/desktop.rs +520 -0
- package/src/element.rs +581 -0
- package/src/exceptions.rs +58 -0
- package/src/lib.rs +20 -0
- package/src/locator.rs +172 -0
- package/src/selector.rs +139 -0
- package/src/types.rs +308 -0
- package/sync-version.js +60 -0
- package/test-tree.js +83 -0
- package/tests/comprehensive-ui-elements.test.js +524 -0
- package/tests/element-chaining.test.js +158 -0
- package/tests/element-range.test.js +207 -0
- package/tests/element-scroll-into-view.test.js +256 -0
- package/tests/element-value.test.js +264 -0
- package/tests/locator-validate.test.js +260 -0
- package/tests/locator-waitfor.test.js +286 -0
- package/wrapper.d.ts +42 -0
- package/wrapper.js +170 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
const { Desktop } = require("../index.js");
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Test for Locator.waitFor() method with 'exists' condition
|
|
5
|
+
*/
|
|
6
|
+
async function testWaitForExists() {
|
|
7
|
+
console.log("🕐 Testing Locator.waitFor('exists')...");
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const desktop = new Desktop();
|
|
11
|
+
|
|
12
|
+
// Get any available application for testing
|
|
13
|
+
const apps = desktop.applications();
|
|
14
|
+
if (apps.length === 0) {
|
|
15
|
+
throw new Error("No applications found for testing");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const testApp = apps[0];
|
|
19
|
+
console.log(`📱 Testing with app: ${testApp.name()}`);
|
|
20
|
+
|
|
21
|
+
// Test: Wait for a window to exist (should succeed immediately)
|
|
22
|
+
console.log("Test: Wait for window to exist");
|
|
23
|
+
const element = await desktop.locator("role:window").waitFor("exists", 5000);
|
|
24
|
+
|
|
25
|
+
if (!element) {
|
|
26
|
+
throw new Error("Expected element to be returned");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
console.log(`✅ Found window: ${element.name()}`);
|
|
30
|
+
return true;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error("❌ WaitFor exists test failed:", error.message);
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Test waitFor() with 'visible' condition
|
|
39
|
+
*/
|
|
40
|
+
async function testWaitForVisible() {
|
|
41
|
+
console.log("🕐 Testing Locator.waitFor('visible')...");
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const desktop = new Desktop();
|
|
45
|
+
|
|
46
|
+
// Test: Wait for a visible window
|
|
47
|
+
console.log("Test: Wait for window to be visible");
|
|
48
|
+
const element = await desktop.locator("role:window").waitFor("visible", 5000);
|
|
49
|
+
|
|
50
|
+
// Check that the element is actually visible
|
|
51
|
+
if (!element.isVisible()) {
|
|
52
|
+
throw new Error("Element should be visible");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log(`✅ Found visible window: ${element.name()}`);
|
|
56
|
+
return true;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error("❌ WaitFor visible test failed:", error.message);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Test waitFor() timeout behavior
|
|
65
|
+
*/
|
|
66
|
+
async function testWaitForTimeout() {
|
|
67
|
+
console.log("🕐 Testing Locator.waitFor() timeout...");
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const desktop = new Desktop();
|
|
71
|
+
|
|
72
|
+
// Test: Wait for a non-existent element (should timeout)
|
|
73
|
+
console.log("Test: Wait for non-existent element (expecting timeout)");
|
|
74
|
+
let timedOut = false;
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await desktop
|
|
78
|
+
.locator("role:button|ThisButtonDoesNotExist12345XYZ")
|
|
79
|
+
.waitFor("exists", 1000);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (err.message.includes("Timed out") || err.message.includes("timeout")) {
|
|
82
|
+
timedOut = true;
|
|
83
|
+
console.log("✅ Correctly timed out");
|
|
84
|
+
} else {
|
|
85
|
+
throw new Error(`Unexpected error: ${err.message}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!timedOut) {
|
|
90
|
+
throw new Error("Expected timeout error");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return true;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error("❌ WaitFor timeout test failed:", error.message);
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Test waitFor() with different conditions
|
|
102
|
+
*/
|
|
103
|
+
async function testWaitForConditions() {
|
|
104
|
+
console.log("🕐 Testing Locator.waitFor() with different conditions...");
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
const desktop = new Desktop();
|
|
108
|
+
|
|
109
|
+
// Test each condition on a window (which should be visible and enabled)
|
|
110
|
+
const conditions = ["exists", "visible", "enabled"];
|
|
111
|
+
|
|
112
|
+
for (const condition of conditions) {
|
|
113
|
+
console.log(`Test: waitFor('${condition}')`);
|
|
114
|
+
const element = await desktop
|
|
115
|
+
.locator("role:window")
|
|
116
|
+
.waitFor(condition, 5000);
|
|
117
|
+
|
|
118
|
+
if (!element) {
|
|
119
|
+
throw new Error(`No element returned for condition '${condition}'`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log(`✅ Condition '${condition}' met`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return true;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.error("❌ WaitFor conditions test failed:", error.message);
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Test waitFor() with invalid condition
|
|
134
|
+
*/
|
|
135
|
+
async function testWaitForInvalidCondition() {
|
|
136
|
+
console.log("🕐 Testing Locator.waitFor() with invalid condition...");
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const desktop = new Desktop();
|
|
140
|
+
|
|
141
|
+
// Test: Invalid condition should throw error
|
|
142
|
+
console.log("Test: Wait with invalid condition");
|
|
143
|
+
let errorThrown = false;
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
await desktop.locator("role:window").waitFor("invalid_condition", 1000);
|
|
147
|
+
} catch (err) {
|
|
148
|
+
if (err.message.includes("Invalid condition")) {
|
|
149
|
+
errorThrown = true;
|
|
150
|
+
console.log("✅ Correctly rejected invalid condition");
|
|
151
|
+
} else {
|
|
152
|
+
throw new Error(`Unexpected error: ${err.message}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!errorThrown) {
|
|
157
|
+
throw new Error("Expected error for invalid condition");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return true;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.error("❌ WaitFor invalid condition test failed:", error.message);
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Test waitFor() with chaining
|
|
169
|
+
*/
|
|
170
|
+
async function testWaitForChaining() {
|
|
171
|
+
console.log("🕐 Testing Locator.waitFor() with chaining...");
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
const desktop = new Desktop();
|
|
175
|
+
const apps = desktop.applications();
|
|
176
|
+
|
|
177
|
+
if (apps.length === 0) {
|
|
178
|
+
throw new Error("No applications found for testing");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const testApp = apps[0];
|
|
182
|
+
|
|
183
|
+
// Test: Wait with chained locator
|
|
184
|
+
console.log("Test: waitFor with chained locator");
|
|
185
|
+
const element = await testApp
|
|
186
|
+
.locator("role:window")
|
|
187
|
+
.waitFor("visible", 3000);
|
|
188
|
+
|
|
189
|
+
console.log(`✅ Found element via chain: ${element.name()}`);
|
|
190
|
+
return true;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
// This might fail if the app has no window, which is acceptable
|
|
193
|
+
if (error.message.includes("Timed out")) {
|
|
194
|
+
console.log("ℹ️ No window found in chain (acceptable)");
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
console.error("❌ WaitFor chaining test failed:", error.message);
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Main test runner
|
|
204
|
+
*/
|
|
205
|
+
async function runWaitForTests() {
|
|
206
|
+
console.log("🚀 Starting Locator.waitFor() tests...\n");
|
|
207
|
+
|
|
208
|
+
let passed = 0;
|
|
209
|
+
let total = 0;
|
|
210
|
+
|
|
211
|
+
// Test 1: Wait for exists
|
|
212
|
+
total++;
|
|
213
|
+
if (await testWaitForExists()) {
|
|
214
|
+
passed++;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
console.log(); // Empty line
|
|
218
|
+
|
|
219
|
+
// Test 2: Wait for visible
|
|
220
|
+
total++;
|
|
221
|
+
if (await testWaitForVisible()) {
|
|
222
|
+
passed++;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
console.log(); // Empty line
|
|
226
|
+
|
|
227
|
+
// Test 3: Timeout behavior
|
|
228
|
+
total++;
|
|
229
|
+
if (await testWaitForTimeout()) {
|
|
230
|
+
passed++;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
console.log(); // Empty line
|
|
234
|
+
|
|
235
|
+
// Test 4: Different conditions
|
|
236
|
+
total++;
|
|
237
|
+
if (await testWaitForConditions()) {
|
|
238
|
+
passed++;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
console.log(); // Empty line
|
|
242
|
+
|
|
243
|
+
// Test 5: Invalid condition
|
|
244
|
+
total++;
|
|
245
|
+
if (await testWaitForInvalidCondition()) {
|
|
246
|
+
passed++;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
console.log(); // Empty line
|
|
250
|
+
|
|
251
|
+
// Test 6: Chaining
|
|
252
|
+
total++;
|
|
253
|
+
if (await testWaitForChaining()) {
|
|
254
|
+
passed++;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
console.log(); // Empty line
|
|
258
|
+
|
|
259
|
+
// Results
|
|
260
|
+
if (passed === total) {
|
|
261
|
+
console.log(`🎉 All waitFor tests passed! (${passed}/${total})`);
|
|
262
|
+
process.exit(0);
|
|
263
|
+
} else {
|
|
264
|
+
console.log(`❌ Some tests failed: ${passed}/${total} passed`);
|
|
265
|
+
process.exit(1);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Export for use in other test files
|
|
270
|
+
module.exports = {
|
|
271
|
+
testWaitForExists,
|
|
272
|
+
testWaitForVisible,
|
|
273
|
+
testWaitForTimeout,
|
|
274
|
+
testWaitForConditions,
|
|
275
|
+
testWaitForInvalidCondition,
|
|
276
|
+
testWaitForChaining,
|
|
277
|
+
runWaitForTests,
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// Run tests if this file is executed directly
|
|
281
|
+
if (require.main === module) {
|
|
282
|
+
runWaitForTests().catch((error) => {
|
|
283
|
+
console.error("💥 Test runner crashed:", error);
|
|
284
|
+
process.exit(1);
|
|
285
|
+
});
|
|
286
|
+
}
|
package/wrapper.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Re-export all types and interfaces from the original declaration file
|
|
2
|
+
export * from './index.d';
|
|
3
|
+
|
|
4
|
+
/** Thrown when an element is not found. */
|
|
5
|
+
export class ElementNotFoundError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Thrown when an operation times out. */
|
|
10
|
+
export class TimeoutError extends Error {
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Thrown when permission is denied. */
|
|
15
|
+
export class PermissionDeniedError extends Error {
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Thrown for platform-specific errors. */
|
|
20
|
+
export class PlatformError extends Error {
|
|
21
|
+
constructor(message: string);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Thrown for unsupported operations. */
|
|
25
|
+
export class UnsupportedOperationError extends Error {
|
|
26
|
+
constructor(message: string);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Thrown for unsupported platforms. */
|
|
30
|
+
export class UnsupportedPlatformError extends Error {
|
|
31
|
+
constructor(message: string);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Thrown for invalid arguments. */
|
|
35
|
+
export class InvalidArgumentError extends Error {
|
|
36
|
+
constructor(message: string);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Thrown for internal errors. */
|
|
40
|
+
export class InternalError extends Error {
|
|
41
|
+
constructor(message: string);
|
|
42
|
+
}
|
package/wrapper.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const native = require('./index.js');
|
|
2
|
+
const util = require('util');
|
|
3
|
+
|
|
4
|
+
function patchInspector(Klass, methodName = 'toString', forcePlainObject = false) {
|
|
5
|
+
if (!Klass || typeof Klass !== 'function') {
|
|
6
|
+
console.log('inspect not a function')
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const proto = Klass.prototype;
|
|
10
|
+
const original = proto[util.inspect.custom];
|
|
11
|
+
proto[util.inspect.custom] = function(...args) {
|
|
12
|
+
if (typeof this[methodName] === 'function') {
|
|
13
|
+
const result = this[methodName](...args);
|
|
14
|
+
if (forcePlainObject && result && typeof result === 'object') {
|
|
15
|
+
return { ...result };
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
if (typeof original === 'function') {
|
|
20
|
+
return original.apply(this, args);
|
|
21
|
+
}
|
|
22
|
+
return { ...this };
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function wrapNativeFunction(fn) {
|
|
27
|
+
if (typeof fn !== 'function') return fn;
|
|
28
|
+
return function(...args) {
|
|
29
|
+
try {
|
|
30
|
+
const result = fn.apply(this, args);
|
|
31
|
+
if (result instanceof Promise) {
|
|
32
|
+
return result.catch(error => {
|
|
33
|
+
throw mapNativeError(error);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw mapNativeError(error);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function wrapClassMethods(Class) {
|
|
44
|
+
const prototype = Class.prototype;
|
|
45
|
+
const methods = Object.getOwnPropertyNames(prototype);
|
|
46
|
+
methods.forEach(method => {
|
|
47
|
+
if (method !== 'constructor' && typeof prototype[method] === 'function') {
|
|
48
|
+
prototype[method] = wrapNativeFunction(prototype[method]);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return Class;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function wrapClass(Class, inspectOptions) {
|
|
55
|
+
const Wrapped = wrapClassMethods(Class);
|
|
56
|
+
patchInspector(Wrapped, ...(inspectOptions || []));
|
|
57
|
+
return Wrapped;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Custom error classes
|
|
61
|
+
class ElementNotFoundError extends Error {
|
|
62
|
+
constructor(message) {
|
|
63
|
+
super(message);
|
|
64
|
+
this.name = 'ElementNotFoundError';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class TimeoutError extends Error {
|
|
69
|
+
constructor(message) {
|
|
70
|
+
super(message);
|
|
71
|
+
this.name = 'TimeoutError';
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class PermissionDeniedError extends Error {
|
|
76
|
+
constructor(message) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.name = 'PermissionDeniedError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
class PlatformError extends Error {
|
|
83
|
+
constructor(message) {
|
|
84
|
+
super(message);
|
|
85
|
+
this.name = 'PlatformError';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
class UnsupportedOperationError extends Error {
|
|
90
|
+
constructor(message) {
|
|
91
|
+
super(message);
|
|
92
|
+
this.name = 'UnsupportedOperationError';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
class UnsupportedPlatformError extends Error {
|
|
97
|
+
constructor(message) {
|
|
98
|
+
super(message);
|
|
99
|
+
this.name = 'UnsupportedPlatformError';
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class InvalidArgumentError extends Error {
|
|
104
|
+
constructor(message) {
|
|
105
|
+
super(message);
|
|
106
|
+
this.name = 'InvalidArgumentError';
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
class InternalError extends Error {
|
|
111
|
+
constructor(message) {
|
|
112
|
+
super(message);
|
|
113
|
+
this.name = 'InternalError';
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Error mapping function
|
|
118
|
+
function mapNativeError(error) {
|
|
119
|
+
if (!error.message) return error;
|
|
120
|
+
|
|
121
|
+
const message = error.message;
|
|
122
|
+
if (message.startsWith('ELEMENT_NOT_FOUND:')) {
|
|
123
|
+
return new ElementNotFoundError(message.replace('ELEMENT_NOT_FOUND:', '').trim());
|
|
124
|
+
}
|
|
125
|
+
if (message.startsWith('OPERATION_TIMED_OUT:')) {
|
|
126
|
+
return new TimeoutError(message.replace('OPERATION_TIMED_OUT:', '').trim());
|
|
127
|
+
}
|
|
128
|
+
if (message.startsWith('PERMISSION_DENIED:')) {
|
|
129
|
+
return new PermissionDeniedError(message.replace('PERMISSION_DENIED:', '').trim());
|
|
130
|
+
}
|
|
131
|
+
if (message.startsWith('PLATFORM_ERROR:')) {
|
|
132
|
+
return new PlatformError(message.replace('PLATFORM_ERROR:', '').trim());
|
|
133
|
+
}
|
|
134
|
+
if (message.startsWith('UNSUPPORTED_OPERATION:')) {
|
|
135
|
+
return new UnsupportedOperationError(message.replace('UNSUPPORTED_OPERATION:', '').trim());
|
|
136
|
+
}
|
|
137
|
+
if (message.startsWith('UNSUPPORTED_PLATFORM:')) {
|
|
138
|
+
return new UnsupportedPlatformError(message.replace('UNSUPPORTED_PLATFORM:', '').trim());
|
|
139
|
+
}
|
|
140
|
+
if (message.startsWith('INVALID_ARGUMENT:')) {
|
|
141
|
+
return new InvalidArgumentError(message.replace('INVALID_ARGUMENT:', '').trim());
|
|
142
|
+
}
|
|
143
|
+
if (message.startsWith('INTERNAL_ERROR:')) {
|
|
144
|
+
return new InternalError(message.replace('INTERNAL_ERROR:', '').trim());
|
|
145
|
+
}
|
|
146
|
+
return error;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Wrap the native classes
|
|
150
|
+
const Desktop = wrapClassMethods(native.Desktop);
|
|
151
|
+
const Element = wrapClass(native.Element);
|
|
152
|
+
const Locator = wrapClass(native.Locator);
|
|
153
|
+
const Selector = wrapClass(native.Selector);
|
|
154
|
+
|
|
155
|
+
// Export everything
|
|
156
|
+
module.exports = {
|
|
157
|
+
Desktop,
|
|
158
|
+
Element,
|
|
159
|
+
Locator,
|
|
160
|
+
Selector,
|
|
161
|
+
// Export error classes
|
|
162
|
+
ElementNotFoundError,
|
|
163
|
+
TimeoutError,
|
|
164
|
+
PermissionDeniedError,
|
|
165
|
+
PlatformError,
|
|
166
|
+
UnsupportedOperationError,
|
|
167
|
+
UnsupportedPlatformError,
|
|
168
|
+
InvalidArgumentError,
|
|
169
|
+
InternalError
|
|
170
|
+
};
|