-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighLevelMethods.cs
More file actions
120 lines (107 loc) · 4.09 KB
/
HighLevelMethods.cs
File metadata and controls
120 lines (107 loc) · 4.09 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
using System.Diagnostics;
using System.Windows.Automation;
using static BaseMethods;
public class HighLevelMethods
{
private bool isNotepadOpen = false; // Assume notepad is closed initially
Process? notepadProcess = null;
AutomationElement? notepadWindow = null;
/// <summary>
/// Sets the value in a text field (should work for edit field or document). Removes any previous value.
/// </summary>
/// <param name="name">
/// Name of the text field (checks for exact name).
/// </param>
/// <param name="value">
/// Value to set in the text field.
/// </param>
/// <exception cref="InvalidOperationException">
/// Exception triggers if anything goes wrong (can't find the main window, can't find the text field, not a text-inputable field, etc).
/// </exception>
public void setTextInField(string name, string value)
{
try
{
if (notepadWindow == null) throw new InvalidOperationException("notepad window not found");
AutomationElement? textElement = FindDescendantWithExactName(notepadWindow, name);
if (textElement == null) throw new InvalidOperationException("text element not found");
SetElementText(textElement, value);
}
catch (Exception ex)
{
Console.WriteLine($"Error editing field: {ex}");
}
}
/// <summary>
/// Method to open notepad (notepad.exe) and identify it.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Exception triggers if anything goes wrong (can't find notepad, didn't find notepad after opening, etc.).
/// </exception>
public void openNotepad()
{
if (!isNotepadOpen)
{
try
{
// Open notepad
notepadProcess = Process.Start("notepad.exe");
if (notepadProcess == null)
{
throw new InvalidOperationException("Could not start Notepad.");
}
// Wait for Notepad to open
notepadProcess.WaitForInputIdle(5000); // Timeout: 5 seconds
Console.WriteLine("Notepad opened successfully.");
// Wait for notepad window to show up as a child of the root element
notepadWindow = WaitForChildWithName(AutomationElement.RootElement, "Notepad", 5000);
if (notepadWindow == null)
{
throw new InvalidOperationException("Could not find notepad name");
}
isNotepadOpen = true;
// Minimize
AutomationElement? minimizeButton = FindDescendantWithExactName(notepadWindow, "Minimize");
if (minimizeButton != null)
{
InvokeElement(minimizeButton);
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to open notepad: {ex}", ex);
}
}
}
/// <summary>
/// Method to close notepad via clicking the close button of the notepad window.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Exception triggers if anything goes wrong (can't find notepad, close button isn't found, etc.)
/// </exception>
public void closeNotepad()
{
if (isNotepadOpen)
{
try
{
if (notepadWindow == null)
{
throw new InvalidOperationException("Notepad cannot be found");
}
AutomationElement? closeButton = FindDescendantWithExactName(notepadWindow, "Close");
if (closeButton == null)
{
throw new InvalidOperationException("No close button found");
}
InvokeElement(closeButton);
isNotepadOpen = false;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to close notepad: {ex}");
Debug.Write($"Could not close notepad: {ex}");
}
}
}
}