如何使用pywinauto测试窗口应用程序

2024-04-19 10:34:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下网站的代码:https://pywinauto.github.io/

from pywinauto.application import Application
app = Application().start("notepad.exe")
app.UntitledNotepad.menu_select("Help->About Notepad")
app.AboutNotepad.OK.click()
app.UntitledNotepad.Edit.type_keys("pywinauto Works!", with_spaces = True)

我有一个问题是:如何知道记事本有无记事本控制。我使用Autoit获取控件信息,但无法获取某些控件的信息,无法获取UntitledNotepad控件,但为什么上面的代码在记事本中知道UntitledNotepad。请告诉我如何知道记事本中是否存在UnttledNotepad控件

谢谢


Tags: 代码fromhttpsioimportgithub信息app
2条回答
>>> from pywinauto.application import Application
>>> app = Application().start("notepad.exe")
>>> window = app.top_window ()
>>> window.print_control_identifiers ()
Control Identifiers:

Notepad - 'Untitled - Notepad'    (L183, T308, R1798, B937)
['Untitled - NotepadNotepad', 'Untitled - Notepad', 'Notepad']
child_window(title="Untitled - Notepad", class_name="Notepad")
   |
   | Edit - ''    (L191, T359, R1790, B929)
   | ['Untitled - NotepadEdit', 'Edit']
   | child_window(class_name="Edit")
   |
   | StatusBar - ''    (L191, T906, R1790, B929)
   | ['StatusBar   Ln 1, Col 1  ', 'StatusBar', 'Untitled - NotepadStatusBar']
   | child_window(class_name="msctls_statusbar32")
>>> window = app.window (best_match = "UntitledNotepad")
>>> window.exists ()
True
>>> window = app["UntitledNotepad"]
>>> window.exists ()
True
>>> window = app.UntitledNotepad
>>> window.exists ()
True
>>> window = app.window (best_match = "Untitled - Notepad")
>>> window.exists ()
True
>>> window = app["Untitled - Notepad"]
>>> window.exists ()
True
>>> window.menu_select("Help->About Notepad")
>>> window.exists ()
True
>>> window = app.window (class_name="Notepad")
>>> window.exists ()
True
>>> window = app.window (title="Untitled - Notepad")
>>> window.exists ()
True

https://pywinauto.readthedocs.io/en/latest/getting_started.html

我写代码如下:

    app = Application().start("notepad.exe")
    app.Notepad.menu_select("Help->About Notepad")
    window = app.top_window()    
    window.print_control_identifiers()

但它不显示关于记事本视图的Ok按钮信息

它仅显示以下信息:

    Starting test: EmptyProject.Test.Test
20191219 16:37:39.197 : INFO : Control Identifiers:

Dialog - 'About Notepad'    (L1019, T401, R1610, B930)
['About NotepadDialog', 'Dialog', 'About Notepad']
child_window(title="About Notepad", class_name="#32770")
   | 
   | Static - ''    (L1085, T543, R1582, B545)
   | ['Static', 'About NotepadStatic', 'Static0', 'Static1', 'About NotepadStatic0', 'About NotepadStatic1']
   | child_window(class_name="Static")
   | 
   | Static - ''    (L1079, T563, R1116, B603)
   | ['Static2', 'About NotepadStatic2']
   | child_window(class_name="Static")
   | 
   | Static - 'Microsoft Windows'    (L1128, T563, R1531, B583)
   | ['Static3', 'Microsoft Windows', 'Microsoft WindowsStatic']
   | child_window(title="Microsoft Windows", class_name="Static")
   | 
   | Static - 'Version 1809 (OS Build 17763.557)'    (L1128, T583, R1592, B603)
   | ['Static4', 'Version 1809 (OS Build 17763.557)', 'Version 1809 (OS Build 17763.557)Static']
   | child_window(title="Version 1809 (OS Build 17763.557)", class_name="Static")
   | 
   | Static - '© 2018 Microsoft Corporation. All rights reserved.'    (L1128, T603, R1548, B623)
   | ['© 2018 Microsoft Corporation. All rights reserved.', '© 2018 Microsoft Corporation. All rights reserved.Static', 'Static5', '© 2018 Microsoft Corporation. All rights reserved.Static0', '© 2018 Microsoft Corporation. All rights reserved.Static1']
   | child_window(title="© 2018 Microsoft Corporation. All rights reserved.", class_name="Static")
   | 
   | Static - ''    (L1128, T623, R1548, B643)
   | ['© 2018 Microsoft Corporation. All rights reserved.Static2', 'Static6']
   | child_window(class_name="Static")
   | 
   | Static - 'The Windows 10 Enterprise operating system and its user interface are protected by trademark and other pending or existing intellectual property rights in the United States and other countries/regions.'    (L1128, T643, R1548, B723)
   | ['Static7', 'The Windows 10 Enterprise operating system and its user interface are protected by trademark and other pending or existing intellectual property rights in the United  <truncated>
Ending test: EmptyProject.Test.Test

相关问题 更多 >