Windows Automation: Automating Windows 7 Notepad within S# Script

Cross-posting from Denis’ blog.

Today we have updated S# with another sample covering the use of Windows Automation API from within a common S# script.

Auto# (AutoSharp)

This is a demo project shows how easily your S# scripts can be enhanced with a set of functions that target Windows UI Automation API and the ways of forming a QA-friendly language on the top of S# runtime. Full source code for this sample is available within the latest S# runtime update here.

Note: Sample project targets Windows 7 Notepad and may not work properly on other versions of Windows because of control ids difference.

To find Windows Automation API for your version of Windows please refer to this article.

Auto# introduces 9 functions that provide Windows Automation support S# scripts:

  • Kill – close all running instances of some process
  • Launch – launch a new process and return its main window
  • Wait – sleep for some time, often required when waiting application response
  • FindByClassName – finds element by Class Name
  • FindById – finds element by Automation ID
  • FindByName – finds element by its Name
  • Expand – expands menu element
  • InvokeById – invokes element by Automation ID
  • FocusEditor – focuses textbox editor

The following workflow is executed in order to automate Notepad:

  1. Kill all running Notepad instances
  2. Launch a new Notepad process
  3. Type some text
  4. Click “File” – “Save As” in the main menu
  5. Type destination file name
  6. Agree to overwrite existing file if confirmation dialog appears
  7. Click “File” – “Exit” to close Notepad

Here’s how the S# script may look like:

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
// Close existing instances of Notepad
 
Kill("notepad");
 
// Launch a new Notepad instance and get main window
 
window = Launch("notepad");
 
// Wait 1 second
 
Wait(1000);
 
// Get main editor region
 
edit = FindByClassName(window, "Edit");
 
// focus main editor
 
FocusEditor(edit);
 
// Send sample text to the editor region
 
SendKeys.SendWait("Automating Notepad using Windows UI Automation and S#");
 
Wait(3000);
 
// Find [File] menu
 
mnuFile = FindById(window, "Item 1");
 
// Expand [File] menu
 
Expand(mnuFile);
 
Wait(1000);
 
// Invoke [Save As] menu item
 
InvokeById(window, "Item 4");
 
Wait(1000);
 
// Get [Save As] dialog
 
saveAsDialog = FindByName(window, "Save As");
 
// Get access to [FileName] textbox
 
saveAsName = FindById(saveAsDialog, "1001");
 
// Focus filename editor
 
FocusEditor(saveAsName);
 
// Write down file name
 
SendKeys.SendWait("D:\\MyTextFile");
 
// Send [Enter] keypress
 
SendKeys.SendWait("{ENTER}");
 
Wait(1000);
 
// Check whether Overwrite Dialog appeared
 
confirmSaveAs = FindByName(saveAsDialog, "Confirm Save As");
 
if (confirmSaveAs != null)
 
{
 
    // Click [OK] button
 
    InvokeById(confirmSaveAs, "CommandButton_6");
 
    Wait(1000);
 
}
 
// Expand [File] menu
 
Expand(mnuFile);
 
Wait(1000);
 
// Click [Exit] item
 
InvokeById(window, "Item 7");

Demo project also includes a very simply UI providing possibility to alter the script above and execute it.

2 Responses to Windows Automation: Automating Windows 7 Notepad within S# Script

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

top