Join 34,000+ subscribers and receive articles from our
blog about software quality, testing, QA and security.

Bulding a php script to automate the creation of testcases


#1

Hello guys,
I’m trying to use the php API, to automate the creation of a testcase in Testrail.
Using the API binding for PHP, I have prepared the following php script

<?php require 'testrail.php'; $client = new TestRailAPIClient('http://testrail.int.master.lan/testrail/'); $client->set_user( 'giuseppe.catanese@cg.tasgroup.it'); $client->set_password('XXXX'); $result = $client->send_post( 'add_case/T2S Stocks', array( 'title' => 'New_developed_testcase_for_the_Boss' /*'template_id' => 1*/ /*'type_id' => 1*/ ) ); var_dump($result); ?>

Running it, I get the following complaining:

Fatal error: Uncaught exception ‘TestRailAPIException’ with message ‘Failed to connect to 192.168.27.250 port 1080: Timed out’ in C:\xampp\htdocs\forGiovanni\create_testcase_project\testrail.php:136 Stack trace: #0 C:\xampp\htdocs\forGiovanni\create_testcase_project\testrail.php(87): TestRailAPIClient->_send_request(‘POST’, ‘add_case/T2S St…’, Array) #1 C:\xampp\htdocs\forGiovanni\create_testcase_project\create_testcase.php(15): TestRailAPIClient->send_post(‘add_case/T2S St…’, Array) #2 {main} thrown in C:\xampp\htdocs\forGiovanni\create_testcase_project\testrail.php on line 136

Can you please recommend what could be the issue ? the idea is later to enrich also the created testcase with other info like stepts, etc.

Please let me know.

Thanks,
Giuseppe


#2

Hi Giuseppe,

Thanks for your posting. This looks like a network connection issue. Can you please double-check the URL/address and if the TestRail can be reached from your test machine?

Cheers,
Tobias


#3

Hi Tobias,
thanks so much indeed to help out on this topic.
I have moved the .php in a location on the testrail machine itself, so now there’s no longer a networking issue. The php got executed but I cannot find the testcase anywhere.
This is what I have prepared:

<?php require 'testrail.php'; $client = new TestRailAPIClient('http://testrail.int.master.lan/testrail/'); $client->set_user( 'giuseppe.catanese@cg.tasgroup.it'); $client->set_password('Gc11071985!'); $result = $client->send_post( 'add_case/General', array( 'title' => 'Giulia', 'template_id' => 1, 'priority_id'=>3, 'type_id' => 1, 'estimate'=> 30m, 'milestone_id'=> 2.1 ) ); var_dump($result); ?>

Could it be a syantax mistake on the way I’m using the php api ?
Please kindly do get in touch.

Many thanks,
Giuseppe


#4

Hi Tobias,
I have figure out the issue: the section id was not right.
It’s now fixed. I let you know if get issues while enhancing the implementation.

Many thanks,
Giuseppe


#5

Hi Giuseppe,

Thanks for your feedback. Yes, you would need to use the numeric ID of the section to add a new case and send_post would also throw an exception in this case if it sees a non-200 error code.

Cheers,
Tobias


#6

Hi Tobias,
thanks again. I need your last help.
Can you clarify how to add the custom_separated _steps field using the php API please ?
I have been trying the following, but Testrail doesn’t look happy:

$client->set_password(‘XXXXXXX’);

$result = $client->send_post(
‘add_case/156’,
array(
‘title’ => ‘Testcase di prova’,
‘priority_id’=> 3,
‘estimate’=> ‘30m’,
‘milestone_id’=> 4,
‘type_id’ => 1,
‘Field Case IDs’ =>‘newtestcase’,
‘custom_use_case_id’=>‘new’,
‘custom_banks_testing’=> [13],
‘custom_steps_separated’=>[
{
‘content’ => ‘Step 1’,
‘expected’ => ‘Expected Result 1’
},
{
‘content’ => ‘Step 2’,
‘expected’ => ‘Expected Result 2’
}
]
)
);

var_dump($result);

?>

