Seapine Labs

TestTrack SOAP SDK Tutorial - PHP

From Seapine Labs

Jump to: navigation, search

This article includes everything you ever wanted to know about writing TestTrack SDK applications in PHP! Don't like PHP? We have tutorials for a variety of languages. 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] Create a Connection

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

// Create client connection
$client = new SoapClient('http://yourserver/ttsoapcgi.wsdl', array('location' => "http://yourserver/cgi-bin/ttsoapcgi.exe", 'uri' => "urn:testtrack-interface/") );
$client->soap_defencoding = 'UTF-8';

$sessionId = 0;
try
{
   // Better to fetch then find the CProject than trying to create one.
   $strSelectPrj = "Sample Project";
   $prj = null;

   $aprj = $client->getProjectList("administrator", "");
   for ($i = 0; $i < count($aprj); ++$i)
   {
      if ($aprj[$i]->database->name == $strSelectPrj) { $prj = $aprj[$i]; break; }
   }

   // Login
   $sessionId = $client->ProjectLogon($prj, "administrator", "");

   //   ...do some stuff...
}
catch (Exception $e) {} // do something with the exception!

// When you're finished, log off.
if ($sessionId != 0) { $client->DatabaseLogoff($sessionId); }

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] WSDL Output

To view your WSDL's content, you can use the following:

// echo all of the functions defined in the wsdl
print("<pre>");
print_r($client->__getFunctions());
print("</pre>");

// echo all of the objects defined in the wsdl
print("<pre>");
print_r($client->__getTypes());
print("</pre>");

[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.
$defect = $client->getDefect($sessionId, 45, "", true);

// retrieve test case #312, with attachments.
$tc = $client->getTestCase($sessionId, 312, "", true);

[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.

// fetch all columns, for all reports
$areports = $client->getRecordListForTable($sessionId, "Report", "", "");

for ($i = 0; $i < count($reports->records); ++$i)
{
   // this is a report row
   $report = $areports->records[$i];

   // this is a piece of data about that report.
   $val = $report->row[5]->value; // "Contains" column
}

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

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:


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.
$defect = $client->editDefect($sessionId, 11, "", true);

// Change the Priority.
$defect->priority = "Before Final";

// If TTP project has custom fields, MUST re-build entire customFieldList array.
$cflist = array(); // place-holder.

// Manually build array, b/c PHP can't handle polymorphism required here.
array_push($cflist, new SoapVar( array( 'recordid' => $defect->customFieldList[0]->recordid, 'name' => $defect->customFieldList[0]->name, 'value' => $defect->customFieldList[0]->value, ), XSD_ANYTYPE, 'CStringField', 'urn:testtrack-interface' ));

// Add as many pushes as needed above then assign place-holder array to CDefect.
$defect->customFieldList = $cflist;

// Save the defect changes
$iResult = $client->saveDefect($sessionId, $defect);

// Or, you can release the edit lock w/o saving changes.
$client->cancelSaveDefect($sessionId, $defect->defectnumber);

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

// Open defect #11 for editing.
$defect = $client->editDefect($sessionId, 11, "", true);

// Re-build entire customFieldList array.
$cflist = array(); // place-holder.

// Manually build array, b/c PHP can't handle polymorphism required here.
array_push($cflist, new SoapVar( array( 'recordid' => $defect->customFieldList[0]->recordid, 'name' => $defect->customFieldList[0]->name, 'value' => $defect->customFieldList[0]->value, ), XSD_ANYTYPE, 'CStringField', 'urn:testtrack-interface' ));

// Add as many pushes as needed above, set value element for any field's value you want to alter then assign place-holder array to CDefect.
$defect->customFieldList = $cflist;

// Save changes.
$iResult = $client->saveDefect($sessionId, $defect);

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] Linking Objects

You can link defects, test cases and test runs within TestTrack.

[edit] Defect Link


[edit] Test Case Link


[edit] Test Run Link


[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
$iResult = $client->promoteUser($sessionId, "John Bark", null, "jbark");

// check that iResult == 0


// Promte to existing global user
$iResult = $client->promoteUser($sessionId, "John Bark", "Sarah Kaiser", null);

// 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] Run Report

In the example below, we'll run a report by name and fetch the results.

$reportrun = $client->getReportRunResultsByName($sessionId, "My Defects Report");

// PHP doesn't like a '-' in func/variable names so you can't access m-FileList directly.
// Here we convert to array then pass it to our handy-dandy func to find the hard way.
// See here: http://ca.php.net/manual/en/language.variables.variable.php#87564
$arunresults = VariableArray(get_object_vars($reportrun), "[m-FileList]");

for ($i = 0; $i < count($arunresults); ++$i)
{
   $filename = VariableArray(get_object_vars($arunresults[$i]), "[m-strFileName]");

   $filedata = VariableArray(get_object_vars($arunresults[$i]), "[m-pFileData]"));
}

[edit] Troubleshooting

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

  • MS SOAP Toolkit - This is a small Microsoft application that helps you trace SOAP requests. Start a formatted trace, point your SOAP client toward the tool's 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 sent.
  • If you're not sure what a function or object looks like, use the wsdl output functions to find that information.
  • The sample code here was written and tested in PHP 5.2.

[edit] Custom Fields & Polymorphism

The PHP SoapClient doesn't handle inheritance as well as it should.

Because of this, you have to make some minor modifications to the CDefect object if your project has custom fields. This might be required with other object types; basically anywhere the SOAP SDK uses ineritance like CField. The necessary code is in the custom field sample abov.














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