Environment setup:
-
Install Python 3.7.
-
install package “pywinauto”.
-
https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html
Install “Inspect” for view control structure. (Windows 10 installed
under “C:\Program Files (x86)\Windows
Kits\10\bin\10.0.17763.0\x64\inspect.exe”)If there is no inspect app in your envrionment, please install it use "Windows Software Development Kit" in here and check the two checkbox as below page.
How to use “Inspact”:
- Launch “Inspect.exe” under install path.
- Open the app which you want test. (For example: Ssms.exe).
- You will find the controls for the test app.
Use “pywinauto” package to test Ssms:
Example Code:
It is will click “Spotlight” menu item on Ssms and print the second menu items under “Spotlight” menu item.
import pywinauto
import time
import os
import signal
import subprocess
import sys
def main(pbitFile,pbixFile):
reportFile = r'%s' % pbitFile
p = subprocess.Popen(r'C:\Program Files\Microsoft Power BI Desktop\bin\PBIDesktop.exe')
time.sleep(15)
base = os.path.basename(reportFile)
filename_withoutextension = os.path.splitext(base)[0]
app = pywinauto.application.Application().connect(
title='Untitled - Power BI Desktop', timeout=120)
try:
app.window(best_match='Untitled - Power BI Desktop').set_focus()
time.sleep(30)
app.window(best_match='Untitled - Power BI Desktop').type_keys('^o')
app.Open.edit1.set_text(reportFile)
app.Open.Open.double_click() # opening large file
# make sure "Open" dialog became invisible
app.Open.wait_not('visible')
potentialWindow = app.window(best_match='Potential security risk')
if potentialWindow.exists():
potentialWindow.type_keys('{ENTER}')
time.sleep(3)
fileQueryWindow = app.window(best_match='File uses DirectQuery')
if fileQueryWindow.exists():
fileQueryWindow.Click()
fileQueryWindow.type_keys('{TAB 1}')
fileQueryWindow.type_keys('{ENTER}')
app.window(best_match=f"{filename_withoutextension}").set_focus()
app.window(best_match=f"{filename_withoutextension}").type_keys(
'{ENTER 2}')
app.window(best_match=f"{filename_withoutextension}").wait_not(
'visible')
time.sleep(30)
filterPane = app.window(best_match='Introducing the new filter pane')
if filterPane.exists():
filterPane.type_keys('{ENTER}')
app.window(best_match='Untitled - Power BI Desktop').type_keys('^s')
app.SaveAs.edit1.set_text(pbixFile)
app.SaveAs.Save.double_click()
time.sleep(30)
except:
print("Unexpected error:", sys.exc_info()[0])
exit(1)
finally:
os.kill(p.pid, signal.SIGTERM)
if __name__ == "__main__":
main(sys.argv[1],sys.argv[2])