Sunday, June 14, 2020

Window application automation using Python (pywinauto)

Notepad / excel / Mspaint automation using pywinauto

Pre-requested :- python set-up is required 

1. Installing Pywinauto: 


pip install pywinauto
2. python code : 
from pywinauto import application
import time 
notepad = application.Application(backend='uia').start('notepad.exe')

notepad_window = notepad.window(title='Untitled - Notepad')
notepad_window.type_keys("Hello Python World!", with_spaces=True)

time.sleep(2)

notepad_window.child_window(title='Close', control_type='Button').click_input()
notepad_window.child_window(title='Don\'t Save', control_type='Button').click_input()

3 . same Notepad test with different syntax   

app = application.Application().start("notepad.exe")

//'find available dialogs ,controls of an application in pywinauto?'
# app.Notepad.print_control_identifiers(filename='output.txt')-- > generate the output is added in file output.txt
# below generate output in console
app.Notepad.print_control_identifiers()

 app.child_window(title='Maximize', control_type='Button')

 app.SaveAs.ComboBox5.Select("UTF-8")

time.sleep(2)
app.Notepad.edit1.SetText("This is me typing %r" )
time.sleep(2)

# close the file 
app.Notepad.TypeKeys("^S")
# app.Notepad.MenuSelect("File -> Exit")

time.sleep(2)
app.SaveAs.edit1.SetText("Test_File.txt" )
time.sleep(2)
app.SaveAs.Save.Click()


4. console output generated by identifier function app.Notepad.print_control_identifiers()
# Control Identifiers:
# 
# Notepad - 'Untitled - Notepad'    (L531, T155, R1688, B614)
# ['Untitled - Notepad', 'Notepad', 'Untitled - NotepadNotepad']
# child_window(title="Untitled - Notepad", class_name="Notepad")
#    | 
#    | Edit - 'This is me typing %r'    (L542, T230, R1677, B567)
#    | ['Untitled - NotepadEdit', 'Edit']
#    | child_window(title="This is me typing %r", class_name="Edit")
#    | 
#    | StatusBar - ''    (L542, T567, R1677, B603)
#    | ['StatusBar   Ln 1, Col 21  ', 'StatusBar   Windows (CRLF)', 'StatusBar', 'StatusBar100%', 'Untitled - NotepadStatusBar']
#    | child_window(class_name="msctls_statusbar32")
#    | 
#    | Button - 'Maximize'     (L1664, T623, R1718, B656)
#    | ['MaximizeButton' , 'Button4', 'Maximize']
#    | child_window(title="Maximize", control_type="Button")
5. Other Functions and arguments used to handle the execution  
# class_name: Elements with this window class
# class_name_re: Elements whose class matches this regular expression
# parent: Elements that are children of this
# process: Elements running in this process
# title: Elements with this text
# title_re: Elements whose text matches this regular expression
# top_level_only: Top level elements only (default=**True**)
# visible_only: Visible elements only (default=**True**)
# enabled_only: Enabled elements only (default=False)
# best_match: Elements with a title similar to this
# handle: The handle of the element to return
# ctrl_index: The index of the child element to return
# found_index: The index of the filtered out child element to return
# predicate_func: A user provided hook for a custom element validation
# active_only: Active elements only (default=False)
# control_id: Elements with this control id
# control_type: Elements with this control type (string; for UIAutomation elements)
# auto_id: Elements with this automation id (for UIAutomation elements)
# framework_id: Elements with this framework id (for UIAutomation elements)
# backend: Back-end name to use while searching (default=None means current active backend)
Reference :  

https://www.mbalslow.com/blog/article/how-to-get-started-gui-automation-pywinauto-python/