Seapine Labs
Personal tools

SDK Python Tutorial

From Seapine Labs

Jump to: navigation, search

This article includes everything you ever wanted to know about writing TestTrack SDK applications in Python! Don't like Python? Use the Java, C# or VB tutorials. Be sure to check out the TestTrack SDK Help pages for more information.

Want Seapine to write your SOAP app for you? Email us for more information.

Contents

[edit] Getting Started

You must install the TestTrack SDK as part of your server installation. If you haven't done this, you'll need to run the TestTrack installer for the version you have installed. Once installed, there are 2 files of interest.

  • http://yourserver/cgi-bin/ttsoapcgi.exe - CGI Executable that all TestTrack SDK commands go through. It's not important to understand CGI, just know where this file was installed.
  • http://server/ttsoapcgi.wsdl - The WSDL file that defines the language you use when talking to the CGI executable. Anyone that uses the TestTrack SDK uses this wsdl, there is no need to generate a specific wsdl for your installation. Everything is genericized so that, no matter how you customize TestTrack, this one wsdl file will handle it.

Once you've installed the TestTrack SDK, you're pretty much ready to roll.

[edit] No Parser?

There are a number of Python libraries that claim to support SOAP, but I've yet to find one that actually works. Generally, they lack support for complex objects which are used throughout the TT SDK. This means we have do things the old fashioned way; build raw XML.

The lack of libraries will require a little more maintenance, as well. Since we're dealing with raw XML requests/responses, if format changes in a new release of TestTrack our scripts will break. This doesn't happen often, but it's something to keep in mind. This tutorial was last updated with the release of TestTrack 2008.

[edit] Create a Connection

The TestTrack SDK requires authentication before you can retrieve and save data.

# Create the ProjectLogon request
request = ""
request += "<?xml version='1.0' encoding='utf-8' ?>\n"
request += """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ttns="urn:testtrack-interface" xmlns:types="urn:testtrack-interface/encodedTypes">\n"""
request += "  <SOAP-ENV:Body SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>\n"
request += "    <ttns:ProjectLogon>\n"
request += "      <pProj href='#id1' />\n"
request += "      <username xsi:type='xsd:string'>" +  "MyUser"  + "</username>\n"
request += "      <password xsi:type='xsd:string'>" +  "MyPwd"  + "</password>\n"
request += "    </ttns:ProjectLogon>\n"
request += "    <ttns:CProject id='id1' xsi:type='ttns:CProject'>\n"
request += "      <database href='#id2' />\n"
request += "      <options href='#id3' />\n"
request += "    </ttns:CProject>\n"
request += "    <ttns:CDatabase id='id2' xsi:type='ttns:CDatabase'>\n"
request += "      <name xsi:type='xsd:string'>" +  "MyProject"  + "</name>\n"
request += "    </ttns:CDatabase>\n"
request += "    <SOAP-ENC:Array id='id3' SOAP-ENC:arrayType='ttns:CProjectDataOption[1]'>\n"
request += "      <Item href='#id4' />\n"
request += "    </SOAP-ENC:Array>\n"
request += "    <ttns:CProjectDataOption id='id4' xsi:type='ttns:CProjectDataOption'>\n"
request += "      <name xsi:type='xsd:string'>TestTrack Pro</name>\n"
request += "    </ttns:CProjectDataOption>\n"
request += "  </SOAP-ENV:Body>\n"
request += "</SOAP-ENV:Envelope>"

# Create a connection to the SOAP CGI.
httpsvr = httplib.HTTPConnection("myserver", "myport")
httpsvr.putrequest("POST", "http://myserver/cgi-bin/ttsoapcgi.exe")
httpsvr.putheader("Content-Type", "text/xml")
httpsvr.putheader("Content-Length", str(len(request)))
httpsvr.endheaders()
httpsvr.send(request)

# Pull the session id, from the logon.
response = httpsvr.getresponse()
session = response.read()
session = session[session.find("<Cookie>")+8:session.find("</Cookie>")]

#      ...do some stuff...


