How to use downloadFile method of com.testsigma.automator.webservices.WebserviceUtil class

Best Testsigma code snippet using com.testsigma.automator.webservices.WebserviceUtil.downloadFile

Source:WebserviceUtil.java Github

copy

Full Screen

...184 removeContentTypeHeader(headers);185 boolean isFileUrl = data.getValue() != null && (data.getValue().toString().startsWith("http")186 || data.getValue().toString().startsWith("https"));187 if (isFileUrl) {188 String filePath = downloadFile(data.getValue().toString(), envSettings);189 String[] fileNames = filePath.split(File.separator);190 builder.addBinaryBody(data.getKey(), new File(filePath), ContentType.DEFAULT_BINARY, fileNames[fileNames.length - 1]);191 } else {192 builder.addPart(data.getKey(), new StringBody(new ObjectMapperService().convertToJson(data.getValue()), ContentType.APPLICATION_JSON));193 }194 }195 return builder.build();196 } else if (entity.getPayload() != null) {197 return new StringEntity(entity.getPayload());198 }199 return null;200 }201 public void removeContentTypeHeader(Map<String, String> headers) {202 String contenttype = null;203 for (Map.Entry<String, String> entry : headers.entrySet()) {204 if (entry.getKey().equalsIgnoreCase("content-type")) {205 contenttype = entry.getKey();206 break;207 }208 }209 if (contenttype != null) {210 headers.remove(contenttype);211 }212 }213 public HttpResponse<String> executeRestCall(String url, RequestMethod method, Map<String, String> headers,214 HttpEntity input) throws IOException {215 HttpResponse<String> returnResponse;216 log.debug(String.format("Executing Rest API call with below props\n method::%s\n, headers::%s\n HttpEntity::%s", method,217 headers, input));218 try {219 HttpUriRequest request = null;220 switch (method) {221 case GET:222 request = new HttpGet(url);223 setHeaders(request, headers);224 break;225 case POST:226 request = new HttpPost(url);227 if (input != null)228 ((HttpPost) request).setEntity(input);229 setHeaders(request, headers);230 break;231 case PUT:232 request = new HttpPut(url);233 if (input != null)234 ((HttpPut) request).setEntity(input);235 setHeaders(request, headers);236 break;237 case PATCH:238 request = new HttpPatch(url);239 if (input != null)240 ((HttpPatch) request).setEntity(input);241 setHeaders(request, headers);242 break;243 case DELETE:244 request = new HttpDelete(url);245 setHeaders(request, headers);246 break;247 case OPTIONS:248 request = new HttpOptions(url);249 setHeaders(request, headers);250 break;251 case TRACE:252 request = new HttpTrace(url);253 setHeaders(request, headers);254 break;255 case HEAD:256 request = new HttpHead(url);257 setHeaders(request, headers);258 break;259 default:260 break;261 }262 log.debug("***Executing REST API Call***");263 org.apache.http.HttpResponse response = httpclient.execute(request);264 log.debug("URL : " + url);265 log.debug("Response Status Code : " + response.getStatusLine().getStatusCode());266 returnResponse = new HttpResponse<>(response, new TypeReference<>() {267 });268 return returnResponse;269 } catch (Exception e) {270 throw e;271 } finally {272 HttpClientUtils.closeQuietly(httpclient);273 }274 }275 private void setHeaders(HttpUriRequest request, Map<String, String> headers) {276 if (headers != null) {277 for (String key : headers.keySet()) {278 request.addHeader(key, headers.get(key));279 }280 }281 }282 private String downloadFile(String fileUrl, Map<String, String> envSettings) throws Exception {283 try {284 if (fileUrl.startsWith(HTTP) || fileUrl.startsWith(HTTPS)) {285 String fileName = FilenameUtils.getName(new java.net.URL(fileUrl).getPath());286 String filePath = getDownloadFilePath(envSettings);287 com.testsigma.automator.http.HttpClient httpClient = EnvironmentRunner.getAssetsHttpClient();288 HttpResponse<String> response = httpClient.downloadFile(fileUrl, filePath + File.separator + fileName);289 if (response != null && response.getStatusCode() == HttpStatus.OK.value()) {290 return filePath + File.separator + fileName;291 } else {292 throw new TestsigmaFileNotFoundException(AutomatorMessages.getMessage(AutomatorMessages.EXCEPTION_DOWNLOAD_LOCAL_FILE, fileUrl));293 }294 } else {295 return fileUrl;296 }297 } catch (Exception e) {298 log.error(e.getMessage(), e);299 throw e;300 }301 }302 private String getDownloadFilePath(Map<String, String> envSettings) {...

Full Screen

Full Screen

downloadFile

Using AI Code Generation

copy

Full Screen

1import com.testsigma.automator.webservices.WebserviceUtil;2import org.testng.annotations.Test;3public class DownloadFile {4 public void downloadFile() throws Exception {5 }6}7import com.testsigma.automator.webservices.WebserviceUtil;8import org.testng.annotations.Test;9public class DownloadFile {10 public void downloadFile() throws Exception {11 }12}13import com.testsigma.automator.webservices.WebserviceUtil;14import org.testng.annotations.Test;15public class DownloadFile {16 public void downloadFile() throws Exception {17 }18}

Full Screen

Full Screen

downloadFile

Using AI Code Generation

copy

Full Screen

1import com.testsigma.automator.webservices.WebserviceUtil;2WebserviceUtil webserviceUtil = new WebserviceUtil();3import com.testsigma.automator.webservices.WebserviceUtil;4WebserviceUtil webserviceUtil = new WebserviceUtil();5if(isDownloaded) {6}

Full Screen

Full Screen

downloadFile

Using AI Code Generation

copy

Full Screen

1import com.testsigma.automator.webservices.WebserviceUtil;2import com.testsigma.automator.webservices.WebserviceUtil;3if(downloadStatus)4{5 System.out.println("File Download Successful");6}7{8 System.out.println("File Download Failed");9}10import com.testsigma.automator.webservices.WebserviceUtil;11import com.testsigma.automator.webservices.WebserviceUtil;12if(downloadStatus)13{14 System.out.println("File Download Successful");15}16{17 System.out.println("File Download Failed");18}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful