我正在使用Selenium和测试ng来验证url是否返回200(成功)或失败(501)错误。但是我得到空指针异常。请帮我解决这个问题。
以下是代码
public void Test_Demo_Services() throws Exception {
System.out.println("Entering Login test method");
//Logging in
this.oService = new WebService(this.oUrl.getSMHeartBeatUrl());
Boolean bbb=this.oService.checkWebService(this.oUrl.getSMHeartBeatUrl());
System.out.println("Exiting Login test method");
}
Webservices.java
public boolean checkWebService(String sUrl) throws Exception {
// Create Gson object (serialize and deserialize json strings)
Gson gson = new GsonBuilder().create();
UrlBuilder oUrl = new UrlBuilder(sUrl); //Base URL..no port or anything needed
//String sServiceUrl = oUrl.getWFMSVCActivitiesUrl();
String sServiceUrl = oUrl.getSMHeartBeatUrl();
//Create your http client
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
//Create http get object
HttpGet getRequest = new HttpGet(sServiceUrl);
//getRequest.addHeader("WMT-UserId",this.getUserID());
getRequest.addHeader("WMT-Security-ID", this.sAuthToken[0]);
getRequest.addHeader("WMT-Token", this.sAuthToken[1]);
//submit request and save response
HttpResponse response = httpclient.execute(getRequest);
//get status code from response
int sStatusCode = response.getStatusLine().getStatusCode();
if(sStatusCode == 200 || sStatusCode == 201) {
ReportResults("PASS", "getActivitiesAll webservice executed successfully", false);
return true;
}
else{
ReportResults("FAIL", "getActivitiesAll webservice unsuccessful due to: " + this.getErrorMessage() , false);
this.setErrorMessage(response.getStatusLine().toString());
return false;
}