Windows Automation: Automating Windows 7 Notepad within S# Script


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:

// 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.

Unleash your fantasy 🙂

Advertisements

2 thoughts on “Windows Automation: Automating Windows 7 Notepad within S# Script

  1. Pingback: Windows Automation: Automating Windows 7 Notepad within S# Script | Orbifold
  2. Pingback: Windows Automation: Automating Windows 7 Notepad within S# Script :: Orbifold

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s