我将 Selenium WebDriver 4.19 与 Java 结合使用,并将测试结果上传到 Slack。 Slack 有一天通知我,我用来执行此操作的 files.upload Web API 方法已被弃用。
Updates required for apps still using the files.upload API
A workspace that you administer contains apps or integrations that recently used the files.upload web API method. We want to inform you about two important changes coming to support for the files.upload API.
May 8, 2024
Newly created Slack apps will no longer be able to access the files.upload API. Existing applications will be able to continue using files.upload until support is discontinued.
March 11, 2025
Support for the files.upload API for all apps will be discontinued.
To prepare for this change, we recommend that app owners migrate away from files.upload and instead use the combination of files.getUploadURLExternal(https://api.slack.com/methods/files.getUploadURLExternal) and files.completeUploadExternal(https://api.slack.com/methods/files.getUploadURLExternal). You can also leverage Slack's SDKs to help transition to this new way of uploading files by visiting our api.slack page(https://api.slack.com/messaging/files#uploading_files).
根据api.slack页面,这些是解决问题的说明
files.getUploadURLExternal
。响应将包含一个 URL,您可以将文件内容发布到该 URL。files.completeUploadExternal
。您可以选择使用此 API 的参数为您的一个或多个文件指定共享目标。这完成了第 1 步中开始的文件上传。这是我与 files.upload 一起使用的现有代码。该代码负责将内容发布到 Java。我对 Java 很陌生。我不明白 Call files.getUploadURLExternal 和 Call files.completeUploadExternal 的含义。有没有人有示例 Java 代码来解释这些调用语句如何工作。
// This method sends the test execution results report to Slack
public void sendTestExecutionReportToSlack() throws Exception {
String url = "https://slack.com/api/files.upload"; // This is the Slack url to upload the test execution results report to
try {
// This is the code to send the test execution report to Slack
HttpClient httpclient = HttpClientBuilder.create().disableContentCompression().build(); // Initiate the HttpClient to send the test execution results report to
HttpPost httppost = new HttpPost(url); // Post the Slack url
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // Build the builder entity to host the test execution results report
FileBody fileBody = new FileBody(new File(PathToReport)); // Add the path of the test execution results report to the file body
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // Set up the builder structure to send the test execution results report and make sure it is browser compatible
builder.addPart("file", fileBody); // Add the file containing the test execution results report to the builder structure
builder.addTextBody("channels", channelName); // Add the channelName to the builder TextBody
builder.addTextBody("token", botUserOAuthAccessToken); // Add the token to the builder TextBody
httppost.setEntity(builder.build()); // Set the Entity for builder, post the test execution results report with all the builder components including file, channels and token
HttpResponse response = null; // Set the HTTPResponse to null
response = httpclient.execute(httppost); // The response is equal to the result of httppost
HttpEntity result = response.getEntity(); // HTTPEntity Result equals the response of getEntity
} catch (Exception e) {
// If the code to send the test execution results report fails, print the associated stack trace
e.printStackTrace(); // Print the Stack Trace
}
}
此外,https://api.slack.com/methods/files.getUploadURLExternal需要一个强制的整数/长度参数(正在上传的文件的大小以字节为单位。)可以使用 builder.addPart() 来完成吗
对于 FilesUploadV2Request,应该发生以下情况: 包装方法使开发者能够轻松地使用新的上传文件的方式。为了减轻 files.upload API 现有用户的困惑,此方法在内部执行一些 HTTP 请求。
第 1 步:https://api.slack.com/methods/files.getUploadURLExternal(每个文件)
第2步:POST请求到upload_url(每个文件)
第3步:https://api.slack.com/methods/files.completeUploadExternal(仅一次)
虽然大部分参数是兼容的,但差别不大。与 files.upload API 不同,这种新方式允许开发人员一次上传多个文件。此外,不再支持以下操作。 - 在多个频道中共享上传的文件 - 设置文件的文件类型
我已经相应地修改了我的代码。我想我已经很接近了,但我的文件仍然无法上传
try //This is the code to send the test execution report to Slack
{
HttpClient httpclient = HttpClientBuilder.create().disableContentCompression().build(); //Initiate the HttpClient to send the test execution results report to
HttpPost httppost = new HttpPost(url); //Setup a new HttpPost object for the URL https://slack.com/api/files.getUploadURLExternal
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); //Build the builder entity which allows you to setup the different components of your test results file/report that is to be uploaded
FileBody fileBody = new FileBody(new File(PathToReport)); //Add the path of the test execution results report to the file body
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); //Set up the builder structure to send the test execution results report and make sure it is browser compatible
builder.addPart("file", fileBody); //Add the file containing the test execution results report to the builder structure
builder.addTextBody("channels", channelName); //Add the channelName to the builder TextBody
builder.addTextBody("token", botUserOAuthAccessToken); //Add the token to the builder TextBody
FilesUploadV2Request haw = new FilesUploadV2Request();
haw.setToken(botUserOAuthAccessToken);
haw.setChannel(channelName);
haw.setFilename(PathToReport);
haw.setFile(new File(PathToReport));
haw.builder();
FilesUploadV2Request.UploadFile.builder();
}
catch (Exception e) //If the code to send the test execution results report fails, print the associated stack trace
{
e.printStackTrace(); //Print the Stack Trace
}
}