【问题标题】:Is there any way to send a API get request after performing some functional steps using Selenium?在使用 Selenium 执行一些功能步骤后,有什么方法可以发送 API 获取请求?
【发布时间】:2023-03-09 02:37:01
【问题描述】:

我正在使用 Cucumber 和 Junit 开发 BDD 框架。我有上传资产然后发布的场景。资产发布后,它将到达不同的第 3 方应用程序。我们可以访问少数第三个应用程序来验证。但在少数其他网站中,我们无法访问 UI,因此我们需要发送 API 获取请求,然后我们将收到有关资产是否存在的响应。

我想知道在执行一些功能步骤后,有什么方法可以通过 selenium 发送 API 获取请求。

第 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 步:验证返回的先前发布的资产(例如:图片)

我们非常感谢您的帮助。谢谢。

【问题讨论】:

  • 为什么不能使用 RESTAssured 来发送 API 请求?
  • 感谢@NandanA 的回复。如果您不介意,我可以知道您使用哪个依赖项来放心。实际上,我尝试了以下方法,但似乎不起作用。 io.rest-assuredrest-assured4.4.0test
  • @Nagaraju 更新了解决方案。请看。

标签: api selenium selenium-webdriver


【解决方案1】:

我可以分享我目前正在处理类似案例的实时场景。这就是我的场景的样子,

场景

  @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>

【讨论】:

  • 更新了我的解决方案。如果有帮助,请采纳答案。
  • 它返回的响应为 Null,我刚刚意识到 API 是安全的,我们需要发送授权详细信息。我已经用更多细节更新了这个问题@Nandan A
  • 嗨,欢迎来到 SO。您不能像这样更改问题?我刚刚回答了原始问题这应该是另一个问题。
  • 对不起,您的回答很有帮助,但无法实现我所期待的。所以我创建了一个新问题stackoverflow.com/questions/70063581/…
猜你喜欢
  • 2020-08-19
  • 2016-11-10
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
  • 2022-01-09
  • 2018-03-03
  • 1970-01-01
相关资源
最近更新 更多