Genetec / azure-devops-extension-publishtestresultscreenshot

Azure DevOps extension used in CI pipeline to upload screenshot in test results report.
https://marketplace.visualstudio.com/items?itemName=Genetec.publish-test-result-screenshot
Apache License 2.0
10 stars 6 forks source link

Allow multiple test outcomes to have screenshots attached #2

Closed aaron-goff closed 2 months ago

aaron-goff commented 4 years ago

Is your feature request related to a problem? Please describe. Allowing users to specify what outcomes they would like screenshots attached for would be terrific. For example, if users are generating screenshots on success, or even ad hoc during test execution, it would be great to be able to add those as attachments.

Describe the solution you'd like Add an input for outcome type

Describe alternatives you've considered Grabbing all test outcomes and iterating through them until there is a match between the screenshot and the test

Additional context I implemented something similar using a modified version of the python script in the original article. I'll add that below, with additional comments. I'd be more than happy to work on implementing this solution, but I'd need some time to make sure I wasn't breaking anything, as I haven't worked on creating custom devops extensions before.

# Get runIds for current build 
# (we execute our tests against multiple flavors, so multiple runs in a single build)
api = f'{baseUrl}/test/runs?buildUri={buildUri}&api-version=5.1'
response = requests.get(api, headers=header)
print("GET", api, "-->", response.status_code)
json_response = json.loads(response.text)['value']
runId = []
for test_result in json_response:
    if "UI Tests" in test_result['name']:
        runId.append(test_result['id'])
# Get results for current build, as an array of [runId, urlWithoutOutcome]
getResultsApi = []
for run in runId:
    getResultsApi.append([run, f'{baseUrl}/test/runs/{run}/results?api-version=5.1&outcomes='])

# Re-usable function to upload image attachment
def uploadAttachment(imgPath, resultId, testName, runId):
    # Only execute if we can find the image
    if os.path.isfile(imgPath):
        print("Screenshot exists, will upload: ", imgPath)
        with open(imgPath, "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read())
            api = f'{baseUrl}/test/runs/{runId}/results/{resultId}/attachments?api-version=5.1-preview.1'
            data = {'stream': encoded_string.decode('utf-8'), 'fileName': f'{testName}.png',
                    'comment': 'Uploaded by REST from pipeline', 'attachmentType': 'GeneralAttachment'}
            response = requests.post(api, headers=header, json=data)
            print("POST", api, "-->", response.status_code)
    else:
        print("Screenshot does not exist, will not upload")
        print("Image Path is: ", imgPath)

for result in getResultsApi:
    # Get failed tests and upload screenshot
    apiFailures = f'{result[1]}3'
    responseFailures = requests.get(apiFailures, headers=header)
    print("GET", apiFailures, "-->", responseFailures.status_code)
    failures = json.loads(responseFailures.text)['value']
    print("failures is:\n", str(failures))
    for failedTest in failures:
        resultId = failedTest['id']
        className = failedTest['automatedTestStorage']
        testName = failedTest['testCaseTitle']
        # Custom image path
        imgPath = f"{screenshotsDir}/failures/{className}/{testName}.png"
        print('imgpath = ' + imgPath)
        uploadAttachment(imgPath, resultId, testName, result[0])

    # Get successful tests and upload screenshots
    apiSuccesses = f'{result[1]}2'
    responseSuccesses = requests.get(apiSuccesses, headers=header)
    print("GET", apiSuccesses, "-->", responseSuccesses.status_code)
    successes = json.loads(responseSuccesses.text)['value']
    print("successes is:\n", str(successes))
    for successfulTest in successes:
        resultId = successfulTest['id']
        className = successfulTest['automatedTestStorage']
        testName = successfulTest['testCaseTitle']
        # Custom image path
        imgPath = f"{screenshotsDir}/successes/{className}/{testName}.png"
        uploadAttachment(imgPath, resultId, testName, result[0])

(as an aside, i'm looking for a solution to add my ad hoc screenshots to the script)

kjgen commented 2 months ago

Closing issue because the extension is being deprecated and will no longer be maintained. An alternative solution is to use the Publish Test Result task which now supports file attachments.