Java 使用selenium和JSON头实现API后自动化
我想使用selenium(java)自动化RESTAPI,有可能吗?如果它在Java中有json格式的头和体部分,您可以使用ApacheHttpClient,例如 例如,ApacheHttpClientPost中的方法可能如下所示:Java 使用selenium和JSON头实现API后自动化,java,rest,selenium,Java,Rest,Selenium,我想使用selenium(java)自动化RESTAPI,有可能吗?如果它在Java中有json格式的头和体部分,您可以使用ApacheHttpClient,例如 例如,ApacheHttpClientPost中的方法可能如下所示: public static String post(String tokenMobile, String method, String version, String body) throws Exception{ try { HttpCl
public static String post(String tokenMobile, String method, String version, String body) throws Exception{
try {
HttpClient httpClient = HttpClientBuilder.create().build();
URIBuilder builder = new URIBuilder();
builder.setScheme("https").setHost(host).setPath(method)
.setParameter("", ""); //Params
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
HttpPost postRequest = new HttpPost(httpget.getURI()); //Header
postRequest.addHeader("Content-Type", "application/json");
postRequest.addHeader("version", version);
postRequest.addHeader("Authorization", "Bearer "+tokenMobile);
StringEntity input = new StringEntity(body); //Body in json
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
StringBuilder stringBuilder = new StringBuilder();
outputs = stringBuilder.append(output).toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return outputs;
}
Selenium是为UI或e2e测试用例的自动化而设计的工具。您可以将Selenium测试用例与API测试用例集成,但这始终不是一个好主意
如果您想自动化API测试用例,请尝试类似于
Rest Assured,Postman,HTTPClient
的方法。您的意思是从带有fetch的浏览器?这似乎是个坏主意。为什么不定期进行http请求呢?您不需要将selenium植入自动REST API。谢谢,我会试试的