|
|
ViewsPersonal toolsTestTrack SOAP SDK Tutorial - PHPFrom Seapine LabsThis 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.
[edit] Getting StartedYou 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.
Once you've installed the TestTrack SDK, you're pretty much ready to roll. [edit] Create a ConnectionThe 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:
[edit] WSDL OutputTo 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 ObjectsThere 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] getObjectCalling 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] getRecordListForTableIf 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:
[edit] Create ObjectAdding 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:
[edit] Update ObjectBefore 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:
[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:
[edit] Update Workflow StateTestTrack 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 ObjectsYou can link defects, test cases and test runs within TestTrack. [edit] Defect Link[edit] Test Case Link[edit] Test Run Link[edit] Promote User/CustomerAt 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.
// 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 ReportIn 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] TroubleshootingWith PHP, using the TestTrack SDK will likely require some debugging tools and liberal use of temporary output statements.
[edit] Custom Fields & PolymorphismThe 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. |
|


