Registering Existing Agent on New Computer

Hi there.

I run my TestProject Agent locally on my machine but have just got a new computer. Can I install the TestProject Agent software on my new machine but somehow map it to the old ‘Agent’ in my account?

Reason being is that all my existing tests and jobs are setup to run on the agent ‘Aaron’s Macbook Pro’ so registering a brand new agent would require me to go into all my existing tests and jobs and change the agent, which would be a lot of very unnecessary manual work

Hi @aaron3,
You can use the following python script to set all the jobs to use your new agent:

import json
import requests


def main():
    # Inset API key here (replace with any API key)
    api_key = "API key"
    # Insert project_ID
    project_id = "project ID"
    # Insert new agent ID
    new_agent_id = "new agent ID"

    # Shared headers (no change is required)
    headers = {"accept": "application/json",
               "Authorization": api_key,
               "Content-Type": "application/json"}
    # Get all jobs in a project
    url = "https://api.testproject.io/v2/projects/" + project_id + "/jobs?onlyScheduled=false&_start=0"

    # Send request
    projects_jobs = requests.get(url, headers=headers)
    print(projects_jobs)

    # Convert text response to JSON
    json_response = json.loads(projects_jobs.text)

    # Replace all jobs in the project
    for i in json_response:
        job_id = i["id"]
        url = "https://api.testproject.io/v2/projects/" + project_id + "/jobs/" + job_id
        # Set new agent ID
        payload = '{"agentId": "' + new_agent_id + '"}'
        # Send request
        r = requests.put(url, data=payload, headers=headers)
        print(r)

        url = "https://api.testproject.io/v2/projects/" + project_id + "/jobs/" + job_id + "/browsers"
        # Set new browser
        payload = '["Chrome"]'
        # Send request
        r = requests.put(url, data=payload, headers=headers)
        print(r)


main()

Just be sure to set the 3 parameters at the start, and press run.
Note that if you want to automatically add other browsers as well you need to change the JSON payload I have added Chrome in my example.
Let me know if that solved your issue :slight_smile: