Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible misunderstanding of how Serenity BDD share same RestResponse between steps #658

Closed
ManuelAbril opened this issue Jan 23, 2017 · 4 comments

Comments

@ManuelAbril
Copy link

Hi there,

I have been using SerenityRest for a couple of days and I have the following issue: I want to have different steps to configure (given), request (when) and response (then). My problem is that I cannot get then results if it is in another step. Could you help me?

Here you are my code:

Steps class

 @Step
    public void findById(int notificationId) {
        initSpec();
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put(QUERY_PARAM_NOTIFICATION_ID, notificationId);
        getResource(BASE_NOTIFICATION_FINDER_PATH + FIND_BY_ID, queryParams);
    }

    @Step
    public void successFindByIdHttpStatusCode() {
        getHttpStatusCode(SC_OK);
    }

Serenity rest custom class

private static RequestSpecification requestSpecification;

    protected static void initSpec() {
        requestSpecification =
                rest().given().spec(
                        new RequestSpecBuilder()
                                .setContentType(ContentType.JSON)
                                .setBaseUri(BASE_URI)
                                .addFilter(new ResponseLoggingFilter())
                                .addFilter(new RequestLoggingFilter())
                                .build());
    }

    protected void addQueryParams(Map<String, ?> queryParams) {
        requestSpecification.params(queryParams);
    }

    protected void getResource(String resourcePath) {
        requestSpecification.when().get(resourcePath);
    }

    protected void getResource(String resourcePath, Map<String, ?> queryParams) {
        addQueryParams(queryParams);
        getResource(resourcePath);
    }

    protected void getHttpStatusCode(int statusCode) {
        requestSpecification.then().statusCode(statusCode);
    }
@scormaq
Copy link
Contributor

scormaq commented Jan 26, 2017

Why don't you use Serenity test session for this purpose?
http://thucydides.info/docs/serenity-staging/#_storing_data_between_steps

import net.serenitybdd.core.Serenity;
...
    // class1
    public void callService() {
        Serenity.setSessionVariable("response").to("bar");
    }
...
    // class2
    public void assertResponse() {
        final String resp = Serenity.sessionVariableCalled("response");
        Assert.assertEquals(resp, "bar");
    }

@ManuelAbril
Copy link
Author

It is what I am doing right now, but it suppose that I can concatenate SereniryRest operations between different Steps wihtout saving into session. Am I wrong?
http://serenity-bdd.info/docs/serenity/#_writing_tests_with_rest_assured

@wakaleo
Copy link
Member

wakaleo commented Feb 21, 2017

You should be able to use the when() and then() methods in separate steps - Serenity will keep the context, eg:

    @When("^s?he looks for the closest taxi rank within (\\d+) meters$")
    public void lookForTheClosestTaxiRankWithin(int maximumDistance) throws Throwable {
        with().parameters(
                "lat", currentLocation.latitude,
                "lon", currentLocation.longitude,
                "radius", maximumDistance)
                .get(TFLPlaces.find(Place.TaxiRank));
}

    // Checking return status and specific field values
    @Then("^the first taxi rank found should be:$")
    public void heShouldFindRankStand(List<TaxiStand> closestStands) throws Throwable {
        TaxiStand closestStand = closestStands.get(0);

        then().statusCode(200)
                .body("places[0].commonName", equalTo(closestStand.commonName))
                .body("places[0].distance", equalTo(closestStand.distance));
    }

@ManuelAbril
Copy link
Author

Hi @wakaleo. I tried to do it as you told me but I have the same problem. I am using three class layouts:

  1. Only to define @gwt methods.
  2. Create method with different steps inside them.
  3. Rest class to manage all GRUP operations.

Could it be a problem?

@wakaleo wakaleo closed this as completed Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants