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

How to get the test run name in UI script and send it via AJAX request?


#1

I am using the UI script that creates the button and has the functionality of triggering server side script (by AJAX request).

For example, here is the AJAX request:

$.ajax(
{
	url: "trigger.php?run_id=" + uiscripts.context.run.id,
        dataType: "json",
        data: { job : job },
        type: "POST"
       .......
}

Here, I know that the run_id var will have the test run id stored and I can get that by $_GET in PHP. How do I get the test run name? I need this for some processing and triggering the relevant Jenkins job.

It would be great if you can write me an example.

Thank you, Pratik!


#2

Hi Pratik,

The test run name is also part of the UI script context and you should be able to query this via uiscripts.context.run.name (on those page that have support for the UI script context).

Cheers,
Tobias


#3

Awesome, thanks for the answer.


#4

You are welcome :slight_smile:

Cheers,
Tobias


#5

Sorry Tobias, one more thing:

Is url: "trigger.py?run_id=" + uiscripts.context.run.id, doable? See I am trying to call a Python file and not PHP. If not, workarounds?

Pratik


#6

Hi Pratik,

Sure, you can call any script you like and this trigger example is really just an example/POC of how such an integration could look like. We still recommend triggering tests outside of TestRail and this would be more flexible than integrating this directly into TestRail’s UI.

Cheers,
Tobias


#7

Hi Tobias,

I am trying to make an AJAX call and trigger Python file (same as when I do for url: trigger.php). However I am running into the issue:

Failed to load resource: the server responded with a status of 404 (Not Found). This file does exist on server as I have created one.

I have checked that the Python is already installed on the Windows machine to which this request is going. The python file does execute when I try to do it locally on windows TestRail server machine.

Otherwords, following php request works but fails on Python:

WORKS:

$.ajax({
      url: 'trigger.php', //this file exists on Windows TestRail server
      type: "GET", //tried with POST too
      success: function() {...

DOES NOT WORK:

$.ajax({
      url: 'trigpy.py', //this file exists on Windows TestRail server
      type: "GET", //tried with POST too
      success: function() {....

Is something missing? I really want to go ahead with Python and not php.


#8

Hi Pratik,

Please double-check that the file really exists and also check the folder of this file. For the script to work as it looks now, the trigpy.py script needs to be in the same folder as TestRail’s index.php. You may also need to adjust the permissions of the script as well as make sure that your web server can execute Python scripts.

Cheers,
Tobias


#9

Hi Tobias, thanks - I was in the incorrect folder, it worked now. How do we get the URL for TestRun via something like uiscripts.context.run.url ? << does not seem to work?

It would be great if I can get the URL via uiscripts.

Pratik


#10

Hi Pratik,

The URL is not part of the UI script context but the URL is always the same if you have the ID:

index.php?/runs/view/<id>

I hope this helps!

Cheers,
Tobias


#11

Awesome, this works like a charm. Thanks Tobias!


#12

You are welcome, Pratik :slight_smile:

Cheers,
Tobias


#13

Hi Pratik,
Can you share your trigger.py script?


#14

Hi Pratik,
Can you share your trigger.py script?


#15

Hello Tobias,

I’m sorry for my stupidity,
do you mean

$.ajax(
{
	url: "trigger.php?run_id=" + uiscripts.context.run.name,
        dataType: "json",
        data: { job : job },
        type: "POST"
       .......
}

???

I tried that and it does not seem to work.
on the php side I get the following error in httpd/error.log:

[:error] [pid 20105] [client 10.67.69.34:52451] PHP Fatal error: Uncaught exception 'TestRailException' with message 'Run ID not available, exiting immediately' in /var/www/html/trigger.php:47\nStack trace:\n#0 {main}\n thrown in /var/www/html/trigger.php on line 47, referer: https://testrail-test.server/index.php?/runs/view/437&group_by=cases:section_id&group_order=asc

(I inserted error_log( print_r($_REQUEST, TRUE) ); in function execute_test($run_id, $case_id, $test_id) { )

And btw, where/how can I get a full list of what is included in uiscripts.context ?

Thank you in advance.


#16

Nevermind, I found the solution: in trigger.php: foreach ($tests as $test) { $test contains ‘title’


#17

Hi Sergey,

Thanks for the update, and glad you were able to get it working!

Regards,
Marco