Passing parameters between tests with RestSharp

As I’m getting more familiar with the RestSharp client library for my API testing I’m confortable with using it for single API response checks e.g. checking a status code is correct, or that the response body is as expected.

However, I’m now looking at more complex tests, namely passing parameters from one API to be used in another. Is anyone aware of how this can be done using RestSharp and, if so, an example would be very useful.

I’ve provided an example below of something similar I’ve done in RestAssured(Java) but I’m now looking to do this using RestSharp and C#

@Test
public void test_ScenarioRetrieveFirstCircuitFor2017SeasonAndGetCountry_ShouldBeAustralia() {

    // First, retrieve the circuit ID for the first circuit of the 2017 season
    String circuitId = given().
    when().
        get("http://ergast.com/api/f1/2017/circuits.json").
    then().
        extract().
        path("MRData.CircuitTable.Circuits.circuitId[0]");

    // Then, retrieve the information known for that circuit and verify it is located in Australia
    given().
        pathParam("circuitId",circuitId).
    when().
        get("http://ergast.com/api/f1/circuits/{circuitId}.json").
    then().
        assertThat().
        body("MRData.CircuitTable.Circuits.Location[0].country",equalTo("Australia"));
}

Hello Andrew!

My understanding of RestSharp is that it simplifies the transaction of an API by abstracting the HTTP call. It would not be used to manage the data inside the API’s response. Once you have the response, you can serialize it (or parse it in a manner of your choosing) and store any of the data elements in variables. At a class level, the variables can be used in any of the methods within the class.

Does that help?

Joe

1 Like

Hi Joe, that does help massively thank you. Take some getting my head around but slowly getting there!