# When you're finished, log off.
request = ""
request += "<?xml version='1.0' encoding='utf-8' ?>\n"
request += """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ttns="urn:testtrack-interface" xmlns:types="urn:testtrack-interface/encodedTypes">"""
request += "  <SOAP-ENV:Body SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>\n"
request += "    <ttns:DatabaseLogoff>\n"
request += "      <cookie xsi:type='xsd:long'>" + str(session) + "</cookie>\n"
request += "    </ttns:DatabaseLogoff>\n"
request += "  </SOAP-ENV:Body>\n"
request += "</SOAP-ENV:Envelope>"

httpsvr = httplib.HTTPConnection("myserver", "myport")
httpsvr.putrequest("POST", "http://myserver/cgi-bin/ttsoapcgi.exe")
httpsvr.putheader("Content-Type", "text/xml")
httpsvr.putheader("Content-Length", str(len(request)))
httpsvr.endheaders()
httpsvr.send(request)

# Was logoff successful?
response = httpsvr.getresponse()

Things to Know:

  • You cannot logon via the TestTrack SDK if you are already logged in via one of the TestTrack clients. It's best to use a dedicated account for your SDK scripting, this ensures you'll always be able to login.
  • If you do not explicitly call DatabaseLogoff, your user will remain logged in until the TestTrack SDK times out. This could present a problem if you try to re-run the script inside that timeframe. It's important to use try/catch exception handling to ensure that an exception in your program doesn't skip logging you off.
  • The CProjectDataOption array governs what licenses you use and functionality you have access to. For example, you can't use a TestTrack TCM license if you don't specify one in the CProjectDataOption array.

[edit] Query Objects

There are two ways to retrieve data through the TestTrack SDK. You can explicitly call a getObject method or you can call the getRecordListForTable method.

[edit] getObject

Calling the get method on an object is useful when you know exactly which object you need. For performance reasons, this is not recommended when you want to extract data from multiple objects of the same type.

# retrieve defect #45, with attachments.
request = ""
request += "<?xml version='1.0' encoding='utf-8' ?>\n"
request += """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ttns="urn:testtrack-interface" xmlns:types="urn:testtrack-interface/encodedTypes">"""
request += "  <SOAP-ENV:Body SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>\n"
request += "    <ttns:getDefect>\n"
request += "      <cookie xsi:type='xsd:long'>" + p_session + "</cookie>\n"    
request += "      <defectNumber xsi:type='xsd:long'>" + p_defectnumber + "</defectNumber>\n"
request += "      <summary xsi:type='xsd:string'>" + p_summary + "</summary>\n"
request += "      <bDownloadAttachments xsi:type='xsd:boolean'>true</bDownloadAttachments>\n"
request += "    </ttns:getDefect>\n"
request += "  </SOAP-ENV:Body>\n"
request += "</SOAP-ENV:Envelope>"

# retrieve test case #312, with attachments.

[edit] getRecordListForTable

If you'd rather query multiple objects of the same type, similar to a SELECT statement in SQL, use getRecordListForTable. This method allows you to specify what data you want to retrieve and apply a filter to the results.

# number, summary, custom field, product and type

# fetch all defects

When calling getRecordListForTable you must specify both the object type you want to query and an array of fields you want to retrieve. Optionally, you can also specify a filter that you've pre-configured in TestTrack.

All of this information can be hard-coded as shown in the previous example, or dynamically passed as shown below.

# What object types can I query?

# What field data is available for a given object type?

# What filters are available?

Things to Know:

  • TableColumn elements use the field's Short Label value.
  • Pretty much anything you can view in a list view column within the GUI clients, you can retrieve through getRecordListForTable.

[edit] Create Object

Adding an object is simply a matter of creating a new instance and calling the addObject method.

For example, to create a defect:

