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

Examples: accessing the API via UI Scripts with session authentication


#1

Hi guys - on the Admin page for the API integration there is the following information:

Just wondering if you have any examples for accessing the API methods via UI scripts?

Thanks!


#2

Hello Glenn,

Thanks for your posting. We don’t currently have a full example on docs.gurock.com but you can use the following example:

[code]name: Example request for the API
description: Shows how to use TestRail’s API via JavaScript
author: Gurock Software
version: 1.0
includes: ^cases/view
excludes:

js:
(document).ready( function() { .ajax(
{
url: ‘index.php?api/v2/get_case/’ + uiscripts.context.case.id,
dataType: ‘json’,
beforeSend: function(xhr, settings)
{
xhr.setRequestHeader(“Content-Type”, “application/json”);
},
success: function(data, status, xhr)
{
console.log(status);
console.log(data);
},
error: function(xhr, status, error)
{
console.log(error);
}
});
}
);
[/code]

This adds a new UI script to the test case view page that reads the current test case via the API. It uses the UI script context for the current test case ID and then issues a get_case API request to read the case from the API:

http://docs.gurock.com/testrail-custom/uiscripts-introduction
http://docs.gurock.com/testrail-api2/reference-cases#get_case

I hope this helps!

Regards,
Tobias


[UI Script] Make Estimate field required
#3

Thanks Tobias! That’s perfect as a starting point :slight_smile:


#4

Definitely a +1!

Thanks!


#5

Do you have an example of how to access the API with a POST command such as add_case or update_case?