Forum Discussion

vpachpute1's avatar
vpachpute1
Frequent Contributor
8 years ago

ReadyAPI 2.0: To apply assertions inside log files

Hi

 

I have one scenario that,

1 Purchase using API. And validate response - Working fine.

2. Inside my application, there is one log file as trans.log

    - After the purchase, there are following parameters sent inside trans.log file.

          <v:template>
                  <v:name>purchase-all</v:name>
          </v:template>

    - Actually I have to assert weather <v:name>purchase-all</v:name> is present or not inside trans.log file.

 

Note: Actually this file generally contains large data. This field<v:name>purchase-all</v:name> might be present earlier before sending my purchase request. So I need to validate after only sending purchase request API.

 

Can you please help me achieving this using ReadyAPI 2.0

 

Thanks in advance

 

Regards

Vishal Pachpute

  • groovyguy's avatar
    groovyguy
    8 years ago

    If you are able to clear the trans.log file and have access to it, you can use the following groovy script:

     

    PrintWriter writer = new PrintWriter("/path/to/log/trans.log");
    writer.print("");
    writer.close();

    That will zero out the file and erase ALL contents of it. From there, you need a groovy script to parse the contents of the log.

    def fileContents = new File("/path/to/log/trans.log").text.split("\n");
    
    
    for (line in fileContents)
    {
    	if (line.contains("<v:name>purchase-all</v:name>"))
    	{
    		log.info(line);
    	}
    }

    That's a basic proof of concept that you should be able to modify to do what you want.

  • groovyguy's avatar
    groovyguy
    Community Hero

    I think my approach to this would be a groovy script test step that can parse a text/log file and search it for specific values, such as the one you indicated. Are you familiar enough with groovy to attempt that?

    • vpachpute1's avatar
      vpachpute1
      Frequent Contributor

      Hi

       

      Yes. It would be good approach. Actually I am new to Groovy. Can you please assist me in doing this. It would be very great help to me.

       

      Note: Usually my log file is large in size. So is it possible to redirect logs in some temporary file and apply assertions and throw this temp log file.

       

      Thanks in advance.

       

      Regards

      Vishal Pachpute

  • nmrao's avatar
    nmrao
    Champion Level 3
    My bet would be -

    Use the api and do the assertions.
    Use any other assertions such as db etc.

    But definitely, do not go for log files or use this approach at all. Log files may be of sizes in multiples of MB to GB and multiple files too. Some times there can be files rolled out where you may not get that later.
    • vpachpute1's avatar
      vpachpute1
      Frequent Contributor

      Hi nmrao

       

      You are exactly right. Log files are generally large in size. In my other scenarios, I am doing same thing. Most of the assertions are at the API level and DB level.

       

      But in some scenarios, when I sends API request, internally application triggers some requests to other application and needs to validate these things. e.g.

      1 When I purchase using API, I got success message in API response. But internally it triggers notifications call and notification is sent to customer as "Thanks for purchasing this ..... etc". So these things needs to validate in logs weather notification is sent or not.

      2. And these notifications are not stored in any DB. So not able to apply assertions in DB.

       

      So I needed to validate in log files.

       

      Regards

      Vishal Pachpute

      • nmrao's avatar
        nmrao
        Champion Level 3
        May be you should raise an enhancement in the application you are working with saying the issue and need a proper way to test it instead of just relying on log.

        Some times even the message is just logged could also be wrong (in the sense, it is just logged, but did something different).