Karate UI automation: How can I read content of generated PDF/word/excel file using Karate UI automation [duplicate] - karate

I have an export to excel feature in our application.
For which I have one scenario:
Perform export to excel
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:
Content-Type →application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
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?

2 options.
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')
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

Find out a way to implement the solution with Java and Karate. Below are the steps:
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)
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);
Now, You will have the excel in the specified location.
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.
In Karate, use the boolean like this:
And match excelCompareResult == true
Hope it will help.

Related

How to call a set of code line only once in Scenario Outline in Karate API [duplicate]

I am calling multiple json and js files in my feature file in background, which is required for every scenarion in my feature file.
def test= read('classpath:testData/responseFiles/test.json')
problem is that, it is running/reading for each scenario. Is there something i can do, so that it read only once for feature file and can use for all scenarios. I am using 9.0.0 karate version
callonce is only working to call feature filen not json file
Read the JSON file in a called feature and then use callonce.

How to read test data from external JSON file and compare with postman response?

I have array of test data json file in local machine. I am writing a test in postman and trying to compare the response against the test data. Instead of doing it in collection runner, is it possible to pass the test data into Tests and compare with the response?
This is not something that is currently supported in Postman:
https://github.com/postmanlabs/postman-app-support/issues/7210
Alternatively what you could do
Copy/paste content of file (assuming it's not huge) and read into a variable which you could then do a string comparison against.
Store the content of the file in an endpoint where you can request it in the pre-request script, save it to a variable then do string comparison.
Neither solution is pretty in my opinion but probably the best you can do.

Updated JSON file is not reading during runtime

Team,
I have service to register a user with certain data along with unique mail id and phone no in JSON file format as a body (for ex: registerbody.json).
Before Post call I am generating unique mail id , phone no and updating the same json file (registerbody.json) fields which is in the same folder where feature file locates. I see the file is updated with the required data during runtime.
I used read () method and performed POST request
Surprisingly read method is not taking updated JSON file instead it is reading old data in the registerbody.json file.
Do you have any idea on this, why it is picking up old data even though file is updated with the latest information?
Please assist me with this.
Karate uses the Java classpath, which is typically target/test-classes. So if you edit a file in src/test/java Karate won't see it unless it is copied. This copying is automatically done when you build / compile your code.
My suggestion is use target/ as a temp folder and then you can read using the file: prefix:
* def payload = read('file:some.json')
Before Post call I am generating unique mail id , phone no and updating the same json file (registerbody.json)
You are making a big mistake here, Karate specializes in updating JSON based on variables. I suggest you take 5 minutes and read this part of the docs VERY carefully: https://github.com/intuit/karate#reading-files
Especially the part about embedded expressions: https://github.com/intuit/karate#embedded-expressions

How to use one scenario output to another scenario without using properties files

I am working on API testing project. My requirement is to use response of one API as a response of another. I need different Feature files for each API. The challenge was to use output of one API as input to another which in my case is output of one feature file as input of another.
Also i don't want to call one feature file in another. So to achieve this currently we are using Runner class to initiate the test and using Properties file to store the responses. In the same run we are reading these properties file which act as input to another API(Feature file).
Is there any other better way to do this since we are not willing to use properties file in the framework.
Thanks
I think you are over-complicating your tests. My advice is combine the 2 calls into one scenario. Else there is no way unless you call a second feature file.

Play: Automating test data setup

I have a playframework project that has reached beta/user testing.
For this testing we require test data to exist in the environment
I am looking for a way to automate this via scripts.
The best way will be via call's to the API passing the correctly shaped data based on the models in the project (thus dependant on the project not external).
Are there any existing SBT plugins that I could utilise that would be able to create the appropriate JSON and pass it to the API to setup the environment
Why do you need a plugin for this? I think what you want to do is to have a set of Json, then call the end-points and see what is the response from the back-end. In case of "setting up" based on a call that has a Json, you could use FakeRequest in your tests:
val application = newGuiceApplicationBuilder().build()
val response = route(application, FakeRequest(POST, "/end-point")).get
contentAsString(response) must include("where is Json")
In your test you can also test the response from the back-end and the Json you are feeding it:
Create a set of Json using Writes, based on a case class you are using in the back-end. You could also purposely create an invalid Json as well, that misses a field for example; or has an invalid structure.
Use Table driven testing and sending FakeRequest with the body/header containing your Json; and then checking it against the expected results.
I'm on the move, when I get home, I can write an example code here.