How to send API requests to AppVerify using Jenkins?

This article will walk you through the format and syntax used when making AppVerify API requests using Jenkins. In this article, we will create a pipeline (Jenkins job) for each request using Groovy code and then build the pipelines.

Note: You can use any other methods to configure your Jenkins jobs and run them. 

AppVerify API requests using Jenkins

Generate Token

Go to Jenkins Dashboard and click "New Item".

Enter a name for the item, select "Pipeline" and click "OK". i.e. Generate Token

Put a description for the pipeline and put the script below in the Script section of the Pipeline.

Click "Apply" and "Save".

This authenticates the user with the Manager, and obtain a token.

Script:

def post = new URL("http://IP Address:port number/api/generatetoken").openConnection();
def message = "username=[username]&password=[password]"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Example:

def post = new URL("http://172.169.12.41:8888/api/generatetoken").openConnection();
def message = "username=admin&password=admin"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Response:

[Token Value] i.e. 6d83469eea9ffc77431c0013aeb944170a1699513f39

Generate a Test Run 

To create a new pipeline for the next request, go back to Jenkins Dashboard and click "New Item".

Enter a name for the item, select "Pipeline" and click "OK". i.e. Generate Test Run

Put a description for the pipeline and put the script below in the Script section of the Pipeline.

Click "Apply" and "Save".

This takes the Test plan ID as input and generates a Test run based on an existing Test plan. 

Script:

def post = new URL("http://IP Address:port number/api/createtpr").openConnection();
def message = "tp_id=[Test Plan ID]"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorization", "Token [Token Value]")
post.setRequestProperty("Accept", "application/xml")
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Example:

def post = new URL("http://172.169.12.41:8888/api/createtpr").openConnection();
def message = "tp_id=ATP-47086"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorization", "Token 6d83469eea9ffc77431c0013aeb944170a1699513f39")
post.setRequestProperty("Accept", "application/xml")
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Response:

"Test run was created successfully."

A Test run will be generated for the Test plan (i.e. ATP-47086). 

Tip:Tip: You can find the desired Test plan ID next to the Test plan name in the "Test Plans" section.

Play a Test run

To create a new pipeline for the next request, go back to Jenkins Dashboard and click "New Item".

Enter a name for the item, select "Pipeline" and click "OK". i.e. Play Test Run

Put a description for the pipeline and put the script below in the Script section of the Pipeline.

Click "Apply" and "Save".

This takes the Test plan ID and Test run number as inputs. Then it finds the available rTesters and have them start playing the processes.

Script:

def post = new URL("http://IP Address:port number/api/playtpr").openConnection();
def message = "tp_id=[Test Plan ID]&tpr_id=[Test Plan Run #]"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorization", "Token [Token Value]")
post.setRequestProperty("Accept", "application/xml")
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Example:

def post = new URL("http://172.169.12.41:8888/api/playtpr").openConnection();
def message = "tp_id=ATP-47086&tpr_id=2"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorization", "Token 6d83469eea9ffc77431c0013aeb944170a1699513f39")
post.setRequestProperty("Accept", "application/xml")
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Response:

"Test run was started to play."

The Test run (i.e. #2) of Test plan (i.e. ATP-47086) will start playing.

Generate and play a Test run 

To create a new pipeline for the next request, go back to Jenkins Dashboard and click "New Item".

Enter a name for the item, select "Pipeline" and click "OK". i.e. Generate and Play Test Run

Put a description for the pipeline and put the script below in the Script section of the Pipeline.

Click "Apply" and "Save".

This takes the Test plan ID as input and generates a Test run based on an existing Test plan. Then, it finds the available rTesters and have them start playing the processes.

Script:

def post = new URL("http://IP Address:port number/api/createandplaytpr").openConnection();
def message = "tp_id=[Test Plan ID]"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorization", "Token [Token Value]")
post.setRequestProperty("Accept", "application/xml")
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Example:

def post = new URL("http://172.169.12.41:8888/api/createandplaytpr").openConnection();
def message = "tp_id=ATP-65621"
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorization", "Token 6d83469eea9ffc77431c0013aeb944170a1699513f39")
post.setRequestProperty("Accept", "application/xml")
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}

Response:

"Test run was created and started to play."

The Test run of Test plan (i.e. ATP-65621will start playing. 

When you create your pipelines, there will be four pipelines listed in your Jenkins view. Now you can start running your jobs by building your first pipeline (i.e. Generate Token ) and use the output to run the next pipelines.