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.environment.Of.myClient.testing.readyapi.java.lib.JsonParser
def getConceptSchemes = "https://PRIMARYURLOFMYCLIENT/api/client/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 'clientData'
def findAGC = jsonParser.getJsonArray("['clientData'][?(@.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
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.environment.Of.myClient.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("['clientData'][?(@.name=='COUNTRY')]['beId']") //put beId on TesSuite level testRunner.testCase.testSuite.setPropertyValue('param_beid', beidCountrycode.get(0) ) log.info beidCountrycodeThen 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.