forked from acdibble/node-autoit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
136 lines (111 loc) · 5.19 KB
/
test.js
File metadata and controls
136 lines (111 loc) · 5.19 KB
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
var au = require('./index');
console.log('=== AutoIt Node.js Test Suite ===');
console.log('Testing with notepad.exe...\n');
try {
// Initialize AutoIt
console.log('1. Initializing AutoIt...');
au.Init();
console.log(' ✓ AutoIt initialized successfully');
// Launch notepad
console.log('\n2. Launching notepad.exe...');
var processId = au.Run("notepad.exe");
console.log(' ✓ Notepad launched with PID:', processId);
// Wait for notepad window to appear
console.log('\n3. Waiting for Notepad window...');
var windowExists = au.WinWait("[Class:Notepad]", "", 5000); // 5 second timeout
if (windowExists) {
console.log(' ✓ Notepad window found');
} else {
console.log(' ✗ Timeout waiting for Notepad window');
process.exit(1);
}
// Test WinExists function
console.log('\n4. Testing WinExists function...');
var exists = au.WinExists("[Class:Notepad]");
console.log(' Window exists:', exists ? '✓ Yes' : '✗ No');
// Test WinActive function (before activation)
console.log('\n5. Testing WinActive function (before activation)...');
var isActive = au.WinActive("[Class:Notepad]");
console.log(' Window active:', isActive ? '✓ Yes' : '✗ No');
// Activate the window
console.log('\n6. Activating Notepad window...');
var activated = au.WinActivate("[Class:Notepad]");
if (activated) {
console.log(' ✓ Window activated successfully');
} else {
console.log(' ✗ Failed to activate window');
}
// Test WinActive function (after activation)
console.log('\n7. Testing WinActive function (after activation)...');
au.Sleep(500); // Give time for activation
isActive = au.WinActive("[Class:Notepad]");
console.log(' Window active:', isActive ? '✓ Yes' : '✗ No');
// Get window information
console.log('\n8. Getting window information...');
var windowTitle = au.WinGetTitle("[Class:Notepad]");
console.log(' Window title:', windowTitle);
var windowHandle = au.WinGetHandle("[Class:Notepad]");
console.log(' Window handle:', windowHandle);
var windowState = au.WinGetState("[Class:Notepad]");
console.log(' Window state:', windowState);
// Send text to notepad
console.log('\n9. Sending text to Notepad...');
au.Send("Hello, AutoIt & Node.js!");
console.log(' ✓ Text sent successfully');
// Test control commands
console.log('\n10. Testing control commands...');
var isVisible = au.ControlCommand("[Class:Notepad]", "", "[CLASS:Edit]", "IsVisible");
console.log(' Edit control visible:', isVisible);
var isEnabled = au.ControlCommand("[Class:Notepad]", "", "[CLASS:Edit]", "IsEnabled");
console.log(' Edit control enabled:', isEnabled);
// Wait a moment to see the result
console.log('\n11. Waiting 3 seconds for demonstration...');
au.Sleep(3000);
// Test async function
console.log('\n12. Testing async function...');
au.ClipPut("Test clipboard content");
au.ClipGet.async(function(err, result) {
if (err) {
console.log('✗ Async ClipGet error:', err.message);
} else {
console.log('✓ Async ClipGet result:', result);
}
});
// Test truly async WinWaitActive - this should NOT block the main thread
console.log('\n13. Testing WinWaitActive.async (non-blocking behavior)...');
console.log(' Starting async WinWaitActive call...');
var startTime = Date.now();
// This should not block - we're waiting for a window that doesn't exist
// Note: timeout is in SECONDS, not milliseconds
au.WinWaitActive.async("NonExistentWindow12345", "", 2, function(err, result) {
var endTime = Date.now();
console.log(' ✓ Async WinWaitActive completed after', endTime - startTime, 'ms');
console.log(' Result:', result, '(0 = timeout, which is expected)');
// Final cleanup
console.log('\n14. Cleaning up...');
var closed = au.WinClose("[Class:Notepad]");
if (closed) {
console.log('✓ Notepad closed successfully');
} else {
console.log('✗ Failed to close Notepad');
// Force close if normal close failed
au.WinKill("[Class:Notepad]");
console.log('✓ Notepad force-closed');
}
console.log('\n=== Test completed successfully! ===');
});
// This should execute immediately, proving the async call above didn't block
console.log(' ✓ This message proves the main thread is NOT blocked!');
console.log(' (If you see this immediately, async is working correctly)');
} catch (error) {
console.error('\n✗ Test failed with error:', error.message);
console.error('Stack trace:', error.stack);
// Attempt cleanup on error
try {
au.WinKill("[Class:Notepad]");
console.log('Emergency cleanup: Notepad force-closed');
} catch (cleanupError) {
console.error('Cleanup failed:', cleanupError.message);
}
process.exit(1);
}