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

Access reports after run_report/ api

I was following the docs in API Reports. After I generated a report thru run_report I was given a json response with 3 links:

{
"report_url": "https://docs.testrail.com/index.php?/reports/view/383",
"report_html": "https://docs.testrail.com/index.php?/reports/get_html/383",
"report_pdf": "https://docs.testrail.com/index.php?/reports/get_pdf/383"

}

How do I access them programmatically and download pdf for example? When I try to fetch any of them, it doesn’t work. It works only when I copy the link from the response and manually paste into the browser.

1 Like

i vote for this topic :+1:
i’m looking for the exact same functionality

So i got feedback today from gurock. Apparently the download really doesn’t work programmatically :man_facepalming:

So I helped myself with a workaround using python + selenium. I do not take responsibility nor do I provide support, but this is how I download the generated report now:

import requests
import base64
import pprint
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
import os.path
import sys
import time

username = 'username'
password = 'password'
download_path = os.path.dirname(sys.argv[0])

auth = str(
    base64.b64encode(
        bytes('%s:%s' % (username, password), 'utf-8')
    ),
    'ascii'
).strip()

headers = {'Authorization': 'Basic ' + auth}
headers['Content-Type'] = 'application/json'

# here also send_get from testrail api is possible
report = requests.get('https://example.testrail.com/index.php?/api/v2/run_report/2', headers=headers)


chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : download_path}
chromeOptions.add_experimental_option("prefs",prefs)
chromeOptions.add_argument('headless');

# create a new Chrome session
driver = webdriver.Chrome(options=chromeOptions)

download_url = report.json()['report_pdf']
download_file = download_path + '\\testrail-report-' + download_url.rsplit('/', 1)[-1] + '.pdf'

# Navigate to the application home page
r = driver.get(download_url)

WebDriverWait(driver, 5).until(
    EC.presence_of_element_located((By.ID, 'name'))
)
eUsername = driver.find_element_by_id("name")
ePassword = driver.find_element_by_id("password")

eUsername.send_keys(username)
ePassword.send_keys(password)

driver.find_element_by_id("button_primary").click()

count = 1
# wait maximum 30 seconds for download to complete
max_wait = 30
while count < max_wait:
    count = count + 1
    if os.path.exists(download_file):
        count = max_wait
    else:
        time.sleep(1)

driver.quit()

Maybe you can use this as well.

Best regards!