如何利用Selenium IDE进行API测试。即,将请求推送到服务器并获取响应。
这可以使用 Javascript - XMLHttpRequest 方法来完成。
创建请求,设置标头和其他参数,然后使用正文命中服务器。
命令:
Execute Script
目标:
var req = new XMLHttpRequest();
var url = '<URL of PAI to test>';
var method = Method to test (GET/POST/PUT/DELETE)>';
req.setRequestHeader('Content-type', 'application/json');
req.open(method, url, false);
req.send(<{REQUEST BODY}>);
价值:
<Any variable name to store response>
这是使用 HTTPClient 库的 Get 调用请求。
package com.qa.tests;
import java.io.IOException;
import java.util.HashMap;
import org.apache.http.Header;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.qa.base.TestBase;
import com.qa.client.RestClient;
import com.qa.util.TestUtil;
public class GetAPITest extends TestBase {
TestBase testBase;
String mainURL;
String apiURL;
String URL;
RestClient restClient;
CloseableHttpResponse closeableHttpResponse;
@BeforeMethod
public void setUp() {
testBase = new TestBase();
mainURL = prop.getProperty("URL");
apiURL = prop.getProperty("serviceURL");
URL = mainURL + apiURL;
}
@Test(priority = 1)
public void getAPITestWithoutHeaders() throws ClientProtocolException, IOException {
restClient = new RestClient();
closeableHttpResponse = restClient.get(URL);
// 1. Status Code
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
System.out.println("Status Code: " + statusCode);
Assert.assertEquals(statusCode, RESPONSE_CODE_200, "Status code is not 200");
// 2. Json String
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
JSONObject responseJson = new JSONObject(responseString);
System.out.println("Response Jason: " + responseJson);
// single value assertion:
// per_page:
String perpageValue = TestUtil.getValueByJPath(responseJson, "/per_page");
System.out.println("Per Page value is: " + perpageValue);
Assert.assertEquals(Integer.parseInt(perpageValue), 6);
// total:
String totalValue = TestUtil.getValueByJPath(responseJson, "/total");
System.out.println("Total value is: " + totalValue);
Assert.assertEquals(Integer.parseInt(totalValue), 12);
// Get value from JSON Array:
String firstName = TestUtil.getValueByJPath(responseJson, "/data[0]/first_name");
String lastName = TestUtil.getValueByJPath(responseJson, "/data[0]/last_name");
String id = TestUtil.getValueByJPath(responseJson, "/data[0]/id");
String email = TestUtil.getValueByJPath(responseJson, "/data[0]/email");
System.out.println(firstName);
System.out.println(lastName);
System.out.println(id);
System.out.println(email);
// 3. All headers
Header[] headersArray = closeableHttpResponse.getAllHeaders();
HashMap<String, String> allHeaders = new HashMap<String, String>();
for (Header header : headersArray) {
allHeaders.put(header.getName(), header.getValue());
}
System.out.println("Headers Array: " + allHeaders);
}
@Test(priority = 2)
public void getAPITestWithHeaders() throws ClientProtocolException, IOException {
restClient = new RestClient();
HashMap<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Content-Type", "application/json");
closeableHttpResponse = restClient.get(URL, headerMap);
// 1. Status Code
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
System.out.println("Status Code: " + statusCode);
Assert.assertEquals(statusCode, RESPONSE_CODE_200, "Status code is not 200");
// 2. Json String
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
JSONObject responseJson = new JSONObject(responseString);
System.out.println("Response Jason: " + responseJson);
// single value assertion:
// per_page:
String perpageValue = TestUtil.getValueByJPath(responseJson, "/per_page");
System.out.println("Per Page value is: " + perpageValue);
Assert.assertEquals(Integer.parseInt(perpageValue), 6);
// total:
String totalValue = TestUtil.getValueByJPath(responseJson, "/total");
System.out.println("Total value is: " + totalValue);
Assert.assertEquals(Integer.parseInt(totalValue), 12);
// Get value from JSON Array:
String firstName = TestUtil.getValueByJPath(responseJson, "/data[0]/first_name");
String lastName = TestUtil.getValueByJPath(responseJson, "/data[0]/last_name");
String id = TestUtil.getValueByJPath(responseJson, "/data[0]/id");
String email = TestUtil.getValueByJPath(responseJson, "/data[0]/email");
System.out.println(firstName);
System.out.println(lastName);
System.out.println(id);
System.out.println(email);
// 3. All headers
Header[] headersArray = closeableHttpResponse.getAllHeaders();
HashMap<String, String> allHeaders = new HashMap<String, String>();
for (Header header : headersArray) {
allHeaders.put(header.getName(), header.getValue());
}
System.out.println("Headers Array: " + allHeaders);
}
}