Saturday, May 23, 2020

pywinauto note


1. start and lookup
backend="win32" is default backend, good for MFC, VB6, VCL, simple WinForms controls and most of the old legacy apps
from pywinauto.application import Application
app = Application(backend="uia").start('notepad.exe')
dlg_spec = app.window(title='无标题 - 记事本')
>>> dlg_spec
>>> dlg_spec.wrapper_object()                   
Actual window lookup is performed by wrapper_object(), it needs the window is on foreground
>>> dlg_spec.print_control_identifiers()

2. magic lookup
app.UntitledNotepad.menu_select("File->SaveAs")
app.SaveAs.ComboBox5.select("UTF-8")
At the 2nd line the SaveAs dialog might not be open by the time this line is executed. So what happens is that we wait until we have a
control to resolve before resolving the dialog. At that point if we can’t find a SaveAs dialog with a ComboBox5 control then we wait a
very short period of time and try again, this is repeated up to a maximum time (currently 5 seconds!)
This is to avoid having to use time.sleep or a “wait” function explicitly.
In some cases, you might prefer disable the magic lookup system, so that Pywinauto immediately raises if you access an attribute
which exists neither on the WindowSpecification object, nor on the underlying element-wrapper object.
In this case, turn off the allow_magic_lookup argument of your Desktop or Application instance:
app = Application(allow_magic_lookup=False)

3. sample notepad in Chinese
from pywinauto import application
app = application.Application()
app.start("Notepad.exe")
app['无标题 - 记事本'].draw_outline()
app['无标题 - 记事本'].menu_select("编辑 -> 替换(&R)...")
app['替换'].print_control_identifiers()
app['替换'].取消.click()
app['无标题 - 记事本'].Edit.type_keys("Hi from Python interactive prompt %s" % str(dir()), with_spaces = True)
app['无标题 - 记事本'].menu_select("文件 -> 退出")
app['记事本'].不保存.click()

4. Huatai
import time
from pywinauto import application

app = application.Application()
app.start("c:/htwt/xiadan.exe")
#app['用户登录'].print_control_identifiers()
app['用户登录'].主站测速.click()

time.sleep(1)
app['<华泰证券>委托连接测试'].开始测速.click()
while True:
try:
app['<华泰证券>委托连接测试'].开始测速.wait("enabled")
break
except RuntimeError:
pass

print(app['<华泰证券>委托连接测试'].开始测速.is_enabled())
app['<华泰证券>委托连接测试'].确定.click()

app['用户登录'].Edit1.set_focus()
app['用户登录'].Edit1.type_keys('******')

5.
dlg = app.window(title_re="Page Setup", class_name="#32770")
app['dlg']['control']
app[u'your dlg title'][u'your ctrl title']

6.
# call ensure_text_changed(ctrl) every 2 sec until it's passed or timeout (4 sec) is expired
@always_wait_until_passes(4, 2)
def ensure_text_changed(ctrl):
    if previous_text == ctrl.window_text():
        raise ValueError('The ctrl text remains the same while change is expected')

7.
from timings import Timings

Timings.defaults()
Timings.slow() # double all timings (~2x slower script execution)
Timings.fast() # divide all timings by two (~2x faster)

8.
DELAYED RESOLUTION FOR SUCCESS Taking the example
app.dlg.control.action()

No comments:

Post a Comment