from pywinauto.application import Application app = Application().Start(cmd_line=u'"C:\\Program Files (x86)\\Notepad++\\notepad++.exe" ') notepad = app[u'Notepad++'] notepad.Wait('ready') systabcontrol = notepad.Tab systabcontrol.Select(u'new 1') app.Kill_()
import pywinauto pwa_app = pywinauto.application.Application() ctrl.Select(0)
systabcontrol
much clearer than some kind of ctrl
. Names are formed based on the class name of the control, or from the shortest name for access (from pywinauto). Only if both of these cases were unsuccessful, would the faceless one be used - control
. button = calcframe.Button19 button.Click() button2 = calcframe.Button20 button2.Click()
button = calcframe.Button19 button.Click() button2 = calcframe.Button20 button2.Click() button.Click() # Click Button19
app = Application().Start(cmd_line=...
and app = Application().Connect(title=...
In most cases, Start
will be enough, but if not needed start the application, select Application.Connect
in the context menu of the object tree by clicking on the window name. The code in the editor will be updated, the commands calcframe.Wait('ready')
with the Application().Start
method will disappear - calcframe.Wait('ready')
at the beginning and app.Kill_()
at the end. from pywinauto.application import Application app = Application().Start(cmd_line=u'"C:\\Program Files (x86)\\Notepad++\\notepad++.exe" ') notepad = app[u'Notepad++'] notepad.Wait('ready') app.Kill_()
from pywinauto.application import Application app = Application().Connect(title=u'new 1 - Notepad++', class_name='Notepad++') notepad = app[u'Notepad++']
app = Application().Start(...)
.root
element in the tree. At the same time, all child elements will be updated.Window#657198
, this SWAPY itself formed the name from the handle of the window, since the name was not defined in the usual way ( window.Texts()
). window = app.Dialog edit = window.Edit2 edit.Click() #
app
variable. With the autogeneration code for this test, we are done. Please note that Notepad ++ will be started and closed after the test; the last line of app.Kill_()
is responsible for this. from pywinauto.application import Application expected_text = “...” app = Application().Start(cmd_line=u'"C:\\Program Files (x86)\\Notepad++\\notepad++.exe" ') notepad = app[u'Notepad++'] notepad.Wait('ready') menu_item = notepad.MenuItem(u'&?->\u041e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435...\tF1') menu_item.Click() window = app.Dialog edit = window.Edit2 actual_text = edit.Texts() app.Kill_() assertEqual(expected_text, actual_text)
ToolBar
in the element tree and perform the Click
action on the button with the index 0. As a result, the code will appear and one new tab will open. toolbar_button2 = toolbarwindow.Button(1) toolbar_button2.Click()
Clear last command
will cancel the last command (the selection) - just what we need. To completely clear the code, there is the Clear the code
command. Full cleaning is hidden behind a dialogue with confirmation, in order to avoid accidents at work. toolbar_button.Click()
toolbar_button = toolbarwindow.Button(0)
and you do not need to initialize it to click again.toolbarwindow.DragMouseInput
method. Details of use can be found in the documentation .systabcontrol.GetTabRect(0).mid_point()
# automatically generated by SWAPY from pywinauto.application import Application app = Application().Start(cmd_line=u'"C:\\Program Files (x86)\\' u'Notepad++\\notepad++.exe" ') notepad = app[u'Notepad++'] notepad.Wait('ready') systabcontrol = notepad.Tab assertEqual([u'Tab', u'new 1'], systabcontrol.Texts()) toolbarwindow = notepad[u'3'] toolbar_button = toolbarwindow.Button(0) toolbar_button.Click() toolbar_button.Click() assertEqual([u'Tab', u'new 1', u'new 2', u'new 3'], systabcontrol.Texts()) systabcontrol.DragMouseInput( press_coords=systabcontrol.GetTabRect(0).mid_point(), release_coords=systabcontrol.GetTabRect(2).mid_point()) assertEqual([u'Tab', u'new 2', u'new 3', u'new 1'], systabcontrol.Texts()) app.Kill_()
app = Application().Start ... app.Kill_()
. But in our case, we do not need to run Notepad ++ again.Application().Start
to Application().Connect
you need to call the context menu for the Notepad ++ application window and select Application().Connect
. # automatically generated by SWAPY from pywinauto.application import Application import time import os SAVE_PATH = r"Notepad_default_path" app = Application().Start(cmd_line=u'"C:\\Windows\\system32\\notepad.exe" check.txt') notepad = app.Notepad notepad.Wait('ready') edit = notepad.Edit edit.TypeKeys("^a^c") # Copy all the text app2 = Application().Connect(title=u'new 1 - Notepad++', class_name='Notepad++') notepad2 = app2[u'Notepad++'] notepad2.Restore() scintilla = notepad2[u'1'] scintilla.TypeKeys("^a^v") # Paste the text #Save a file menu_item = notepad2.MenuItem(u'&\u0424\u0430\u0439\u043b->\u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u0430\u043a...\tCtrl+Alt+S') menu_item.Click() window = app2.Dialog edit2 = window.Edit filename = "checked_at_%s" % time.time() # Compose a filename edit2.TypeKeys(filename) button = window.Button button.Click() with open(os.path.join(SAVE_PATH, filename)) as f: assertEqual(“expected_text”, f.read()) app.Kill_()
u0424 \ u0430 \ u0439 \ u043b -> \ u0421 \ u043e \ u0445 \ u0440 \ u0430 \ u043d \ u0438 \ u0442 \ u044c \ u043a \ u0430 \ u043a ... \ tCtrl + Alt # automatically generated by SWAPY from pywinauto.application import Application import time import os SAVE_PATH = r"Notepad_default_path" app = Application().Start(cmd_line=u'"C:\\Windows\\system32\\notepad.exe" check.txt') notepad = app.Notepad notepad.Wait('ready') edit = notepad.Edit edit.TypeKeys("^a^c") # Copy all the text app2 = Application().Connect(title=u'new 1 - Notepad++', class_name='Notepad++') notepad2 = app2[u'Notepad++'] notepad2.Restore() scintilla = notepad2[u'1'] scintilla.TypeKeys("^a^v") # Paste the text #Save a file menu_item = notepad2.MenuItem(u'&\u0424\u0430\u0439\u043b->\u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u0430\u043a...\tCtrl+Alt+S') menu_item.Click() window = app2.Dialog edit2 = window.Edit filename = "checked_at_%s" % time.time() # Compose a filename edit2.TypeKeys(filename) button = window.Button button.Click() with open(os.path.join(SAVE_PATH, filename)) as f: assertEqual(“expected_text”, f.read()) app.Kill_()
Click()
and then manually change to get the text - Texts()
. Or manually added TypeKeys
. Future releases have yet to simplify such popular actions by adding additional items to the context menu.window.Edit
, and if this is not possible (invalid name for Python variable), then through __getitem__
- window[u'0']
.__getitem__
. The idea is still the easiest - to get a short codetoolbarwindow = notepad[u'3']
. Everything works, everything is OK. But imagine, you opened this test after a while, and there is such a magic number. Instead, the troika could be Toolbar
- the most understandable, and not the shortest name. The plans are to give the user the opportunity to choose a name (“Name! Name, sister!”).Source: https://habr.com/ru/post/270247/
All Articles