I have been trying to use the following API syntax:

“custom_steps_separated”: [
{
“content”: “Step 1”,
“expected”: “Expected Result 1”
},
{
“content”: “Step 2”,
“expected”: “Expected Result 2”
},

	..
]

Can you please kindly help me with that ? I’m probably doing something wrong with custom_separated_steps.
I look forward to your feedback.

Many thanks,
Giuseppe


#7

Hi Giuseppe,

I would recommend reviewing the example for adding a case with steps:

http://docs.gurock.com/testrail-api2/reference-cases

Your code looks correct but depending on the TR version, you might also need to submit the template ID (template_id). Otherwise, TR would assign the default template to the new case and this template might not support the separated steps field.

Cheers,
Tobias


#8

Hi Tobias,
I’m very sorry for bugging you.Thanks again for supporting me and the Tas Group so kindly.
I have added the template id, but Testrail is not returning any error with the following version of code:

<?php require 'testrail.php'; $client = new TestRailAPIClient('http://testrail.int.master.lan/testrail/'); $client->set_user( 'giuseppe.catanese@cg.tasgroup.it'); $client->set_password('Gc11071985!'); $result = $client->send_post( 'add_case/156', array( 'title' => 'Nuovo Testcase di prova con gli step per alessandra', 'priority_id'=> 3, 'estimate'=> '30m', 'milestone_id'=> 4, 'type_id' => 2, 'Field Case IDs' =>'newtestcase', 'custom_use_case_id'=>'new', 'custom_banks_testing'=> [13], 'template_id'=> 2, 'custom_steps_separated'=>[ {'content' => 'Step 1', 'expected' => 'Expected Result 1' } ] ) ); var_dump($result); ?>

Can you ask internally the required syntax for the custom_steps_separated field for php please ?The way you describe it here http://docs.gurock.com/testrail-api2/reference-cases
it’s a bit not clear. Do I need to use the => operator or the colon for the syntax ?
Can you please report here a suitable php example ?
Please let us know.

Many thanks,
Giuseppe


#9

In the end I got it to work in this way:

<?php require 'testrail.php'; $client = new TestRailAPIClient('http://testrail.int.master.lan/testrail/'); $client->set_user( 'giuseppe.catanese@cg.tasgroup.it'); $client->set_password('Gc11071985!'); $result = $client->send_post( 'add_case/156', array( 'title' => 'Nuovo Test Ale', 'priority_id'=> 3, 'estimate'=> '30m', 'milestone_id'=> 4, 'type_id' => 2, 'Field Case IDs' =>'newtestcase', 'custom_use_case_id'=>'new', 'custom_banks_testing'=> [13], 'template_id'=> 2, 'custom_steps_separated'=>array('collection:step' => array('index' => 1,'content' => 'ciao', 'expected' => 'Ale'), array('index' => 2,'content' => 'ciao', 'expected' => 'Giulia') ) ) ); var_dump($result); ?>

Thanks a lot,
Giuseppe


#10

Hi Giuseppe,

Thanks for your reply and good to hear it works now! The 'collection:step' is not necessary but using an array of arrays to format the steps looks correct. You should also be able to use an array of objects and this should be serialized to the same JSON string. The example from the documentation uses the raw JSON string so this looks different from PHP’s notation.

Cheers,
Tobias


#11

Hello Tobias,
thanks for you continuous support and sorry to carry on this topic.
Now I’m working on creating automatically a testcase on testrail, gathering information from a html page.
Doing the web scraping of the page, I’m trying to use my php code to popolate the steps of the testcase.
Here what I have prepared so far:

