0

I have a test class with multiple methods written in RestAssured and TestNG. And I want to execute these methods sequentially in a loop. How can we do that?

The requirement is to fill up a train. I have an API which gives me the number of seats available on a train. Knowing that number, I want to run a loop such that it executes a few test methods like do a journey search, create a booking, make the payment and confirm the booking sequentially every time. So lets say if we have 50 seats available, I want to run the test 50 times where each loop executes each of the methods sequentially.

This is my sample code:

public class BookingEndToEnd_Test {

RequestSpecification reqSpec;
ResponseSpecification resSpec;
String authtoken = "";
String BookingNumber = "";
........few methods....

@BeforeClass
public void setup() {
  ......
 }

@Test
public void JourneySearch_Test() throws IOException {

JSONObject jObject = PrepareJourneySearchRequestBody();

Response response = 
        given()
        .spec(reqSpec)
        .body(jObject.toString())
        .when()
        .post(EndPoints.JOURNEY_SEARCH)
        .then()
        .spec(resSpec)
        .extract().response();


}

@Test(dependsOnMethods = { "JourneySearch_Test" })
public void MakeBooking_Test() throws IOException, ParseException {

JSONObject jObject = PrepareProvBookingRequestBody();

Response response = 

 given()
 .log().all()
.spec(reqSpec)
.body(jObject.toString())
.when()
.post(EndPoints.BOOKING)
.then()
.spec(resSpec)
.extract().response();

}

@Test(dependsOnMethods = { "MakeBooking_Test" })
public void MakePayment_Test() throws IOException, ParseException {

JSONObject jObject = PreparePaymentRequestBody();

Response response = 
 given()
.spec(reqSpec)
.pathParam("booking_number", BookingNumber)
.body(jObject.toString())
.when()
.post(EndPoints.MAKE_PAYMENT)
.then()
.spec(resSpec)
.body("data.booking.total_price_to_be_paid", equalTo(0) )
.extract().response();



}

@Test(dependsOnMethods = { "MakePayment_Test" })
public void ConfirmBooking_Test() throws IOException {
Response response = 
        (Response) given()
.spec(reqSpec)
.pathParam("booking_number", BookingNumber)
.when()
.post(EndPoints.CONFIRM_BOOKING)
.then()
.spec(resSpec)
.extract().response();

}



}

I tried using invocationCount = n. But that executes the method n number of times however I want to run other test methods in sequence first and then run this test second time.

@Test(invocationCount = 3)
public void JourneySearch_Test() throws IOException {

I also tried looking at the @Factory annotation however every Factory solution that I explored explains how to create a simple data set using a data provider. My data set comes from an excel sheet.

Further, like mention before, if I just get a mere number like 50 seats available and want to run all test methods sequentially 50 times, can someone kindly suggest the best way to do it please?

Mihir
  • 491
  • 5
  • 21
  • I don't see a reason to divide this script into 3 separate test cases since you don't do different assertions – Fenio Aug 20 '19 at 07:05
  • Assertions are same but end points are different and the body that goes with every request is different. Also input for second method comes from output from first method and so on.@Fenio – Mihir Aug 20 '19 at 07:23

1 Answers1

1

Isn't that acceptable?

@Test
public void test() throws IOException, ParseException {

JSONObject jObject = PrepareProvBookingRequestBody();
given()
 .log().all()
.spec(reqSpec)
.body(jObject.toString())
.when()
.post(EndPoints.BOOKING)
.then()
.spec(resSpec);

JSONObject jObject = PreparePaymentRequestBody();

given()
.spec(reqSpec)
.pathParam("booking_number", BookingNumber)
.body(jObject.toString())
.when()
.post(EndPoints.MAKE_PAYMENT)
.then()
.spec(resSpec)
.body("data.booking.total_price_to_be_paid", equalTo(0));

given()
.spec(reqSpec)
.pathParam("booking_number", BookingNumber)
.when()
.post(EndPoints.CONFIRM_BOOKING)
.then()
.spec(resSpec);
}
Fenio
  • 3,528
  • 1
  • 13
  • 27
  • I actually did not think this can be done. Also I wanted to seperate them out as these were different end points. If one of them failed, the entire test method would be reported as a failure. What you have suggested makes it very easy for me now but reporting wise could we separate them?@Fenio – Mihir Aug 20 '19 at 07:57
  • @Mihir You can continue with `PreparePaymentRequest` even if `PrepareProvBookingRequest` failed? – Fenio Aug 20 '19 at 08:03
  • No. Prepare methods use the values from the response previous test methods and use them in the input body of the current request. So PrepareJourneySearchRequest uses a template and values are driven by an excel sheet. Response parameters from JourneySearch test are used as input for PrepareProvBookingRequest. If that passes, the response from that is used as input for PreparePaymentRequest and if that passes, then the response values are used for Confirming. Its all sequential and dependent on the method above it.@Fenio – Mihir Aug 20 '19 at 08:18
  • To explain this more clearly. we do a journey search for example London To Paris on a travel date. Thats journey search. Then from the results of many options we choose one of the trains and make a booking. Thats provisional booking. Next we make the payment by different payment methods like credit card or debit card or.vouchers so thats making payment and then finally once payment goes through we get the confirmation. Thats the entire flow and I want this to be done for say all seats available. If there are 50 seats left.I want to fill it all using the tests.@Fenio – Mihir Aug 20 '19 at 08:23
  • @Mihir I don't think you should separate those tests. It seems like it's more like testing a process, not a module/unit. End-to-End tests tend to be in one method annotated with `@Test`. Making dependencies between tests is considered a bad practice. – Fenio Aug 20 '19 at 08:25
  • Ok thanks. Ill follow what you have suggested and keep you updated on it. Thanks for your help@Fenio – Mihir Aug 20 '19 at 08:54
  • Requesting for your help on https://stackoverflow.com/questions/57572362/setting-invocationcount-from-a-variable @Fenio – Mihir Aug 20 '19 at 11:13
  • https://stackoverflow.com/questions/58246734/running-restassured-smoke-tests-with-maven-on-different-environments-with-its-ow @Fenio. Can you help please? – Mihir Oct 06 '19 at 11:16