cancel
Showing results for 
Search instead for 
Did you mean: 

ReadyAPI use groovyscript to fetch the token from the environment

SOLVED
Highlighted
Frequent Contributor

ReadyAPI use groovyscript to fetch the token from the environment

Hello,

 

I'm already using Token Authentication for my testcases. It's setup on environment level.

For one of my testcases I need to go to the URL where an overview of all elements that I need is visible. But before I can acces that webpage I need a token. The token that I've defined on environment level. How can I do that in groovy please?

I already have 2 events as described in the smartbear documentation for 

* TestRunListener.afterStep

* SubmitListener.beforeSubmit

 

What I have so far:

import be.fgov.bosa.pdc.testing.readyapi.java.lib.JsonParser

def getConceptSchemes = "https://PRIMARYURLOFMYCLIENT/api/pdc/concepts/schemes".toURL().getText()
log.info getConceptSchemes
//parse json string using JsonSlurper - basically converts it to a groovy list
JsonParser jsonParser = new JsonParser(getConceptSchemes)

//get data starting from roottag 'pdcData'
def findAGC = jsonParser.getJsonArray("['pdcData'][?(@.name=='Activity Group')]['beId']") //==JsonPath
//in order to not print the [" you need to point to the x-th element in that array. Here the beId is the first element
log.info findAGC.get(0)

assert findAGC.length()== 1
//get the beId and put on TestSuite level, this is the first element from the array.
testRunner.testCase.testSuite.setPropertyValue('param_beid', findAGC.get(0) )

I've tried a code that was given on StackOverflow but it doesn't work for me.

import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade

//Read from URL with access token

def authContainer = testRunner.testCase.testSuite.project.OAuth2ProfileContainer
// Get OAuth2 profile name
def profileName = context.expand("AdminV2.0")
def authProfile = authContainer.getProfileByName(profileName)

def clientFacade = new OltuOAuth2ClientFacade()
clientFacade.requestAccessToken(authProfile)
//To get the access token
def accessToken = authProfile.getAccessToken()

 

Can someone help me please? @nmrao   ?

 

Thanks in advance,

AAB

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Frequent Contributor

Re: ReadyAPI use groovyscript to fetch the token from the environment

So....Ok, did it a little bit differently. I've added an extra TestSuite where I'm fetching the overview of the schemes. I've saved that into a parameter on project level.

 

def response = context.expand( '${REST Request#Response}' ) log.info response testRunner.testCase.testSuite.project.setPropertyValue("SchemeS", response )

 

In the testcase itself I deleted the pointing to the URL and called upon the parameter on project level.

import be.fgov.bosa.pdc.testing.readyapi.java.lib.JsonParser

//get the overview schemes on project level
def getConceptSchemes = testRunner.testCase.testSuite.project.getPropertyValue('SchemeS')
//parse json string using JsonSlurper - basically converts it to a groovy list
JsonParser jsonParser = new JsonParser(getConceptSchemes)

//search for beId country
def beidCountrycode=  jsonParser.getJsonArray("['pdcData'][?(@.name=='COUNTRY')]['beId']")
//put beId on TesSuite level
testRunner.testCase.testSuite.setPropertyValue('param_beid', beidCountrycode.get(0) )
log.info beidCountrycode

 

Then I put the AdminV2.0 Token on Projectlevel in the authManager and selected this profile for the project itself. ReadyAPI then asks me if it needs to inherent to the children cases. I said yes.

View solution in original post

4 REPLIES 4
Highlighted
New Contributor

Re: ReadyAPI use groovyscript to fetch the token from the environment

Hi AAB,

 

Hopefully I read your question right here. The way I do this is to get my Auth Profile via a slightly different method; like this:

def oAuthProfile = testRunner.testCase.testSuite.getProject().getAuthRepository().getEntry(nameOfProfile)

 

"oAuthProfile.getAccessToken()" seems to work for me after that.

 

I hope it helps!

-Dan

Highlighted
Frequent Contributor

Re: ReadyAPI use groovyscript to fetch the token from the environment

Hi Dan  @dwenning 

thanks for answering. Could you tell me if I need some packages to import please?

 

I'll give it a try though.

 

Cheers

AAB

Highlighted
New Contributor

Re: ReadyAPI use groovyscript to fetch the token from the environment

Hello again!

 

You *shouldn't* need any imports to run that chunk of groovy. I have the two additional libraries below, but I believe they are leftover from some refactoring I did a while back. So give it a try without them. I hope it helps!

 

import com.eviware.soapui.impl.rest.OAuth2Profile.*
import java.io.*

-Dan

Highlighted
Frequent Contributor

Re: ReadyAPI use groovyscript to fetch the token from the environment

So....Ok, did it a little bit differently. I've added an extra TestSuite where I'm fetching the overview of the schemes. I've saved that into a parameter on project level.

 

def response = context.expand( '${REST Request#Response}' ) log.info response testRunner.testCase.testSuite.project.setPropertyValue("SchemeS", response )

 

In the testcase itself I deleted the pointing to the URL and called upon the parameter on project level.

import be.fgov.bosa.pdc.testing.readyapi.java.lib.JsonParser

//get the overview schemes on project level
def getConceptSchemes = testRunner.testCase.testSuite.project.getPropertyValue('SchemeS')
//parse json string using JsonSlurper - basically converts it to a groovy list
JsonParser jsonParser = new JsonParser(getConceptSchemes)

//search for beId country
def beidCountrycode=  jsonParser.getJsonArray("['pdcData'][?(@.name=='COUNTRY')]['beId']")
//put beId on TesSuite level
testRunner.testCase.testSuite.setPropertyValue('param_beid', beidCountrycode.get(0) )
log.info beidCountrycode

 

Then I put the AdminV2.0 Token on Projectlevel in the authManager and selected this profile for the project itself. ReadyAPI then asks me if it needs to inherent to the children cases. I said yes.

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors