-1

I've below code in python to connect to HP QC ALM and it's working as expected when values are hardcoded:

from win32com.client import Dispatch
class QC_ConnectorClass(object):
def __init__(self):
    print("class init")

def ConnectToQC(self):
    #HP QC OTA methods
    self.TD = Dispatch("TDApiOle80.TDConnection.1")
    self.TD.InitConnectionEx("http://hpqcurl.org")
    self.TD.Login("UName","Pwd")
    self.TD.Connect("Domain","project")
    if self.TD.Connected == True:
        print("Logged in")
        self.TD.Logout();
        print("Logged out")
        self.TD.ReleaseConnection();
    else:
        print("Login failed")

On passing hp qc url to variable like

hpQCURL="http://hpqcurl.org" 

and pass the variable like this:

self.TD.InitConnectionEx(hpQCURL)

I receive the following error:

File "<COMObject TDApiOle80.TDConnection.1>", line 2, in InitConnectionEx
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147023174), None)
Nick
  • 3,454
  • 6
  • 33
  • 56
Karthick Raju
  • 757
  • 8
  • 29

1 Answers1

3

from win32com.client import Dispatch
class QC_ConnectorClass(object):
    var = "http://hpqcurl.org"
    def __init__(self):
        print("class init")    
    def ConnectToQC(self):
        #HP QC OTA methods
        self.TD = Dispatch("TDApiOle80.TDConnection.1")
        self.TD.InitConnectionEx(QC_ConnectorClass.var)
        self.TD.Login("UName","Pwd")
        self.TD.Connect("Domain","project")
        if self.TD.Connected == True:
            print("Logged in")
            self.TD.Logout();
            print("Logged out")
            self.TD.ReleaseConnection();
        else:
            print("Login failed")

Worked for me, but you can also initialize the variable globally outside the scope of the class. In this case I defined a static variable, that's why I need to call it in this way: QC_ConnectorClass.var But take a look on this answer to understand the importance of the position of the initialization (correct way to define class variables in Python)

  • is there a way I can declare empty var and get the config value from .ini file on __init__ and pass the same in ConnnectToQC? – Karthick Raju Nov 27 '18 at 12:41
  • I found your answer helpful. I made a mistaken in .ini file. Since I am from .Net background, I pass values inside double quotes. So, I had like `url="http://url.org"` in .ini file. Actually, double quotes not required and has to be like `url=http://url.org`. It's fixed now. Thanks! Welcome to Stackoverflow! – Karthick Raju Nov 27 '18 at 13:11