This page explains in detail how to execute Rest API's exposed by ExperitestContinuous Testing.
Demonstration about how to get application information and upload an application to Cloud is described below.
SEO Metadata | ||
---|---|---|
| ||
This page explains in detail how to execute Rest API's exposed by Continuous Testing. Demonstration about how to get application information and upload an application to Cloud is described below. |
Table of Contents | ||
---|---|---|
|
Rest API Authorization
Info |
---|
These are important keys for the examples described below.:
|
...
|
Cloud Server requires authentication/authorization to perform Rest Operations. The Authentication can be done using two ways as stated below,:
Using Bearer token - Access Key (See YOUR_ACCESS_KEY above)
Postman
Using username and password
Postman
Note: Authenticated An authenticated user will take the a specific role and perform the operation. Typically the role is assigned at the time when a user is created.GET Application Information
Example - Get Application Information (GET)
Get Applications Rest API allows Project Admin to get applications and is defined as GET as GET/apiAPI/v1/applications
The GET requests for Applications supports filtering mechanism. Typically, filtering is used to search based on some meaningful criteria.
...
For example in the following GET request,
GET /apiAPI/v1/applications?osType=android, "osType" is filter filters name and "android" is the value. This request will return all android applications.
Using CURL
CURL command is commonly used to transfer data across different underlying protocols. It is also popularly used to perform Rest operations.
...
In the above command ?osType=android&packageName=com.experitest.ExperiBank is the filter syntax. osType and packageName are filters and are used to filter the applications.
Using POSTMAN
...
Postman is an application which makes API development faster and easy and is installed in Chome browser.
Screenshot below shows how to execute Get Applications using a filter.
...
Using JAVA
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
dependencies {
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9'
} |
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.http.HttpHeaders;
import org.testng.annotations.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
public class RestProjectAPI {
private HttpResponse<String> responseString;
private HttpResponse<InputStream> responseInputStream;
private String urlBase = "http://hostname:port/"; //TODO: modify hostname and port of your Reporter
private String user = "user"; //TODO: user name
private String password = "....."; //TODO: user password
private String accessKey= "....."; //TODO: user access key
String projectName = "projectName";//TODO: project name is here
String projectID = "projectID";//TODO: project ID is here
@Test
public void getApplications() {
String url = urlBase + "/api/v1/applications?osType=android&packageName=com.experitest.ExperiBank";
try {
responseString = Unirest.get(url)
.basicAuth(user, password)
.header("content-type", "application/json")
.asString();
System.out.println(responseString.getBody());
} catch (Exception e) {
e.printStackTrace();
}
}
|
Example - Uploading an application (POST)
This Rest API uploads an application and gets the Id of the application.
This is a POST request and defined as POST /apiAPI/v1/applications/new
Using CURL
Since this API is post posted request it can contain a parameter which can be sent as headers as well as in body. Typically in CURL commands, header parameters are sent with -H parameter and body parameters are sent with -F parameter.
Code Block | ||||
---|---|---|---|---|
| ||||
>> curl -v -i -X POST -k -H "Authorization: Bearer YOUR_ACCESS_KEY" -H -F "uniqueName=UICatalog" -F file=@C:\com.experitest.uicatalog_.MainActivity_ver_3.1710.apk CLOUD_SERVER/api/v1/applications/new HTTP/1.1 200 {"status":"SUCCESS","data":{"created":"true","id":"5754383"},"code":"OK"}* |
Note:
- In the above command "Authorization" is passed as a header because its send with -H option and "uniqueName" and "file" are sent as a body since it is sent as -F.
- The file parameter value i.e file=@ has a character '@', this indicate indicates that the file is uploaded as form based.
Using POSTMAN
...
Screenshot The screenshot below shows the usage of the Rest API
Notice "file" and "uniquename" are sent as the body.
Using JAVA
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
dependencies { compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1. |
...
In the screenshot below the header Authorization is specified.
...
4.9'
} |
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.http.HttpHeaders;
import org.testng.annotations.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
public class RestProjectAPI {
private HttpResponse<String> responseString;
private HttpResponse<InputStream> responseInputStream;
private String urlBase = "http://hostname:port/"; //TODO: modify hostname and port of your Reporter
private String user = "user"; //TODO: user name
private String password = "....."; //TODO: user password
private String accessKey= "....."; //TODO: user access key
String projectName = "projectName";//TODO: project name is here
String projectID = "projectID";//TODO: project ID is here
@Test
public void uploadApp() {
String url = urlBase + "/api/v1/applications/new";
try {
Unirest.post(url)
.basicAuth(user, password)
.field("file", new File("C:\\Users\\TestUser\\Downloads\\com.experitest.uicatalog_.MainActivity_ver_3.1710.apk"))
.asBinary();
} catch (Exception e) {
e.printStackTrace();
}
}
} |