Search code examples
pythonpython-3.6pywinauto

pywinauto scripts work differently in execution of file vs interactive mode


I'm trying to make a script for Windows application. But the script works differently when executing the file (python test.py) vs. Python interactive mode.

Here is the code.

import pywinauto

app = pywinauto.application.Application()
app.start(r"C:\customPrograms\ControlStation.exe")
    
app.window(title_re=u'Login').Wait('visible', timeout=60, retry_interval=1)

dlg = app.Login
dlg.Edit1.TypeKeys("testSuper")
dlg.PasswordEdit.TypeKeys("test")
dlg.OK.SetFocus()
dlg.OK.click()

app.window(title_re=u'Room select').Wait('visible', timeout=60, 
retry_interval=1)

roomdlg = app.Roomselect
roomdlg.Connect.click()

app = pywinauto.Application().connect(title='tmpSuper - local')
app.testSuper.MenuBar.MenuBarClickInput('#4->#1->#0', app)
app.testSuper.People.click()

When I run this script as a file (python test.py), I've got this error:

pywinauto.base_wrapper.ElementNotEnabled

enter image description here

When I run the scripts one by one in an interactive Python session, there was no error. It works fine.

enter image description here

What is a difference between those two ways? What is the best way to execute automation scripts?

I'm using Python 3.6 and pywinauto 0.6.4.


Solution

  • This is typical timing problem (you can't reproduce it step by step because there is a delay between manual steps execution). roomdlg.Connect.click() waits for existing "Connect" button, but not for enabled state. So the error message is meaningful.

    Solution should be:

    roomdlg.Connect.wait("enabled") # timeout is optional param, default is 5 sec.
    roomdlg.Connect.click()
    

    Some apps may have issues like delayed OnClick() handlers subscription even if button is already enabled. In this case you may need app.wait_cpu_usage_lower() to indicate that app process CPU usage is below threshold.

    Current document about timings is here: https://pywinauto.readthedocs.io/en/latest/wait_long_operations.html (need to be extended with global timings settings though).