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

How can I extract dropdown list "text" when I want to create report


#1

Dear all,

I have a project that need to customize report, that based on result fields of test run, so I created many dropdown lists.

ex, one of customized dropdown below:
=------------=------------=------------=------------=------------=------------=------------
brand:
1, sony
2, htc
3, apple

…ect,

Then user can select any he wants.
=------------=------------=------------=------------=------------=------------=------------

I created many dropdown lists in result fields, but when I create report,

report can’t extract true value that user selected, only getting “order” of dropdown list,

For example , user selected ‘htc’ of brand from dropdown list in result fields of test run,

after creating report there is showing “2”(because ‘htc’ is no.2 in dropdown list), not showing “htc”,

I guess report take option’s attribute ‘value’ in html,

how can I extract the value I really want?

Thanks a lot,

Sincerely,

Kenji


#2

Hi Kenji,

Thanks for your posting. Which report are you referring to? Is this a custom report? If you are using the API to extract data, you can use the get_result_fields and get_case_fields API methods to convert the IDs to the values to their textual representation. Please note that the numbers for the values are IDs and not the order (the order is determined by the order in the list).

Cheers,
Tobias


#3

Hi, @tgurock

I follow your way that use get_result_fields api method to do,

I got another problem,

I develop my project on my own mac,

but get_result_fields didn’t work, it only work on our company server(linux),

I don’t know why it didn’t on my work and I couldn’t find better way to solve,

because there are not any console existing,

I have no idea how to find any solution to solve,

there is a file name is “TestRail_api.php” in “report/views/index”,

first line in report.php I add “<?php include(dirname(__FILE__)."/views/index/TestRail_api.php") ?>”,

then I write a line “$test = $this->_api->send_command(‘GET’, ‘get_result_fields’);” in the file name is report.php,

after clicking “add and view report” button, the circle just rotate over and over again, [report page (server.IP)/testrail/index.php?/reports/overview, I mean report can’t build success :frowning: ]

maybe something settings or …etc I lost you can reminder of me?

please help me,

thanks a lot!

Kenji


#4

Hi Kenji,

Thanks for your reply. I’m not sure I understand. Can you let me know which report you are generating? Is this a report from the Reports tab?

Regarding the APIs: you would use the APIs outside of TestRail and independently of the reports and this would be option to get the results (get_results_for_run) as well as field meta data.

Cheers,
Tobias


#5

Dear @tgurock,

I create customized report by myself, so I create report from report tab, (I guess, it mean right side of report page?, which can generate customized report.In below image, I create QA report - Testing modified.)

here is my report hierarchy:

In report.php, I include TestRail_api.php by “<?php include(dirname(__FILE__)."/views/index/TestRail_api.php") ?>”

code in report.php:

$test = $this->_api->send_command('GET', 'get_result_fields');

echo "Comment: ".json_encode($test);

Here is code in TestRail_api.php below:

define('TESTRAIL_API_ADDRESS', 'http://server/testrail');
define('TESTRAIL_API_USER', 'user');
define('TESTRAIL_API_PASSWORD', 'pwd');

class TestRail_api
{
    private $_address;
    private $_userpwd;

public function __construct($address, $user, $password)
{		
	$this->_address = rtrim($address, '/') . '/';
	$this->_user = $user;
	$this->_password = $password;
	$this->_userpwd = "$user:$password";
}

private function _send_request($method, $url, $data,
	&$status_code)
{
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

	if ($method == 'POST')
	{
		if ($data)
		{
			curl_setopt(
				$ch,
				CURLOPT_POSTFIELDS, 
				json_encode($data)
			);
		}
		else
		{
			$headers['Content-Length'] = '0';
			curl_setopt($ch, CURLOPT_POSTFIELDS, '');
		}
	}
	else
	{
		curl_setopt($ch, CURLOPT_POST, false);
	}

	curl_setopt(
		$ch,
		CURLOPT_HTTPHEADER,
		array(
			'Expect:',
			'Content-Type: application/json'
		)
	);

	curl_setopt($ch, CURLOPT_USERPWD, "$this->_userpwd");

	$data = curl_exec($ch);
	if ($data === false)
	{
		throw new TestRailException(curl_error($ch));
	}

	$info = curl_getinfo($ch);
	curl_close($ch);

	$status_code = $info['http_code'];
	return $data;
}

public function send_command($method, $uri, $data = null)
{
	$url = $this->_address . "index.php?api/v2/$uri";
	print_to_file($url);

	$response = $this->_send_request($method, $url, $data,
		$status_code);

	

	$obj = null;
	if ($response && is_string($response))
	{
		$obj = json_decode($response);
	}

	if ($status_code != 200)
	{
		throw new TestRailException(
			'HTTP status code: ' . $status_code
		);
	}

	return $obj;
}
}

class TestRailException extends Exception
{
}

Strangely, when I call TestRail_api by

“$test = $this->_api->send_command(‘GET’, ‘get_results_for_run’);” or

“$test = $this->_api->send_command(‘GET’, ‘get_result_fields’);” in report.php,

it can’t work in my localhost(macbook),

but, it can work on our company server(Linux)!!!

Magic! Can you guys help me? thanks a lot!

Cheers,

Kenji


#6

Hi Kenji,

Thanks for your reply. If the API requests work in general but not on a specific server, it might be an issue with the authentication credentials and/or a network issue. You should be able to find out more by adding some logging to your code and looking at the response of the HTTP call. You can add logging to custom code as follows:

http://docs.gurock.com/testrail-faq/system-script-debug
http://docs.gurock.com/testrail-faq/system-debug

In general, it’s usually simpler to use TestRail’s exports or the API to generate custom reports (outside of TestRail) instead of writing and integrating custom reports and that’s what we usually recommend.

Cheers,
Tobias