<?php require 'testrail.php'; require('simple_html_dom.php'); $client = new TestRailAPIClient('http://testrail.int.master.lan/testrail/'); $client->set_user( 'giuseppe.catanese@cg.tasgroup.it'); $client->set_password('Gc11071985!'); //$parameter = $_GET["path"]; //$title = $_GET["titolo"]; $html=file_get_html('http://sahi_pro2/sahipro_export/giuseppeale.sah.htm'); // creating an array of steps $steps = array(); foreach($html->find('textarea') as $steprow) { $steps[] = $steprow->plaintext; } $risultato=count($steps); $stepsepa=array(array()); $j=0; $k=1; $i=0; while(($j<=($risultato/2-1))&&($k<=($risultato/2))&&($i<=($risultato-2))) { $stepsepa[$j]= array('collection:step' => array('index'=> $k, 'content'=> $steps[$i], 'expected'=> '')); $j=$j+1; $k=$k+1; $i=$i+2; } $result = $client->send_post( 'add_case/156', array( 'title' => 'TAS testcase', 'priority_id'=> 2, 'estimate'=> '15m', 'milestone_id'=> 4, 'type_id' => 6, 'Field Case IDs' =>'newtestcase', 'custom_use_case_id'=>'new', 'custom_banks_testing'=> [13], 'template_id'=> 2, 'custom_preconds'=> 'Here the link to full html page of the created testcase http://sahi_pro2/sahipro_export/giuseppeale.sah.htm', 'custom_steps_separated'=> $stepsepa ) ); print_r($result); print_r($stepsepa); print_r($steps); ?>

The testcase looks created in testrail but strangely the steps looks empty. I see the three steps created but the content and the expected results are empty.
Do you have any idea of what testrail doesn’t like ?
Here i attach also the html code of the webpage that I’m gathering information from:

Any idea would be very helpful.
Please let me know your thoughts.

Many thanks again,
Giuseppe


#12

html code attacged

<head>
	<title>prova_sahipro4.sah.htm</title>
	<style>
		body{background-color:lightyellow;}
		#maindiv{background-color:lightyellow;}
		div.code{margin-bottom:60px;}
		code {font-family:monospace;font-weight:bold;font-size:16px;padding:0;display:inline;color:black;}
		img {margin-top:15px;margin-bottom:10px;display:inline;height:337;border-style: none}
		td {padding-left:20px;}div.code{xborder-bottom:1px dotted gray;margin-bottom:60px;display:inline;width:370px;}	
		.description{display:none;}
		.codearea{width:378px;height:50px;vertical-align:top;margin-top:0px;font-family:verdana;margin-bottom:20px;}
		.descriptionDiv{width:377px;height:262px;vertical-align:bottom;margin-top:0px;overflow-y:scroll;border-width: 1px 1px 1px; border-style: solid; border-color: rgb(204, 204, 204);}
	</style>
</head>
<body>
	<div id='maindiv'>
		<table><tr><td><a href='prova_sahipro4.sah_images/prova_sahipro4.sah_navigateTo__http___swf-svi-tff-tlm1.dev.int.master.lan_8090_wng-fews___login____0.png' target="_blank"><img height='337' src='prova_sahipro4.sah_images/prova_sahipro4.sah_navigateTo__http___swf-svi-tff-tlm1.dev.int.master.lan_8090_wng-fews___login____0.png' title='prova_sahipro4.sah_images/prova_sahipro4.sah_navigateTo__http___swf-svi-tff-tlm1.dev.int.master.lan_8090_wng-fews___login____0.png'/></a></td><td><textarea class='codearea' disabled>_navigateTo("http://swf-svi-tff-tlm1.dev.int.master.lan:8090/wng-fews/#/login/");</textarea><br/>

Navigate to “http://swf-svi-tff-tlm1.dev.int.master.lan:8090/wng-fews/#/login/” URL


_setValue(_textbox("user"), "TAS");
Enter value "TAS" in "user" textbox

_setValue(_password("password"), "TAS");
Enter value "TAS" in "password" password box

_click(_span("k-input ng-scope"));
Click on "k-input ng-scope" span

_click(_listItem("x (dwp-jboss-env:0)"));
Click on "x (dwp-jboss-env:0)" listItem

_click(_submit("Login"));
Click on "Login" submit button

_click(_div("glyphicon glyphicon-user pull-right"));
Click on "glyphicon glyphicon-user pull-right" div

_click(_link("Log Out"));
Click on "Log Out" link

