QualityMatters
QualityMatters

Reputation: 915

API Automation Testing : Is there any way to automate download scenario with content validation?

I have an export to excel feature in our application. For which I have one scenario:

  1. Perform export to excel
  2. Validate API response status and exported excel content.

With Postman, I am able to save exported excel in .xlsx format with "Send and Download" option on which later I am validating content (Column Headers and row values) manually.

Is there any way to automate this scenario end to end through API automation?

Currently, I am doing get operation (Karate framework) which is responding me these headers in response:

  1. Content-Type →application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

  2. Accept-Ranges →bytes

Body: Stream objects which are not human readable.

Status: 200 ok

If e2e automation not possible/feasible, what should be the acceptance criteria for automation in this case than?

Upvotes: 1

Views: 2935

Answers (2)

Peter Thomas
Peter Thomas

Reputation: 58088

2 options.

  1. If you are sure the binary contents of the file will never change, do a binary comparison, see this example: upload-image.feature: And match response == read('karate-logo.jpg')

  2. You have to write some custom code. There are Java libraries to read Excel. Use one of those, read the data, and then compare with expected results. Refer the docs on Java interop and write a helper function to do this.

EDIT - also see this answer: https://stackoverflow.com/a/53050249/143475

Upvotes: 1

QualityMatters
QualityMatters

Reputation: 915

Find out a way to implement the solution with Java and Karate. Below are the steps:

  1. Send your responsebytes to a java class Utility. This is how I did in Karate feature file:

    And def helper = Java.type('com.java.Utility') And def excel = helper.ByteArrayToExcel(responseBytes)

  2. In Utility class, you will have a ByteArrayToExcel method which will contain this code:

    import org.apache.commons.io.FileUtils;

    FileUtils.writeByteArrayToFile(new File("src\test\java\testdata\Actual_Response.xlsx"), ResponseBytes);

  3. Now, You will have the excel in the specified location.

  4. Write a method to compare two excel file (Actual and your expected one for the particular request). Google it, you will find the code. Specify, its return type to boolean.

  5. In Karate, use the boolean like this:

And match excelCompareResult == true

Hope it will help.

Upvotes: 1

Related Questions