# Create the CDefect object.  Better to do in ElementTree.
defect="""<pDefect>
      <recordid>5</recordid>
      <defectnumber>9</defectnumber>
      <summary>When accessing the database MOOGLE via the web, some of the characters do not display correctly</summary>
      <type>Cosmetic</type>
      <component>Component Y</component>
      <reference>a</reference>
      <enteredby>Administrator, System</enteredby>
      <dateentered>2006-08-30</dateentered>
      <locationaddedfrom>Add window</locationaddedfrom>
      <datetimecreated>2006-10-05T07:59:17-07:00</datetimecreated>
      <datetimemodified>2008-07-09T17:49:27-07:00</datetimemodified>
      <createdbyuser>Administrator, System</createdbyuser>
      <modifiedbyuser>SoapScript2</modifiedbyuser>
      <actualhourstofix>40</actualhourstofix>
      <estimatedhours>8</estimatedhours>
      <reportedbylist ns0:arrayType="ttns:CReportedByRecord[1]" ns1:type="SOAP-ENC:Array" xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance">
        <item ns1:type="ttns:CReportedByRecord">
          <recordid>5</recordid>
          <foundby>Administrator, System</foundby>
          <datefound>2006-08-30</datefound>
          <foundinversion>Version 3</foundinversion>
          <comments>I was browsing to the "About Our Company" page on the website, and there was some text on the screen that did not look right.  Unfortunately, I was not able to reproduce this defect, and did not get a screenshot the first time around.  Perhaps we should have our web developers take a closer look at the code on this page.</comments>
          <reproduced>Could Not</reproduced>
          <reproducedsteps>Could not reproduce!</reproducedsteps>
          <testconfigtype>1</testconfigtype>
          <showorder>1</showorder>
        </item>
      </reportedbylist>
    </pDefect>"""

# Add the defect to TestTrack.
request=""
request += "<?xml version='1.0' encoding='utf-8' ?>\n"
request += """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ttns="urn:testtrack-interface" xmlns:types="urn:testtrack-interface/encodedTypes">"""
request += "  <SOAP-ENV:Body SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>\n"
request += "    <ttns:addDefect>\n"
request += "      <cookie xsi:type='xsd:long'>" + p_session + "</cookie>\n"
request += defect    
request += "    </ttns:addDefect>\n"
request += "</SOAP-ENV:Body>\n</SOAP-ENV:Envelope>\n"

Things to Know:

  • When setting drop-down list values, you must match exactly (case-sensitive) with an existing value in the drop-down list.
  • When setting date/time values, you must also set the set{FieldName} value to true.

[edit] Update Object

Before updating an object, you must first lock it for editing by calling editObject.

# Open defect #11 for editing.

# Change the Priority.

# Save the defect changes

# Or, you can release the edit lock w/o saving changes.

Things to Know:

  • You cannot edit an object that is being edited by another user. For example, if someone else had defect #11 open for edit in their client, the example above would fail.
  • If you successfully lock a defect for edit, that lock remains until you save it, cancel the save or logout. You should use try/catch exception handling to ensure that you release the lock as soon as possible.

[edit] Update Custom Field

# Lock the defect for edit.

# Find and update the 'My Custom' custom field.

# Save changes.

Things to Know:

  • The customfieldlist array holds the base-class CField objects, which must be casted as appropriate before you can set a value. In the example, 'My Custom' is a text field so we casted to CStringField. See the TestTrack SDK Types page for a full list of supported field types.

[edit] Update Workflow State

TestTrack calculates state based on event history. This means you can't simply set a value to change state. Instead, you have to apply the necessary events to move the object into the desired state.

# Lock the defect for edit.

# Create the Fix event.

# Populate custom field

# Add the event to defect's eventlist.

# Save our changes.

[edit] Add File Attachment

# Lock the defect for edit.

# Create the file attachment.

# Add the attachment to the defect.

# Save our changes.

[edit] Promote User/Customer

At times you may need to turn a local user or customer into a global account. There are essentially two ways to do this. You can promote them as a new global user, or you can link them to an existing global user.


In the examples below, John Bark is a local user in our TestTrack project, Sarah Kaiser is a global user in the Seapine License Server.

# Promte as new global user

# check that iResult == 0


# Promte to existing global user

#check that iResult == 0

Note: The name parameters are searched based on your user settings in TT. So if you have names setup to display as 'lname, fname', then that's the format you should use when passing them to the call. If you passed them exactly as shown in the examples above (fname lname), they won't be found and the promote will fail.

[edit] Troubleshooting

With Python, using the TestTrack SDK will practically require some debugging tools and liberal use of temporary output statements.

  • MS SOAP Toolkit - This is a small app from Microsoft that helps you trace SOAP requests. Start a formatted trace, point your SOAP client toward the tools listening port, then have the tool redirect to the real web server port. With that configured, you can intercept the response/request loop and see exactly what is being communicated to the TestTrack Server.
  • soap UI - A free tool that lets you send raw SOAP envelopes. We sometimes use this internally to debug issues, since you can control the data being being sent.













Issue Management Software | Source Code Control Software | Test Case Management