Home > Back-end >  Is there any way to send a API get request after performing some functional steps using Selenium?
Is there any way to send a API get request after performing some functional steps using Selenium?

Time:11-24

I am working on BDD framework with cucumber and Junit. I have scenario to upload an asset then publish. Once the asset is published it will reach to different 3rd party application. We have access for few 3rd application to validate. But in few other website we do not have the access to the UI, so we need to send a API get request then we will receive the response about asset is present or not.

I am wondering is there any way to send the API get request through selenium after performing the some functional steps.

step 1: Send a post request, it will send a response like below.

{
    "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
    }
}

Step 2: Send a get request using the preview link in the above response.

Step 3: validate the previously published asset returned(ex: Image)

Your help is highly appreciated. Thank You.

CodePudding user response:

I can share a real time scenario currently where I am working with similar case. This is how my scenario looks,

Scenario:

  @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 

If you see the last step of the scenario we are invoking an API call to fetch and validate the details.

Stepdefinition:

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

Code:

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;
}

Imports:

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:

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>4.2.0</version>
    </dependency>
  • Related