var textarea = document.getElementsByTagName("textarea"); var codearea = []; var codeareaParent = []; var description = []; for (var i=0; i<textarea.length; i++){ textarea[i].disabled = true; if(textarea[i].className=="description"){ description.push(textarea[i]); } if(textarea[i].className=="codearea"){ codearea.push(textarea[i]); } } for (var i=0; i<codearea.length;i++){ codeareaParent.push(codearea[i].parentNode); } for(var i=0; i<description.length; i++){ var descriptionDiv = document.createElement("div"); descriptionDiv.className = "descriptionDiv"; descriptionDiv.innerHTML = description[i].value; codeareaParent[i].appendChild(descriptionDiv); }
	var del_button = document.getElementsByName('del_i');
	 var totalDelete = [];
	 for (var j=0; j<del_button.length;j++){
		 totalDelete.push(del_button[j]);
	}
	 
	 for (var i=0; i<totalDelete.length; i++){
		 totalDelete[i].parentNode.removeChild(totalDelete[i]);
	 }
	 
</script>

#13

Hi Giuseppe,

The code snippet is a bit difficult to follow, but the expected format for custom_steps_separated using PHP syntax is as follows:

'custom_steps_separated'=> array(
    array('content' => 'Step 1', 'expected' => 'Expected 1'),
    array('content' => 'Step 2', 'expected' => 'Expected 2'),
    ..
);

I would recommend comparing this to the format you use and then adjust your code accordingly. For example, you wouldn’t need the ‘collection:step’ part and you can simply use a regular array of arrays to format the steps.

Cheers,
Tobias


#14

Hi Tobias,
thanks for your attempt to help. I have been trying also the syntax suggested by you as follow:

<?php require 'testrail.php'; require('simple_html_dom.php'); $client = new TestRailAPIClient('http://testrail.int.master.lan/testrail/'); $client->set_user( 'giuseppe.catanese@cg.tasgroup.it'); $client->set_password('Gc11071985!'); $html=file_get_html('http://sahi_pro2/sahipro_export/giuseppeale.sah.htm'); // creating an array of steps $steps = array(); foreach($html->find('textarea') as $steprow) { $steps[] = $steprow->innertext; } $risultato=count($steps); $stepsepa=array(array()); $j=0; $i=1; while(($j<=($risultato/2-1))&&($i<=($risultato-1))) { $stepsepa[$j]= array( array('content' => $steps[$i], 'expected' => 'Insert the expected result here')); $j=$j+1; $i=$i+2; } $result = $client->send_post( 'add_case/156', array( 'title' => 'TAS testcase automatic', 'priority_id'=> 2, 'estimate'=> '15m', 'milestone_id'=> 4, 'type_id' => 6, 'Field Case IDs' =>'newtestcase', 'custom_use_case_id'=>'new', 'custom_banks_testing'=> [13], 'template_id'=> 2, 'custom_preconds'=> 'Here the link to full html page of the created testcase http://sahi_pro2/sahipro_export/giuseppeale.sah.htm', 'custom_steps_separated'=> $stepsepa ) ); print_r($result); var_dump($result); print_r($stepsepa); var_dump($stepsepa); ?>

Strange enough,
this is the output of the print_r of the most important variables.

