ContributionsMost RecentMost LikesSolutionsRe: Getting rawReponse for a graphQL request using groovy My Question itself was incorrect - I meant rawRequest. Sorry for that. Re: Getting rawReponse for a graphQL request using groovy Yes, rawResponse give entire raw response. We are looking for raw request. Above would give only request body. Re: Getting rawReponse for a graphQL request using groovy We are calling an eventHandler script - TestRunListener.afterStep. This step would excute after a REST or GraphQL step and get the test data for reporting purpose. We would like to get rawResponse that includes method, URL, headers used and body in the GQL call. we are able capture all the info except headers. Getting rawReponse for a graphQL request using groovy As a part tests data capture I would like to capture rawResponse of a graphQL test step. For REST requests, we can use TestStep.testRequest.messageExchange.rawRequestData messageExhange is not available for graphQL type. Any pointers? TIA. Re: Auth Token is not getting refreshed due to which API is getting failed with error 401 Jainsoumya, call is not a problem. but how to reach you? (do not want to put my email/ph# here 🙂 ) Re: Groovy script - Sum two values of REST response and pass the sum on to the next request mrechberger, checkout below script. I am reading it from a file as I don't have a request to read it's response. assumption behind my script is that you want to modify the response with new json array and "value" should be sum of 1.4+ 2. import net.sf.json.groovy.* import groovy.json.JsonSlurper import groovy.json.JsonBuilder //Read response from a file jsonFile = "C:/Users/new2API/Downloads/response.txt" def jsonResponse = new File(jsonFile).text def parsedJson = new JsonSlurper().parseText jsonResponse //if directly reading response from a previous REST Call //Get response //jsonResponse = context.expand('${'+ TestStepName +'#Response}').toString() //parsedJson = new JsonSlurper().parseText jsonResponse log.info parsedJson //reading values from Json response and add def valSum = 0 parsedJson.Items.each{ valSum = valSum + it.Price.value } /* //modify earlier response to send as a payload //here you are sending only one json array in the payload parsedJson.Items.remove(0) //update json nodes parsedJson.Items[0].Description = 'ABC' parsedJson.Items[0].Price.currencyCode = 'EUR' parsedJson.Items[0].Price.value = valSum //rebiuld json payload def requestPayload = new JsonBuilder(parsedJson).toString() log.info requestPayload */ //if you want add another json array then use this code def itemJson = parsedJson.Items[0] log.info itemJson //add it to earlier response def newJsonArray = parsedJson.Items + itemJson //## Get Request Map ##// def jsonMap = [:] parsedJson.each{ jsonMap[it.key] = it.value } //define items map def itemsMap = [:] newJsonArray.each{ itemsMap[it.key] = it.value } // Replace items with new array// jsonMap['Items'] = newJsonArray //update json nodes jsonMap.Items[2].Description = 'ABC' jsonMap.Items[2].Price.currencyCode = 'EUR' jsonMap.Items[2].Price.value = valSum //rebiuld json payload def requestPayload = new JsonBuilder(jsonMap).toString() log.info requestPayload Re: Auth Token is not getting refreshed due to which API is getting failed with error 401 Jainsoumya, probably you are right. I am not getting a full picture of your test configuration. However, from your Auth Manager snapshot it is clear that you are using an Authorization Profile to generate a token. Also, I am assuming that there is way to generate a token from Auth profile screen when it expires. We architected our tests to use these three components of ReadyAPI - environment, AuthProfile and event handlers. Environments comes handy if you want to run tests in different environments say QA, Dev, or Pre-PROD etc. Also, you can have different auth profiles for each environment and switching between different environments makes it easy. Finally event handlers to perform certain task based on the conditions such as authToken refresh before a service call. please refer to attached snapshot to get an idea about event handlers usage. Please note that you may have to run the test at testcase level or testsuite level in order to see this specific even to trigger. Hope this helps. Re: Auth Token is not getting refreshed due to which API is getting failed with error 401 Jainsoumya, looks like you are using OAuth (1 or 2) for your access token. Once you configure your authProfile to retrieve token, you can use event handlers to run your scripts to automatically update token. We have implemented very similar approach. we are using TestRunListener.beforeStep and this script will be called only for a REST step (this is done using script logic). you also could do this for SubmitListener.beforeStep event too. //## Get Project ##// def project = testRunner.testCase.testSuite.getProject() //## Get TestStep ##// def TestStep = context.getCurrentStep() //## Get oAuth Profile Name ##// def oAuthProfileName def oAuthProfile def TokenStatus def oAuthClientFacade def currentToken def ENV def i = 0 if (TestStep.config.type == 'restrequest'){ //## get current environment ##// log.info "Running GetAccessToken event script..." ENV = testRunner.testCase.testSuite.project.activeEnvironment.name log.info "test environment is $ENV..." //## Get oAuth profile name list ##// def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList() oAuthProfilelist.each{ oAuthProfileName = oAuthProfilelist[i] //log.info "oAuth Profile name is: $oAuthProfileName" if(oAuthProfileName == "$ENV"){ //log.info "oAuth Profile for $ENV is: $oAuthProfileName" return } i++ } //## Get Token Status ##// oAuthProfile = project.getAuthRepository().getEntry("$oAuthProfileName") TokenStatus = oAuthProfile.accessTokenStatus.toString() log.info "Access Token Status for $oAuthProfileName is: $TokenStatus" Thread.sleep(5000) //oAuthProfile.setClientID("$ClientID") //oAuthProfile.setClientSecret("$ClientSecret") //## Get Access Token from the server if expired ##// if ((TokenStatus != 'RETRIEVED_FROM_SERVER') || (currentToken == oAuthProfile.getAccessToken())) { log.info "Retrieving token from the auth server..." oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS) oAuthClientFacade.requestAccessToken(oAuthProfile, true) log.info "Access Token Status is " + oAuthProfile.accessTokenStatus Thread.sleep(6000) } } thanks. Re: How to encode a PDF file to base64 in groovy? AlexKaras, I tried this solution and it didn't work for a PDF file. Below code worked from me. byte [] bytes = Files.readAllBytes(TestFile.toPath()); String EncodedFile = Base64.getEncoder().encodeToString(bytes) thanks, Sunil Re: How to encode a PDF file to base64 in groovy? richie, that's what even I thought too. couldn't find any groovy native function to do this. but ended up using Java snippet. Here is the updated code: import java.io.File import java.nio.file.Files import java.util.Base64 //## Get Directories ##// def dumpFilePath = context.expand( '${#Project#TestDataPath}' ) def docName = context.expand( '${#TestCase#fileName}' ) //## Encode file to Base64 format ##// log.info "get pdf file from $dumpFilePath" testFile = new File(dumpFilePath + "$docName") log.info "Encoding the file..." byte [] bytes = Files.readAllBytes(testFile.toPath()) String encodedFile = Base64.getEncoder().encodeToString(bytes) log.info "Updating encoded file at testCase level property testFile" testRunner.testCase.setPropertyValue("testFile", "$encodedFile")