I am creating Microsoft SQL Server Management Studio Automation tool using python. The problem is I can't select the Child_tree(Northwind) database It's selecting the Parent_tree(Databases). I need to do much more, clicking the child_tree(Northwind) right click option (Ex. Tasks-> backup). Help me to do the best automation code. Thanks in Advance.
import pywinauto
import socket
import binascii
host = socket.gethostname() #Getting system host name
n2 = int('0b111000001100001011100110111001101110111011011110111001001100100', 2) #password
n1 = int('0b111010101110011011001010111001001101110011000010110110101100101', 2) # username
n = int('0b1110011011001010111001001110110011001010111001001101110011000010110110101100101', 2) #servername av
if (host == "systemhostXXX" or host == "systemhostyyy"): # checking the host name
try:
pwa_app = pywinauto.application.Application()
path = pwa_app.start_(r"C:/Program Files (x86)/Microsoft SQL Server/100/Tools/Binn/VSShell/Common7/IDE/Ssms.exe") #Opening the .exe file
print("Status: Application Launched successfully!!")
except:
print("Error: Applicatin Launching Error!!")
try:
pwa_app.ConnecttoServer.ComboBox1.Select("Database Engine") #Selecting the combobox value
pwa_app.ConnecttoServer.edit1.SetText(binascii.unhexlify('%x' % n))
pwa_app.ConnecttoServer.ComboBox3.Select("SQL Server Authentication")
pwa_app.ConnecttoServer.edit2.SetText(binascii.unhexlify('%x' % n1)) # convert binary into string
pwa_app.ConnecttoServer.edit3.SetText(binascii.unhexlify('%x' % n2))
print("Status: Log-in Process!!")
pwa_app.ConnecttoServer.Connect.Click()
except:
print("Error: Log-In Failed!!Please Relaunch!")
try:
pwa_app.ConnecttoServer.Ok.Click() #Button click (OK)
pwa_app.ConnecttoServer.Cancel.Click()
print("Error: Restoration going-on!!")
except:
print("Status: Log-in Success!!")
try:
w_handle = pywinauto.findwindows.find_windows(title=u'Microsoft SQL Server Management Studio', class_name='wndclass_desked_gsk')[0]
window = pwa_app.window_(handle=w_handle)
ctrl = window['TreeView']
ctrl.GetItem([u'SQL Server 8.0.2039']).Click()
ctrl.GetItem([u'SQL Server 8.0.2039', u'Databases', u'Northwind']).Click() #Selecting the database
except:
print("Database selection failed !!")
else: print 'Dear', host,'You are not Authorized to Run this program\n'
As I could understand in comments, you need waiting until main window is open after login.
Please check how it works.
EDIT:
It seems you generated the code for
'Microsoft SQL Server Management Studio'
window using SWAPY. It means that the window was already open.But in automated workflow
Log-in
is quite long operation (may take up to 10 seconds I believe). So when you clicked "Connect" button,'Microsoft SQL Server Management Studio'
is not open yet. You may see some progress window or even nothing for a few seconds.Function
find_windows
doesn't wait while the window appears on the screen. It just finds the window at that moment. So when you execute lineWindowSpecification object is created (
window
variable).ctrl = window['TreeView']
is also WindowSpecification object. They are just descriptions and are not connected with real window/control. But the following statementis equivalent to
or
pywinauto hides
WrapperObject()
call using power of Python. So it's called automatically. Default timeout is 5 seconds in this case. It might be insufficient for long time operations like Log-in. That's why I suggest callingWait('ready', timeout=20)
explicitly.'ready'
means'exists visible enabled'
using logicalAND
.