我正在使用 Cucumber 和 Junit 開發 BDD 框架。我有上傳資產然后發布的場景。資產發布后,它將到達不同的第 3 方應用程式。我們可以訪問少數第三個應用程式進行驗證。但是在其他一些網站中,我們無法訪問 UI,因此我們需要發送 API 獲取請求,然后我們將收到有關資產是否存在的回應。
我想知道在執行一些功能步驟后,有什么方法可以通過 selenium 發送 API get 請求。
第 1 步:發送 post 請求,它將發送如下回應。
{
"totalAssetsModifiedOrCreated": 1,
"totalAssetsDeleted": 0,
"deletedAssets": [],
"hits": [
{
"path": "/content/dam/global-asset-library/Products/automation/download.jpg",
"renditions": [
"/content/dam/global-asset-library/Products/automation/download.jpg/jcr:content/renditions/cq5dam.web.1280.1280.jpeg"
],
"metadata": {
//Asset metadata
},
"previewLink": "https://qa.dam.com/content/dam/global-asset-library/Products/automation/download.jpg?qtm=1637340248265"
}
],
"status": {
"code": "200",
"message": "Search results found.",
"success": true
}
}
第 2 步:使用上述回應中的預覽鏈接發送獲取請求。
第 3 步:驗證回傳的先前發布的資產(例如:影像)
非常感謝您的幫助。謝謝你。
uj5u.com熱心網友回復:
我可以分享當前正在處理類似案例的實時場景。這就是我的場景的樣子,
場景:
@Regression
Scenario: Create a new case by using newly created values
Given Login to XYZ Portal
And Create a case based on newly created values and configurations
And Write the case details in excel
And Search for the case and download the ABC file
And Get and verify the case details by using API
如果您看到該場景的最后一步,我們將呼叫 API 呼叫來獲取和驗證詳細資訊。
步驟定義:
@And("^Get and verify the case details by using API$")
public void get_and_validate_the_generated_case() {
Response response = executeGetRequest(url, 200, 30);
// Validate your response
}
代碼:
public static Response executeGetRequest(String url, int responseCode, int timeOut) {
int timeOutInMilliseconds = timeOut * 1000;
Response response = null;
try {
RestAssured.baseURI = url;
RestAssured.config = RestAssuredConfig.config().httpClient(
HttpClientConfig.httpClientConfig().setParam("http.socket.timeout", timeOutInMilliseconds)
.setParam("http.connection.timeout", timeOutInMilliseconds));
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
response = request.get();
if (response.getStatusCode() != responseCode) {
System.out.println("Failed at getRequest. Expected response code: " responseCode
" & Actual response code: " response.getStatusCode());
}
return response;
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
進口:
import io.restassured.RestAssured;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
依賴:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.2.0</version>
</dependency>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364304.html
