-1

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.

Nagaraju
  • 11
  • 5
  • Why cannot you use RESTAssured to send API requests? – Nandan A Nov 18 '21 at 08:08
  • Does this answer your question? [Is there any way to start with a POST request using Selenium?](https://stackoverflow.com/questions/5660956/is-there-any-way-to-start-with-a-post-request-using-selenium) – Marc Sances Nov 18 '21 at 08:16
  • Thanks @NandanA for your response. If you don't mind may I know which dependency are you using for rest-assured. Actually i tried with the following but it seems like not working. io.rest-assured rest-assured 4.4.0 test – Nagaraju Nov 19 '21 at 15:21
  • @Nagaraju updated the solution. Please see. – Nandan A Nov 19 '21 at 15:43

1 Answers1

0

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>
Nandan A
  • 2,702
  • 1
  • 12
  • 23
  • Updated my solution. If it helps can you please accept the answer. – Nandan A Nov 19 '21 at 15:42
  • It was returning the response as Null, I just realized that the API is secured one we need to send the authorization details. I have updated the question with more details @Nandan A – Nagaraju Nov 19 '21 at 17:59
  • Hi, welcome to SO. You cannot change the question like this I just answered the original question this should be an another question. – Nandan A Nov 20 '21 at 07:53
  • Sorry for the confusion, Your answer is helpful but am not able to achieve what i am looking exactly. So i have created a new question https://stackoverflow.com/questions/70063581/is-there-any-way-to-send-authornicated-post-and-get-api-requests-with-selenium – Nagaraju Nov 22 '21 at 09:37