API.pm 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. # ABSTRACT: Provides an interface to TestRail's REST api via HTTP
  2. # PODNAME: TestRail::API
  3. package TestRail::API;
  4. =head1 SYNOPSIS
  5. use TestRail::API;
  6. my ($username,$password,$host) = ('foo','bar','testlink.baz.foo');
  7. my $tr = TestRail::API->new($username, $password, $host);
  8. =head1 DESCRIPTION
  9. C<TestRail::API> provides methods to access an existing TestRail account using API v2. You can then do things like look up tests, set statuses and create runs from lists of cases.
  10. It is by no means exhaustively implementing every TestRail API function.
  11. =head1 IMPORTANT
  12. All the methods aside from the constructor should not die, but return a false value upon failure.
  13. When the server is not responsive, expect a -500 response, and retry accordingly.
  14. I recommend using the excellent L<Attempt> module for this purpose.
  15. =cut
  16. use 5.010;
  17. use strict;
  18. use warnings;
  19. use Carp qw{cluck confess};
  20. use Scalar::Util qw{reftype looks_like_number};
  21. use Clone 'clone';
  22. use Try::Tiny;
  23. use JSON::MaybeXS ();
  24. use HTTP::Request;
  25. use LWP::UserAgent;
  26. use Data::Validate::URI qw{is_uri};
  27. =head1 CONSTRUCTOR
  28. =head2 B<new (api_url, user, password)>
  29. Creates new C<TestRail::API> object.
  30. =over 4
  31. =item STRING C<API URL> - base url for your TestRail api server.
  32. =item STRING C<USER> - Your TestRail User.
  33. =item STRING C<PASSWORD> - Your TestRail password.
  34. =item BOOLEAN C<DEBUG> - Print the JSON responses from TL with your requests.
  35. =back
  36. Returns C<TestRail::API> object if login is successful.
  37. my $tr = TestRail::API->new('http://tr.test/testrail', 'moo','M000000!');
  38. Dies on all communication errors with the TestRail server.
  39. Does not do above checks if debug is passed.
  40. =cut
  41. sub new {
  42. my ($class,$apiurl,$user,$pass,$debug) = @_;
  43. confess("Constructor must be called statically, not by an instance") if ref($class);
  44. confess("Invalid URI passed to constructor") if !is_uri($apiurl);
  45. $user //= $ENV{'TESTRAIL_USER'};
  46. $pass //= $ENV{'TESTRAIL_PASSWORD'};
  47. $debug //= 0;
  48. my $self = {
  49. user => $user,
  50. pass => $pass,
  51. apiurl => $apiurl,
  52. debug => $debug,
  53. testtree => [],
  54. flattree => [],
  55. user_cache => [],
  56. type_cache => [],
  57. tr_fields => undef,
  58. default_request => undef,
  59. browser => new LWP::UserAgent()
  60. };
  61. #Create default request to pass on to LWP::UserAgent
  62. $self->{'default_request'} = new HTTP::Request();
  63. $self->{'default_request'}->authorization_basic($user,$pass);
  64. bless( $self, $class );
  65. return $self if $self->debug; #For easy class testing without mocks
  66. #Manually do the get_users call to check HTTP status
  67. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  68. confess "Error: network unreachable" if !defined($res);
  69. if ( (reftype($res) || 'undef') ne 'ARRAY') {
  70. confess "Unexpected return from _doRequest: $res" if !looks_like_number($res);
  71. confess "Could not communicate with TestRail Server! Check that your URI is correct, and your TestRail installation is functioning correctly." if $res == -500;
  72. confess "Could not list testRail users! Check that your TestRail installation has it's API enabled, and your credentials are correct" if $res == -403;
  73. confess "Bad user credentials!" if $res == -401;
  74. confess "HTTP error $res encountered while communicating with TestRail server. Resolve issue and try again." if !$res;
  75. confess "Unknown error occurred: $res";
  76. }
  77. confess "No users detected on TestRail Install! Check that your API is functioning correctly." if !scalar(@$res);
  78. $self->{'user_cache'} = $res;
  79. return $self;
  80. }
  81. =head1 GETTERS
  82. =head2 B<apiurl>
  83. =head2 B<debug>
  84. Accessors for these parameters you pass into the constructor, in case you forget.
  85. =cut
  86. sub apiurl {
  87. my $self = shift;
  88. confess("Object methods must be called by an instance") unless ref($self);
  89. return $self->{'apiurl'}
  90. }
  91. sub debug {
  92. my $self = shift;
  93. confess("Object methods must be called by an instance") unless ref($self);
  94. return $self->{'debug'};
  95. }
  96. #Convenient JSON-HTTP fetcher
  97. sub _doRequest {
  98. my ($self,$path,$method,$data) = @_;
  99. confess("Object methods must be called by an instance") unless ref($self);
  100. my $req = clone $self->{'default_request'};
  101. $method //= 'GET';
  102. $req->method($method);
  103. $req->url($self->apiurl.'/'.$path);
  104. warn "$method ".$self->apiurl."/$path" if $self->debug;
  105. my $coder = JSON::MaybeXS->new;
  106. #Data sent is JSON
  107. my $content = $data ? $coder->encode($data) : '';
  108. $req->content($content);
  109. $req->header( "Content-Type" => "application/json" );
  110. my $response = $self->{'browser'}->request($req);
  111. return $response if !defined($response); #worst case
  112. if ($response->code == 403) {
  113. cluck "ERROR: Access Denied.";
  114. return -403;
  115. }
  116. if ($response->code != 200) {
  117. cluck "ERROR: Arguments Bad: ".$response->content;
  118. return -int($response->code);
  119. }
  120. try {
  121. return $coder->decode($response->content);
  122. } catch {
  123. if ($response->code == 200 && !$response->content) {
  124. return 1; #This function probably just returns no data
  125. } else {
  126. cluck "ERROR: Malformed JSON returned by API.";
  127. cluck $@;
  128. if (!$self->debug) { #Otherwise we've already printed this, but we need to know if we encounter this
  129. cluck "RAW CONTENT:";
  130. cluck $response->content
  131. }
  132. return 0;
  133. }
  134. }
  135. }
  136. =head1 USER METHODS
  137. =head2 B<getUsers ()>
  138. Get all the user definitions for the provided Test Rail install.
  139. Returns ARRAYREF of user definition HASHREFs.
  140. =cut
  141. sub getUsers {
  142. my $self = shift;
  143. confess("Object methods must be called by an instance") unless ref($self);
  144. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  145. return -500 if !$res || (reftype($res) || 'undef') ne 'ARRAY';
  146. $self->{'user_cache'} = $res;
  147. return $res;
  148. }
  149. =head2 B<getUserByID(id)>
  150. =cut
  151. =head2 B<getUserByName(name)>
  152. =cut
  153. =head2 B<getUserByEmail(email)>
  154. Get user definition hash by ID, Name or Email.
  155. Returns user def HASHREF.
  156. =cut
  157. #I'm just using the cache for the following methods because it's more straightforward and faster past 1 call.
  158. sub getUserByID {
  159. my ($self,$user) = @_;
  160. confess("Object methods must be called by an instance") unless ref($self);
  161. confess("User ID must be integer") unless $self->_checkInteger($user);
  162. $self->getUsers() if !defined($self->{'user_cache'});
  163. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  164. foreach my $usr (@{$self->{'user_cache'}}) {
  165. return $usr if $usr->{'id'} == $user;
  166. }
  167. return 0;
  168. }
  169. sub getUserByName {
  170. my ($self,$user) = @_;
  171. confess("Object methods must be called by an instance") unless ref($self);
  172. confess("User must be string") unless $self->_checkString($user);
  173. $self->getUsers() if !defined($self->{'user_cache'});
  174. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  175. foreach my $usr (@{$self->{'user_cache'}}) {
  176. return $usr if $usr->{'name'} eq $user;
  177. }
  178. return 0;
  179. }
  180. sub getUserByEmail {
  181. my ($self,$email) = @_;
  182. confess("Object methods must be called by an instance") unless ref($self);
  183. confess("Email must be string") unless $self->_checkString($email);
  184. $self->getUsers() if !defined($self->{'user_cache'});
  185. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  186. foreach my $usr (@{$self->{'user_cache'}}) {
  187. return $usr if $usr->{'email'} eq $email;
  188. }
  189. return 0;
  190. }
  191. =head1 PROJECT METHODS
  192. =head2 B<createProject (name, [description,send_announcement])>
  193. Creates new Project (Database of testsuites/tests).
  194. Optionally specify an announcement to go out to the users.
  195. Requires TestRail admin login.
  196. =over 4
  197. =item STRING C<NAME> - Desired name of project.
  198. =item STRING C<DESCRIPTION> (optional) - Description of project. Default value is 'res ipsa loquiter'.
  199. =item BOOLEAN C<SEND ANNOUNCEMENT> (optional) - Whether to confront users with an announcement about your awesome project on next login. Default false.
  200. =back
  201. Returns project definition HASHREF on success, false otherwise.
  202. $tl->createProject('Widgetronic 4000', 'Tests for the whiz-bang new product', true);
  203. =cut
  204. sub createProject {
  205. my ($self,$name,$desc,$announce) = @_;
  206. confess("Object methods must be called by an instance") unless ref($self);
  207. confess("Project name must be string") unless $self->_checkString($name);
  208. $desc //= 'res ipsa loquiter';
  209. $announce //= 0;
  210. confess("Project description must be string") unless $self->_checkString($desc);
  211. confess("Announce must be integer") unless $self->_checkInteger($announce);
  212. my $input = {
  213. name => $name,
  214. announcement => $desc,
  215. show_announcement => $announce
  216. };
  217. my $result = $self->_doRequest('index.php?/api/v2/add_project','POST',$input);
  218. return $result;
  219. }
  220. =head2 B<deleteProject (id)>
  221. Deletes specified project by ID.
  222. Requires TestRail admin login.
  223. =over 4
  224. =item STRING C<NAME> - Desired name of project.
  225. =back
  226. Returns BOOLEAN.
  227. $success = $tl->deleteProject(1);
  228. =cut
  229. sub deleteProject {
  230. my ($self,$proj) = @_;
  231. confess("Object methods must be called by an instance") unless ref($self);
  232. confess("Project ID must be integer") unless $self->_checkInteger($proj);
  233. my $result = $self->_doRequest('index.php?/api/v2/delete_project/'.$proj,'POST');
  234. return $result;
  235. }
  236. =head2 B<getProjects ()>
  237. Get all available projects
  238. Returns array of project definition HASHREFs, false otherwise.
  239. $projects = $tl->getProjects;
  240. =cut
  241. sub getProjects {
  242. my $self = shift;
  243. confess("Object methods must be called by an instance") unless ref($self);
  244. my $result = $self->_doRequest('index.php?/api/v2/get_projects');
  245. #Save state for future use, if needed
  246. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  247. $self->{'testtree'} = $result;
  248. #Note that it's a project for future reference by recursive tree search
  249. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  250. foreach my $pj (@{$result}) {
  251. $pj->{'type'} = 'project';
  252. }
  253. return $result;
  254. }
  255. =head2 B<getProjectByName ($project)>
  256. Gets some project definition hash by it's name
  257. =over 4
  258. =item STRING C<PROJECT> - desired project
  259. =back
  260. Returns desired project def HASHREF, false otherwise.
  261. $project = $tl->getProjectByName('FunProject');
  262. =cut
  263. sub getProjectByName {
  264. my ($self,$project) = @_;
  265. confess("Object methods must be called by an instance") unless ref($self);
  266. confess("Project must be string.") unless $self->_checkString($project);
  267. #See if we already have the project list...
  268. my $projects = $self->{'testtree'};
  269. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  270. $projects = $self->getProjects() unless scalar(@$projects);
  271. #Search project list for project
  272. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  273. for my $candidate (@$projects) {
  274. return $candidate if ($candidate->{'name'} eq $project);
  275. }
  276. return 0;
  277. }
  278. =head2 B<getProjectByID ($project)>
  279. Gets some project definition hash by it's ID
  280. =over 4
  281. =item INTEGER C<PROJECT> - desired project
  282. =back
  283. Returns desired project def HASHREF, false otherwise.
  284. $projects = $tl->getProjectByID(222);
  285. =cut
  286. sub getProjectByID {
  287. my ($self,$project) = @_;
  288. confess("Object methods must be called by an instance") unless ref($self);
  289. confess("No project provided.") unless $project;
  290. confess("Project ID must be integer") unless $self->_checkInteger($project);
  291. #See if we already have the project list...
  292. my $projects = $self->{'testtree'};
  293. $projects = $self->getProjects() unless scalar(@$projects);
  294. #Search project list for project
  295. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  296. for my $candidate (@$projects) {
  297. return $candidate if ($candidate->{'id'} eq $project);
  298. }
  299. return 0;
  300. }
  301. =head1 TESTSUITE METHODS
  302. =head2 B<createTestSuite (project_id, name, [description])>
  303. Creates new TestSuite (folder of tests) in the database of test specifications under given project id having given name and details.
  304. =over 4
  305. =item INTEGER C<PROJECT ID> - ID of project this test suite should be under.
  306. =item STRING C<NAME> - Desired name of test suite.
  307. =item STRING C<DESCRIPTION> (optional) - Description of test suite. Default value is 'res ipsa loquiter'.
  308. =back
  309. Returns TS definition HASHREF on success, false otherwise.
  310. $tl->createTestSuite(1, 'broken tests', 'Tests that should be reviewed');
  311. =cut
  312. sub createTestSuite {
  313. my ($self,$project_id,$name,$details) = @_;
  314. confess("Object methods must be called by an instance") unless ref($self);
  315. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  316. confess("Name must be a string") unless $self->_checkString($name);
  317. $details ||= 'res ipsa loquiter';
  318. confess("Project details must be a string") unless $self->_checkString($details);
  319. my $input = {
  320. name => $name,
  321. description => $details
  322. };
  323. my $result = $self->_doRequest('index.php?/api/v2/add_suite/'.$project_id,'POST',$input);
  324. return $result;
  325. }
  326. =head2 B<deleteTestSuite (suite_id)>
  327. Deletes specified testsuite.
  328. =over 4
  329. =item INTEGER C<SUITE ID> - ID of testsuite to delete.
  330. =back
  331. Returns BOOLEAN.
  332. $tl->deleteTestSuite(1);
  333. =cut
  334. sub deleteTestSuite {
  335. my ($self,$suite_id) = @_;
  336. confess("Object methods must be called by an instance") unless ref($self);
  337. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  338. my $result = $self->_doRequest('index.php?/api/v2/delete_suite/'.$suite_id,'POST');
  339. return $result;
  340. }
  341. =head2 B<getTestSuites (project_id)>
  342. Gets the testsuites for a project
  343. =over 4
  344. =item STRING C<PROJECT ID> - desired project's ID
  345. =back
  346. Returns ARRAYREF of testsuite definition HASHREFs, 0 on error.
  347. $suites = $tl->getTestSuites(123);
  348. =cut
  349. sub getTestSuites {
  350. my ($self,$proj) = @_;
  351. confess("Object methods must be called by an instance") unless ref($self);
  352. confess("Project ID must be integer") unless $self->_checkInteger($proj);
  353. return $self->_doRequest('index.php?/api/v2/get_suites/'.$proj);
  354. }
  355. =head2 B<getTestSuiteByName (project_id,testsuite_name)>
  356. Gets the testsuite that matches the given name inside of given project.
  357. =over 4
  358. =item STRING C<PROJECT ID> - ID of project holding this testsuite
  359. =item STRING C<TESTSUITE NAME> - desired parent testsuite name
  360. =back
  361. Returns desired testsuite definition HASHREF, false otherwise.
  362. $suites = $tl->getTestSuitesByName(321, 'hugSuite');
  363. =cut
  364. sub getTestSuiteByName {
  365. my ($self,$project_id,$testsuite_name) = @_;
  366. confess("Object methods must be called by an instance") unless ref($self);
  367. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  368. confess("Testsuite Name must be String") unless $self->_checkString($testsuite_name);
  369. #TODO cache
  370. my $suites = $self->getTestSuites($project_id);
  371. return -500 if !$suites || (reftype($suites) || 'undef') ne 'ARRAY'; #No suites for project, or no project
  372. foreach my $suite (@$suites) {
  373. return $suite if $suite->{'name'} eq $testsuite_name;
  374. }
  375. return 0; #Couldn't find it
  376. }
  377. =head2 B<getTestSuiteByID (testsuite_id)>
  378. Gets the testsuite with the given ID.
  379. =over 4
  380. =item STRING C<TESTSUITE_ID> - TestSuite ID.
  381. =back
  382. Returns desired testsuite definition HASHREF, false otherwise.
  383. $tests = $tl->getTestSuiteByID(123);
  384. =cut
  385. sub getTestSuiteByID {
  386. my ($self,$testsuite_id) = @_;
  387. confess("Object methods must be called by an instance") unless ref($self);
  388. confess("Testsuite ID must be integer") unless $self->_checkInteger($testsuite_id);
  389. my $result = $self->_doRequest('index.php?/api/v2/get_suite/'.$testsuite_id);
  390. return $result;
  391. }
  392. =head1 SECTION METHODS
  393. =head2 B<createSection(project_id,suite_id,name,[parent_id])>
  394. Creates a section.
  395. =over 4
  396. =item INTEGER C<PROJECT ID> - Parent Project ID.
  397. =item INTEGER C<SUITE ID> - Parent TestSuite ID.
  398. =item STRING C<NAME> - desired section name.
  399. =item INTEGER C<PARENT ID> (optional) - parent section id
  400. =back
  401. Returns new section definition HASHREF, false otherwise.
  402. $section = $tr->createSection(1,1,'nugs',1);
  403. =cut
  404. sub createSection {
  405. my ($self,$project_id,$suite_id,$name,$parent_id) = @_;
  406. confess("Object methods must be called by an instance") unless ref($self);
  407. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  408. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  409. confess("Section name must be string") unless $self->_checkString($name);
  410. confess("Parent section ID must be integer") unless !defined($parent_id) || $self->_checkInteger($parent_id);
  411. my $input = {
  412. name => $name,
  413. suite_id => $suite_id
  414. };
  415. $input->{'parent_id'} = $parent_id if $parent_id;
  416. my $result = $self->_doRequest('index.php?/api/v2/add_section/'.$project_id,'POST',$input);
  417. return $result;
  418. }
  419. =head2 B<deleteSection (section_id)>
  420. Deletes specified section.
  421. =over 4
  422. =item INTEGER C<SECTION ID> - ID of section to delete.
  423. =back
  424. Returns BOOLEAN.
  425. $tr->deleteSection(1);
  426. =cut
  427. sub deleteSection {
  428. my ($self,$section_id) = @_;
  429. confess("Object methods must be called by an instance") unless ref($self);
  430. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  431. my $result = $self->_doRequest('index.php?/api/v2/delete_section/'.$section_id,'POST');
  432. return $result;
  433. }
  434. =head2 B<getSections (project_id,suite_id)>
  435. Gets sections for a given project and suite.
  436. =over 4
  437. =item INTEGER C<PROJECT ID> - ID of parent project.
  438. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  439. =back
  440. Returns ARRAYREF of section definition HASHREFs.
  441. $tr->getSections(1,2);
  442. =cut
  443. sub getSections {
  444. my ($self,$project_id,$suite_id) = @_;
  445. confess("Object methods must be called by an instance") unless ref($self);
  446. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  447. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  448. return $self->_doRequest("index.php?/api/v2/get_sections/$project_id&suite_id=$suite_id");
  449. }
  450. =head2 B<getSectionByID (section_id)>
  451. Gets desired section.
  452. =over 4
  453. =item INTEGER C<PROJECT ID> - ID of parent project.
  454. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  455. =back
  456. Returns section definition HASHREF.
  457. $tr->getSectionByID(344);
  458. =cut
  459. sub getSectionByID {
  460. my ($self,$section_id) = @_;
  461. confess("Object methods must be called by an instance") unless ref($self);
  462. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  463. return $self->_doRequest("index.php?/api/v2/get_section/$section_id");
  464. }
  465. =head2 B<getSectionByName (project_id,suite_id,name)>
  466. Gets desired section.
  467. =over 4
  468. =item INTEGER C<PROJECT ID> - ID of parent project.
  469. =item INTEGER C<SUITE ID> - ID of suite to get section for.
  470. =item STRING C<NAME> - name of section to get
  471. =back
  472. Returns section definition HASHREF.
  473. $tr->getSectionByName(1,2,'nugs');
  474. =cut
  475. sub getSectionByName {
  476. my ($self,$project_id,$suite_id,$section_name) = @_;
  477. confess("Object methods must be called by an instance") unless ref($self);
  478. confess("Project ID must be an integer") unless $self->_checkInteger($project_id);
  479. confess("Suite ID must be an integer") unless $self->_checkInteger($suite_id);
  480. confess("Section Name must be a string") unless $self->_checkString($section_name);
  481. my $sections = $self->getSections($project_id,$suite_id);
  482. return -500 if !$sections || (reftype($sections) || 'undef') ne 'ARRAY';
  483. foreach my $sec (@$sections) {
  484. return $sec if $sec->{'name'} eq $section_name;
  485. }
  486. return 0;
  487. }
  488. =head1 CASE METHODS
  489. =head2 B<getCaseTypes ()>
  490. Gets possible case types.
  491. Returns ARRAYREF of case type definition HASHREFs.
  492. $tr->getCaseTypes();
  493. =cut
  494. sub getCaseTypes {
  495. my $self = shift;
  496. confess("Object methods must be called by an instance") unless ref($self);
  497. my $types = $self->_doRequest("index.php?/api/v2/get_case_types");
  498. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  499. $self->{'type_cache'} = $types if !$self->{'type_cache'}; #We can't change this with API, so assume it is static
  500. return $self->{'type_cache'};
  501. }
  502. =head2 B<getCaseTypeByName (name)>
  503. Gets case type by name.
  504. =over 4
  505. =item STRING C<NAME> - Name of desired case type
  506. =back
  507. Returns case type definition HASHREF.
  508. $tr->getCaseTypeByName();
  509. =cut
  510. sub getCaseTypeByName {
  511. #Useful for marking automated tests, etc
  512. my ($self,$name) = @_;
  513. confess("Object methods must be called by an instance") unless ref($self);
  514. confess("Case type must be string") unless $self->_checkString($name);
  515. my $types = $self->getCaseTypes();
  516. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  517. foreach my $type (@$types) {
  518. return $type if $type->{'name'} eq $name;
  519. }
  520. return 0;
  521. }
  522. =head2 B<createCase(section_id,title,type_id,options,extra_options)>
  523. Creates a test case.
  524. =over 4
  525. =item INTEGER C<SECTION ID> - Parent Project ID.
  526. =item STRING C<TITLE> - Case title.
  527. =item INTEGER C<TYPE_ID> (optional) - desired test type's ID. Defaults to whatever your TR install considers the default type.
  528. =item HASHREF C<OPTIONS> (optional) - Custom fields in the case are the keys, set to the values provided. See TestRail API documentation for more info.
  529. =item HASHREF C<EXTRA OPTIONS> (optional) - contains priority_id, estimate, milestone_id and refs as possible keys. See TestRail API documentation for more info.
  530. =back
  531. Returns new case definition HASHREF, false otherwise.
  532. $custom_opts = {
  533. preconds => "Test harness installed",
  534. steps => "Do the needful",
  535. expected => "cubicle environment transforms into Dali painting"
  536. };
  537. $other_opts = {
  538. priority_id => 4,
  539. milestone_id => 666,
  540. estimate => '2m 45s',
  541. refs => ['TRACE-22','ON-166'] #ARRAYREF of bug IDs.
  542. }
  543. $case = $tr->createCase(1,'Do some stuff',3,$custom_opts,$other_opts);
  544. =cut
  545. sub createCase {
  546. my ($self,$section_id,$title,$type_id,$opts,$extras) = @_;
  547. confess("Object methods must be called by an instance") unless ref($self);
  548. confess("Section ID ($section_id) must be integer") unless $self->_checkInteger($section_id);
  549. confess("title must be string") unless $self->_checkString($title);
  550. confess("Type ID must be integer") unless !defined($type_id) || $self->_checkInteger($type_id);
  551. confess("Options must be HASHREF") unless !defined($opts) || (reftype($opts) || 'undef') ne 'HASH';
  552. confess("Extras must be HASHREF") unless !defined($extras) || (reftype($extras) || 'undef') ne 'HASH';
  553. my $stuff = {
  554. title => $title,
  555. type_id => $type_id
  556. };
  557. #Handle sort of optional but baked in options
  558. if (defined($extras) && reftype($extras) eq 'HASH') {
  559. $stuff->{'priority_id'} = $extras->{'priority_id'} if defined($extras->{'priority_id'});
  560. $stuff->{'estimate'} = $extras->{'estimate'} if defined($extras->{'estimate'});
  561. $stuff->{'milestone_id'} = $extras->{'milestone_id'} if defined($extras->{'milestone_id'});
  562. $stuff->{'refs'} = join(',',@{$extras->{'refs'}}) if defined($extras->{'refs'});
  563. }
  564. #Handle custom fields
  565. if (defined($opts) && reftype($opts) eq 'HASH') {
  566. foreach my $key (keys(%$opts)) {
  567. $stuff->{"custom_$key"} = $opts->{$key};
  568. }
  569. }
  570. my $result = $self->_doRequest("index.php?/api/v2/add_case/$section_id",'POST',$stuff);
  571. return $result;
  572. }
  573. =head2 B<deleteCase (case_id)>
  574. Deletes specified section.
  575. =over 4
  576. =item INTEGER C<CASE ID> - ID of case to delete.
  577. =back
  578. Returns BOOLEAN.
  579. $tr->deleteCase(1324);
  580. =cut
  581. sub deleteCase {
  582. my ($self,$case_id) = @_;
  583. confess("Object methods must be called by an instance") unless ref($self);
  584. confess("Case ID must be integer") unless $self->_checkInteger($case_id);
  585. my $result = $self->_doRequest("index.php?/api/v2/delete_case/$case_id",'POST');
  586. return $result;
  587. }
  588. =head2 B<getCases (project_id,suite_id,section_id)>
  589. Gets cases for provided section.
  590. =over 4
  591. =item INTEGER C<PROJECT ID> - ID of parent project.
  592. =item INTEGER C<SUITE ID> - ID of parent suite.
  593. =item INTEGER C<SECTION ID> - ID of parent section
  594. =back
  595. Returns ARRAYREF of test case definition HASHREFs.
  596. $tr->getCases(1,2,3);
  597. =cut
  598. sub getCases {
  599. my ($self,$project_id,$suite_id,$section_id) = @_;
  600. confess("Object methods must be called by an instance") unless ref($self);
  601. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  602. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  603. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  604. my $url = "index.php?/api/v2/get_cases/$project_id&suite_id=$suite_id";
  605. $url .= "&section_id=$section_id" if $section_id;
  606. return $self->_doRequest($url);
  607. }
  608. =head2 B<getCaseByName (project_id,suite_id,section_id,name)>
  609. Gets case by name.
  610. =over 4
  611. =item INTEGER C<PROJECT ID> - ID of parent project.
  612. =item INTEGER C<SUITE ID> - ID of parent suite.
  613. =item INTEGER C<SECTION ID> - ID of parent section.
  614. =item STRING <NAME> - Name of desired test case.
  615. =back
  616. Returns test case definition HASHREF.
  617. $tr->getCaseByName(1,2,3,'nugs');
  618. =cut
  619. sub getCaseByName {
  620. my ($self,$project_id,$suite_id,$section_id,$name) = @_;
  621. confess("Object methods must be called by an instance") unless ref($self);
  622. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  623. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  624. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  625. confess("Test Case name must be string") unless $self->_checkString($name);
  626. my $cases = $self->getCases($project_id,$suite_id,$section_id);
  627. return -500 if !$cases || (reftype($cases) || 'undef') ne 'ARRAY';
  628. foreach my $case (@$cases) {
  629. return $case if $case->{'title'} eq $name;
  630. }
  631. return 0;
  632. }
  633. =head2 B<getCaseByID (case_id)>
  634. Gets case by ID.
  635. =over 4
  636. =item INTEGER C<CASE ID> - ID of case.
  637. =back
  638. Returns test case definition HASHREF.
  639. $tr->getCaseByID(1345);
  640. =cut
  641. sub getCaseByID {
  642. my ($self,$case_id) = @_;
  643. confess("Object methods must be called by an instance") unless ref($self);
  644. confess("Case ID must be integer") unless $self->_checkInteger($case_id);
  645. return $self->_doRequest("index.php?/api/v2/get_case/$case_id");
  646. }
  647. =head1 RUN METHODS
  648. =head2 B<createRun (project_id,suite_id,name,description,milestone_id,assigned_to_id,case_ids)>
  649. Create a run.
  650. =over 4
  651. =item INTEGER C<PROJECT ID> - ID of parent project.
  652. =item INTEGER C<SUITE ID> - ID of suite to base run on
  653. =item STRING C<NAME> - Name of run
  654. =item STRING C<DESCRIPTION> (optional) - Description of run
  655. =item INTEGER C<MILESTONE ID> (optional) - ID of milestone
  656. =item INTEGER C<ASSIGNED TO ID> (optional) - User to assign the run to
  657. =item ARRAYREF C<CASE IDS> (optional) - Array of case IDs in case you don't want to use the whole testsuite when making the build.
  658. =back
  659. Returns run definition HASHREF.
  660. $tr->createRun(1,1345,'RUN AWAY','SO FAR AWAY',22,3,[3,4,5,6]);
  661. =cut
  662. #If you pass an array of case ids, it implies include_all is false
  663. sub createRun {
  664. my ($self,$project_id,$suite_id,$name,$desc,$milestone_id,$assignedto_id,$case_ids) = @_;
  665. confess("Object methods must be called by an instance") unless ref($self);
  666. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  667. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  668. confess("Name must be string") unless $self->_checkString($name);
  669. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  670. confess("Milestone ID must be integer") unless !defined($milestone_id) || $self->_checkInteger($milestone_id);
  671. confess("Assigned To ID must be integer") unless !defined($assignedto_id) || $self->_checkInteger($assignedto_id);
  672. confess("Case IDs must be ARRAYREF") unless !defined($case_ids) || (reftype($case_ids) || 'undef') eq 'ARRAY';
  673. my $stuff = {
  674. suite_id => $suite_id,
  675. name => $name,
  676. description => $desc,
  677. milestone_id => $milestone_id,
  678. assignedto_id => $assignedto_id,
  679. include_all => defined($case_ids) ? 0 : 1,
  680. case_ids => $case_ids
  681. };
  682. my $result = $self->_doRequest("index.php?/api/v2/add_run/$project_id",'POST',$stuff);
  683. return $result;
  684. }
  685. =head2 B<deleteRun (run_id)>
  686. Deletes specified run.
  687. =over 4
  688. =item INTEGER C<RUN ID> - ID of run to delete.
  689. =back
  690. Returns BOOLEAN.
  691. $tr->deleteRun(1324);
  692. =cut
  693. sub deleteRun {
  694. my ($self,$run_id) = @_;
  695. confess("Object methods must be called by an instance") unless ref($self);
  696. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  697. my $result = $self->_doRequest("index.php?/api/v2/delete_run/$run_id",'POST');
  698. return $result;
  699. }
  700. =head2 B<getRuns (project_id)>
  701. Get all runs for specified project.
  702. =over 4
  703. =item INTEGER C<PROJECT_ID> - ID of parent project
  704. =back
  705. Returns ARRAYREF of run definition HASHREFs.
  706. $allRuns = $tr->getRuns(6969);
  707. =cut
  708. sub getRuns {
  709. my ($self,$project_id) = @_;
  710. confess("Object methods must be called by an instance") unless ref($self);
  711. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  712. return $self->_doRequest("index.php?/api/v2/get_runs/$project_id");
  713. }
  714. =head2 B<getRunByName (project_id,name)>
  715. Gets run by name.
  716. =over 4
  717. =item INTEGER C<PROJECT ID> - ID of parent project.
  718. =item STRING <NAME> - Name of desired run.
  719. =back
  720. Returns run definition HASHREF.
  721. $tr->getRunByName(1,'R2');
  722. =cut
  723. sub getRunByName {
  724. my ($self,$project_id,$name) = @_;
  725. confess("Object methods must be called by an instance") unless ref($self);
  726. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  727. confess("Run name must be string") unless $self->_checkString($name);
  728. my $runs = $self->getRuns($project_id);
  729. return -500 if !$runs || (reftype($runs) || 'undef') ne 'ARRAY';
  730. foreach my $run (@$runs) {
  731. return $run if $run->{'name'} eq $name;
  732. }
  733. return 0;
  734. }
  735. =head2 B<getRunByID (run_id)>
  736. Gets run by ID.
  737. =over 4
  738. =item INTEGER C<RUN ID> - ID of desired run.
  739. =back
  740. Returns run definition HASHREF.
  741. $tr->getRunByID(7779311);
  742. =cut
  743. sub getRunByID {
  744. my ($self,$run_id) = @_;
  745. confess("Object methods must be called by an instance") unless ref($self);
  746. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  747. return $self->_doRequest("index.php?/api/v2/get_run/$run_id");
  748. }
  749. =head1 PLAN METHODS
  750. =head2 B<createPlan (project_id,name,description,milestone_id,entries)>
  751. Create a test plan.
  752. =over 4
  753. =item INTEGER C<PROJECT ID> - ID of parent project.
  754. =item STRING C<NAME> - Name of plan
  755. =item STRING C<DESCRIPTION> (optional) - Description of plan
  756. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  757. =item ARRAYREF C<ENTRIES> (optional) - New Runs to initially populate the plan with -- See TestRail API documentation for more advanced inputs here.
  758. =back
  759. Returns test plan definition HASHREF, or false on failure.
  760. $entries = {
  761. suite_id => 345,
  762. include_all => 1,
  763. assignedto_id => 1
  764. }
  765. $tr->createPlan(1,'Gosplan','Robo-Signed Soviet 5-year plan',22,$entries);
  766. =cut
  767. sub createPlan {
  768. my ($self,$project_id,$name,$desc,$milestone_id,$entries) = @_;
  769. confess("Object methods must be called by an instance") unless ref($self);
  770. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  771. confess("Plan name must be string") unless $self->_checkString($name);
  772. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  773. confess("Milestone ID must be integer") unless !defined($milestone_id) || $self->_checkInteger($milestone_id);
  774. confess("Entries must be ARRAYREF") unless !defined($entries) || (reftype($entries) || 'undef') eq 'ARRAY';
  775. my $stuff = {
  776. name => $name,
  777. description => $desc,
  778. milestone_id => $milestone_id,
  779. entries => $entries
  780. };
  781. my $result = $self->_doRequest("index.php?/api/v2/add_plan/$project_id",'POST',$stuff);
  782. return $result;
  783. }
  784. =head2 B<deletePlan (plan_id)>
  785. Deletes specified plan.
  786. =over 4
  787. =item INTEGER C<PLAN ID> - ID of plan to delete.
  788. =back
  789. Returns BOOLEAN.
  790. $tr->deletePlan(8675309);
  791. =cut
  792. sub deletePlan {
  793. my ($self,$plan_id) = @_;
  794. confess("Object methods must be called by an instance") unless ref($self);
  795. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  796. my $result = $self->_doRequest("index.php?/api/v2/delete_plan/$plan_id",'POST');
  797. return $result;
  798. }
  799. =head2 B<getPlans (project_id)>
  800. Deletes specified plan.
  801. =over 4
  802. =item INTEGER C<PROJECT ID> - ID of parent project.
  803. =back
  804. Returns ARRAYREF of plan definition HASHREFs.
  805. $tr->getPlans(8);
  806. =cut
  807. sub getPlans {
  808. my ($self,$project_id) = @_;
  809. confess("Object methods must be called by an instance") unless ref($self);
  810. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  811. return $self->_doRequest("index.php?/api/v2/get_plans/$project_id");
  812. }
  813. =head2 B<getPlanByName (project_id,name)>
  814. Gets specified plan by name.
  815. =over 4
  816. =item INTEGER C<PROJECT ID> - ID of parent project.
  817. =item STRING C<NAME> - Name of test plan.
  818. =back
  819. Returns plan definition HASHREF.
  820. $tr->getPlanByName(8,'GosPlan');
  821. =cut
  822. sub getPlanByName {
  823. my ($self,$project_id,$name) = @_;
  824. confess("Object methods must be called by an instance") unless ref($self);
  825. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  826. confess("Plan name must be string") unless $self->_checkString($name);
  827. my $plans = $self->getPlans($project_id);
  828. return -500 if !$plans || (reftype($plans) || 'undef') ne 'ARRAY';
  829. foreach my $plan (@$plans) {
  830. return $plan if $plan->{'name'} eq $name;
  831. }
  832. return 0;
  833. }
  834. =head2 B<getPlanByID (plan_id)>
  835. Gets specified plan by ID.
  836. =over 4
  837. =item INTEGER C<PLAN ID> - ID of plan.
  838. =back
  839. Returns plan definition HASHREF.
  840. $tr->getPlanByID(2);
  841. =cut
  842. sub getPlanByID {
  843. my ($self,$plan_id) = @_;
  844. confess("Object methods must be called by an instance") unless ref($self);
  845. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  846. return $self->_doRequest("index.php?/api/v2/get_plan/$plan_id");
  847. }
  848. =head1 MILESTONE METHODS
  849. =head2 B<createMilestone (project_id,name,description,due_on)>
  850. Create a milestone.
  851. =over 4
  852. =item INTEGER C<PROJECT ID> - ID of parent project.
  853. =item STRING C<NAME> - Name of milestone
  854. =item STRING C<DESCRIPTION> (optional) - Description of milestone
  855. =item INTEGER C<DUE_ON> - Date at which milestone should be completed. Unix Timestamp.
  856. =back
  857. Returns milestone definition HASHREF, or false on failure.
  858. $tr->createMilestone(1,'Patriotic victory of world perlism','Accomplish by Robo-Signed Soviet 5-year plan',time()+157788000);
  859. =cut
  860. sub createMilestone {
  861. my ($self,$project_id,$name,$desc,$due_on) = @_;
  862. confess("Object methods must be called by an instance") unless ref($self);
  863. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  864. confess("Name must be string") unless $self->_checkString($name);
  865. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  866. confess("Due on must be unix time stamp (integer)") unless !defined($due_on) || $self->_checkInteger($due_on);
  867. my $stuff = {
  868. name => $name,
  869. description => $desc,
  870. due_on => $due_on # unix timestamp
  871. };
  872. my $result = $self->_doRequest("index.php?/api/v2/add_milestone/$project_id",'POST',$stuff);
  873. return $result;
  874. }
  875. =head2 B<deleteMilestone (milestone_id)>
  876. Deletes specified milestone.
  877. =over 4
  878. =item INTEGER C<MILESTONE ID> - ID of milestone to delete.
  879. =back
  880. Returns BOOLEAN.
  881. $tr->deleteMilestone(86);
  882. =cut
  883. sub deleteMilestone {
  884. my ($self,$milestone_id) = @_;
  885. confess("Object methods must be called by an instance") unless ref($self);
  886. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  887. my $result = $self->_doRequest("index.php?/api/v2/delete_milestone/$milestone_id",'POST');
  888. return $result;
  889. }
  890. =head2 B<getMilestones (project_id)>
  891. Get milestones for some project.
  892. =over 4
  893. =item INTEGER C<PROJECT ID> - ID of parent project.
  894. =back
  895. Returns ARRAYREF of milestone definition HASHREFs.
  896. $tr->getMilestones(8);
  897. =cut
  898. sub getMilestones {
  899. my ($self,$project_id) = @_;
  900. confess("Object methods must be called by an instance") unless ref($self);
  901. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  902. return $self->_doRequest("index.php?/api/v2/get_milestones/$project_id");
  903. }
  904. =head2 B<getMilestoneByName (project_id,name)>
  905. Gets specified milestone by name.
  906. =over 4
  907. =item INTEGER C<PROJECT ID> - ID of parent project.
  908. =item STRING C<NAME> - Name of milestone.
  909. =back
  910. Returns milestone definition HASHREF.
  911. $tr->getMilestoneByName(8,'whee');
  912. =cut
  913. sub getMilestoneByName {
  914. my ($self,$project_id,$name) = @_;
  915. confess("Object methods must be called by an instance") unless ref($self);
  916. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  917. confess("Milestone name must be string") unless $self->_checkString($name);
  918. my $milestones = $self->getMilestones($project_id);
  919. return -500 if !$milestones || (reftype($milestones) || 'undef') ne 'ARRAY';
  920. foreach my $milestone (@$milestones) {
  921. return $milestone if $milestone->{'name'} eq $name;
  922. }
  923. return 0;
  924. }
  925. =head2 B<getMilestoneByID (milestone_id)>
  926. Gets specified milestone by ID.
  927. =over 4
  928. =item INTEGER C<MILESTONE ID> - ID of milestone.
  929. =back
  930. Returns milestone definition HASHREF.
  931. $tr->getMilestoneByID(2);
  932. =cut
  933. sub getMilestoneByID {
  934. my ($self,$milestone_id) = @_;
  935. confess("Object methods must be called by an instance") unless ref($self);
  936. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  937. return $self->_doRequest("index.php?/api/v2/get_milestone/$milestone_id");
  938. }
  939. =head1 TEST METHODS
  940. =head2 B<getTests (run_id)>
  941. Get tests for some run.
  942. =over 4
  943. =item INTEGER C<RUN ID> - ID of parent run.
  944. =back
  945. Returns ARRAYREF of test definition HASHREFs.
  946. $tr->getTests(8);
  947. =cut
  948. sub getTests {
  949. my ($self,$run_id) = @_;
  950. confess("Object methods must be called by an instance") unless ref($self);
  951. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  952. return $self->_doRequest("index.php?/api/v2/get_tests/$run_id");
  953. }
  954. =head2 B<getTestByName (run_id,name)>
  955. Gets specified test by name.
  956. =over 4
  957. =item INTEGER C<RUN ID> - ID of parent run.
  958. =item STRING C<NAME> - Name of milestone.
  959. =back
  960. Returns test definition HASHREF.
  961. $tr->getTestByName(36,'wheeTest');
  962. =cut
  963. sub getTestByName {
  964. my ($self,$run_id,$name) = @_;
  965. confess("Object methods must be called by an instance") unless ref($self);
  966. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  967. confess("Test name must be string") unless $self->_checkString($name);
  968. my $tests = $self->getTests($run_id);
  969. return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
  970. foreach my $test (@$tests) {
  971. return $test if $test->{'title'} eq $name;
  972. }
  973. return 0;
  974. }
  975. =head2 B<getTestByID (test_id)>
  976. Gets specified test by ID.
  977. =over 4
  978. =item INTEGER C<TEST ID> - ID of test.
  979. =back
  980. Returns test definition HASHREF.
  981. $tr->getTestByID(222222);
  982. =cut
  983. sub getTestByID {
  984. my ($self,$test_id) = @_;
  985. confess("Object methods must be called by an instance") unless ref($self);
  986. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  987. return $self->_doRequest("index.php?/api/v2/get_test/$test_id");
  988. }
  989. =head2 B<getTestResultFields()>
  990. Gets custom fields that can be set for tests.
  991. Returns ARRAYREF of result definition HASHREFs.
  992. =cut
  993. sub getTestResultFields {
  994. my $self = shift;
  995. confess("Object methods must be called by an instance") unless ref($self);
  996. return $self->{'tr_fields'} if defined($self->{'tr_fields'}); #cache
  997. $self->{'tr_fields'} = $self->_doRequest('index.php?/api/v2/get_result_fields');
  998. return $self->{'tr_fields'};
  999. }
  1000. =head2 B<getTestResultFieldByName(SYSTEM_NAME,PROJECT_ID)>
  1001. Gets a test result field by it's system name. Optionally filter by project ID.
  1002. =over 4
  1003. =item B<SYSTEM NAME> - STRING: system name of a result field.
  1004. =item B<PROJECT ID> - INTEGER (optional): Filter by whether or not the field is enabled for said project
  1005. =back
  1006. =cut
  1007. sub getTestResultFieldByName {
  1008. my ($self,$system_name,$project_id) = @_;
  1009. confess("Object methods must be called by an instance") unless ref($self);
  1010. confess("System name must be string") unless $self->_checkString($system_name);
  1011. my @candidates = grep {$_->{'name'} eq $system_name} @{$self->getTestResultFields()};
  1012. return 0 if !scalar(@candidates);
  1013. if (defined $project_id) {
  1014. @candidates = grep {
  1015. $_->{'configs'}->[0]->{'context'}->{'is_global'} ||
  1016. ( grep {$_ == $project_id} @{ $_->{'configs'}->[0]->{'context'}->{'project_ids'} } )
  1017. } @candidates;
  1018. }
  1019. return $candidates[0];
  1020. }
  1021. =head2 B<getPossibleTestStatuses()>
  1022. Gets all possible statuses a test can be set to.
  1023. Returns ARRAYREF of status definition HASHREFs.
  1024. =cut
  1025. sub getPossibleTestStatuses {
  1026. my $self = shift;
  1027. confess("Object methods must be called by an instance") unless ref($self);
  1028. return $self->_doRequest('index.php?/api/v2/get_statuses');
  1029. }
  1030. =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
  1031. Creates a result entry for a test.
  1032. =over 4
  1033. =item INTEGER C<TEST_ID> - ID of desired test
  1034. =item INTEGER C<STATUS_ID> - ID of desired test result status
  1035. =item STRING C<COMMENT> (optional) - Any comments about this result
  1036. =item HASHREF C<OPTIONS> (optional) - Various "Baked-In" options that can be set for test results. See TR docs for more information.
  1037. =item HASHREF C<CUSTOM OPTIONS> (optional) - Options to set for custom fields. See buildStepResults for a simple way to post up custom steps.
  1038. =back
  1039. Returns result definition HASHREF.
  1040. $options = {
  1041. elapsed => '30m 22s',
  1042. defects => ['TSR-3','BOOM-44'],
  1043. version => '6969'
  1044. };
  1045. $custom_options = {
  1046. step_results => [
  1047. {
  1048. content => 'Step 1',
  1049. expected => "Bought Groceries",
  1050. actual => "No Dinero!",
  1051. status_id => 2
  1052. },
  1053. {
  1054. content => 'Step 2',
  1055. expected => 'Ate Dinner',
  1056. actual => 'Went Hungry',
  1057. status_id => 2
  1058. }
  1059. ]
  1060. };
  1061. $res = $tr->createTestResults(1,2,'Test failed because it was all like WAAAAAAA when I poked it',$options,$custom_options);
  1062. =cut
  1063. sub createTestResults {
  1064. my ($self,$test_id,$status_id,$comment,$opts,$custom_fields) = @_;
  1065. confess("Object methods must be called by an instance") unless ref($self);
  1066. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  1067. confess("Status ID must be integer") unless $self->_checkInteger($status_id);
  1068. confess("Comment must be string") unless !defined($comment) || $self->_checkString($comment);
  1069. confess("Options must be HASHREF") unless !defined($opts) || (reftype($opts) || 'undef') eq 'HASH';
  1070. confess("Custom Options must be HASHREF") unless !defined($custom_fields) || (reftype($custom_fields) || 'undef') eq 'HASH';
  1071. my $stuff = {
  1072. status_id => $status_id,
  1073. comment => $comment
  1074. };
  1075. #Handle options
  1076. if (defined($opts) && reftype($opts) eq 'HASH') {
  1077. $stuff->{'version'} = defined($opts->{'version'}) ? $opts->{'version'} : undef;
  1078. $stuff->{'elapsed'} = defined($opts->{'elapsed'}) ? $opts->{'elapsed'} : undef;
  1079. $stuff->{'defects'} = defined($opts->{'defects'}) ? join(',',@{$opts->{'defects'}}) : undef;
  1080. $stuff->{'assignedto_id'} = defined($opts->{'assignedto_id'}) ? $opts->{'assignedto_id'} : undef;
  1081. }
  1082. #Handle custom fields
  1083. if (defined($custom_fields) && reftype($custom_fields) eq 'HASH') {
  1084. foreach my $field (keys(%$custom_fields)) {
  1085. $stuff->{"custom_$field"} = $custom_fields->{$field};
  1086. }
  1087. }
  1088. return $self->_doRequest("index.php?/api/v2/add_result/$test_id",'POST',$stuff);
  1089. }
  1090. =head2 B<getTestResults(test_id,limit)>
  1091. Get the recorded results for desired test, limiting output to 'limit' entries.
  1092. =over 4
  1093. =item INTEGER C<TEST_ID> - ID of desired test
  1094. =item POSITIVE INTEGER C<LIMIT> (OPTIONAL) - provide no more than this number of results.
  1095. =item INTEGER C<OFFSET> (OPTIONAL) - Offset to begin viewing result set at.
  1096. =back
  1097. Returns ARRAYREF of result definition HASHREFs.
  1098. =cut
  1099. sub getTestResults {
  1100. my ($self,$test_id,$limit,$offset) = @_;
  1101. confess("Object methods must be called by an instance") unless ref($self);
  1102. confess("Test ID must be positive integer") unless $self->_checkInteger($test_id);
  1103. confess("Result limitation must be positive integer") unless !defined($limit) || ($self->_checkInteger($limit) && $limit > 0);
  1104. confess("Result offset must be integer") unless !defined($offset) || $self->_checkInteger($offset);
  1105. my $url = "index.php?/api/v2/get_results/$test_id";
  1106. $url .= "&limit=$limit" if defined($limit);
  1107. $url .= "&offset=$offset" if defined($offset);
  1108. return $self->_doRequest($url);
  1109. }
  1110. =head1 CONFIGURATION METHODS
  1111. =head2 B<getConfigurations(project_id)>
  1112. Gets the available configurations for a project.
  1113. =over 4
  1114. =item INTEGER C<PROJECT_ID> - ID of relevant project
  1115. =back
  1116. Returns ARRAYREF of configuration definition HASHREFs.
  1117. =cut
  1118. sub getConfigurations {
  1119. my ($self,$project_id) = @_;
  1120. confess("Object methods must be called by an instance") unless ref($self);
  1121. confess("Test ID must be positive integer") unless $self->_checkInteger($project_id);
  1122. my $url = "index.php?/api/v2/get_configs/$project_id";
  1123. return $self->_doRequest($url);
  1124. }
  1125. =head1 STATIC METHODS
  1126. =head2 B<buildStepResults(content,expected,actual,status_id)>
  1127. Convenience method to build the stepResult hashes seen in the custom options for getTestResults.
  1128. =over 4
  1129. =item STRING C<CONTENT> (optional) - The step itself.
  1130. =item STRING C<EXPECTED> (optional) - Expected result of test step.
  1131. =item STRING C<ACTUAL> (optional) - Actual result of test step
  1132. =item INTEGER C<STATUS ID> (optional) - Status ID of result
  1133. =back
  1134. =cut
  1135. #Convenience method for building stepResults
  1136. sub buildStepResults {
  1137. my ($content,$expected,$actual,$status_id) = @_;
  1138. return {
  1139. content => $content,
  1140. expected => $expected,
  1141. actual => $actual,
  1142. status_id => $status_id
  1143. };
  1144. }
  1145. #Type checks
  1146. sub _checkInteger {
  1147. shift;
  1148. my $integer = shift;
  1149. return ( defined $integer && looks_like_number($integer) && int($integer) == $integer );
  1150. }
  1151. sub _checkString {
  1152. shift;
  1153. my $str = shift;
  1154. return ( defined($str) && !ref($str) );
  1155. }
  1156. 1;
  1157. __END__
  1158. =head1 SEE ALSO
  1159. L<HTTP::Request>
  1160. L<LWP::UserAgent>
  1161. L<JSON::MaybeXS>
  1162. L<http://docs.gurock.com/testrail-api2/start>
  1163. =head1 SPECIAL THANKS
  1164. Thanks to cPanel Inc, for graciously funding the creation of this module.