print_r of $result = Array ( [id] => 4098 [title] => TAS testcase automatic [section_id] => 156 [template_id] => 2 [type_id] => 6 [priority_id] => 2 [milestone_id] => 4 [refs] => [created_by] => 11 [created_on] => 1481651689 [updated_by] => 11 [updated_on] => 1481651689 [estimate] => 15m [estimate_forecast] => [suite_id] => 1 [custom_use_case_id] => new [custom_automation_type] => [custom_incomplete] => [custom_preconds] => Here the link to full html page of the created testcase http://sahi_pro2/sahipro_export/giuseppeale.sah.htm [custom_expected] => [custom_steps_separated] => Array ( [0] => Array ( [content] => [expected] => ) [1] => Array ( [content] => [expected] => ) [2] => Array ( [content] => [expected] => ) ) [custom_note] => [custom_banks_testing] => Array ( [0] => 13 ) ) Array ( [0] => Array ( [0] => Array ( [content] => Navigate to “http://swf-qlt-tlm-dwp.dev.int.master.lan:8090/aquarius/#/login/” URL [expected] => Insert the expected result here ) ) [1] => Array ( [0] => Array ( [content] => Click on “Controller mode: Sahi Java Ruby” div [expected] => Insert the expected result here ) ) [2] => Array ( [0] => Array ( [content] => Navigate to “http://swf-qlt-tlm-dwp.dev.int.master.lan:8090/aquarius/#/login/” URL [expected] => Insert the expected result here ) ) )

print_r of $stepsepa = Array ( [0] => Array ( [0] => Array ( [content] => Navigate to “http://swf-qlt-tlm-dwp.dev.int.master.lan:8090/aquarius/#/login/” URL [expected] => Insert the expected result here ) ) [1] => Array ( [0] => Array ( [content] => Click on “Controller mode: Sahi Java Ruby” div [expected] => Insert the expected result here ) ) [2] => Array ( [0] => Array ( [content] => Navigate to “http://swf-qlt-tlm-dwp.dev.int.master.lan:8090/aquarius/#/login/” URL [expected] => Insert the expected result here ) ) )

Somehow this instruction:

‘custom_steps_separated’=> $stepsepa is messing up things. Any idea of the reason ?

Thanks again,
Giuseppe


#15

Hi Giuseppe,

As far as I can tell, $stepsepa includes too many array levels and it looks like you use three array levels instead of just an array of arrays. For example,

$stepsepa=array(array());

should be:

$stepsepa=array();

and

$stepsepa[$j]= array( array('content' => $steps[$i], 'expected' => 'Insert the expected result here'));

would need to be:

$stepsepa[$j]= array('content' => $steps[$i], 'expected' => 'Insert the expected result here');

I would recommend following my previous example and comparing this to your code/formatted steps.

I hope this helps!

Cheers,
Tobias


#16

Thanks so much Tobias, it does work indeed very well !
Here I’m sharing the final update of the php code:

<?php require 'testrail.php'; require('simple_html_dom.php'); $client = new TestRailAPIClient('http://testrail.int.master.lan/testrail/'); $client->set_user( 'giuseppe.catanese@cg.tasgroup.it'); $client->set_password('Gc11071985!'); //$parameter = $_GET["path"]; //$title = $_GET["titolo"]; $html=file_get_html('http://sahi_pro2/sahipro_export/giuseppeale.sah.htm'); // creating an array of steps $steps = array(); foreach($html->find('textarea') as $steprow) { $steps[] = $steprow->innertext; } // creating an array of images $images = array(); foreach($html->find('img') as $imagerow) { $images[] = $imagerow->src; } $risultato=count($steps); $stepsepa=array(); $j=0; $i=1; while(($j<=($risultato/2-1))&&($i<=($risultato-1))) { $stepsepa[$j]= array('content' => $steps[$i], 'expected' => 'http://sahi_pro2/sahipro_export/'.$images[$j]); $j=$j+1; $i=$i+2; } $result = $client->send_post( 'add_case/156', array( 'title' => 'TAS testcase automatic', 'priority_id'=> 2, 'estimate'=> '15m', 'milestone_id'=> 4, 'type_id' => 6, 'Field Case IDs' =>'newtestcase', 'custom_use_case_id'=>'new', 'custom_banks_testing'=> [13], 'template_id'=> 2, 'custom_preconds'=> 'Here the link to full html page of the created testcase http://sahi_pro2/sahipro_export/giuseppeale.sah.htm', 'custom_steps_separated'=> $stepsepa ) ); //print_r($result); //var_dump($result); //print_r($result); print_r($images); //var_dump($stepsepa); ?>

Thanks again so much for your continuos help and support.
It’s indeed great to get all your help guys.

Warm Regards,
Giuseppe


#17

Hi Giuseppe,

Great to hear it works now, happy to help :slight_smile:

Cheers,
Tobias