cancel
Showing results for 
Search instead for 
Did you mean: 

Oauth 2 - Script to fetch oauth profile name

SOLVED
Highlighted
Occasional Contributor

Oauth 2 - Script to fetch oauth profile name

I am trying to fetch the auth profile name that is selected for this environment at the test sutie level.

 

I am using below code, please help me debug the issue on first line

 

String oAuthProfile = testRunner.testCase.getTestStepByName("Groovy Script").getSelectedAuthprofile()

def oAuthProfile = context.getCurrentStep().testCase.testSuite.project.getAuthRepository().getEntry(oAuthProfile)

 

Also am newbie, how do i figure out various classes and methods available in readyapi to use in groovy script

 

3 REPLIES
Moderator

Re: Oauth 2 - Script to fetch oauth profile name

Hello @subhaD,

 

To get name and type of the auth profile applied to the current TestSuite you can use this script:

 

def ts = testRunner.testCase.testSuite
def profileName = ts.getAuthProfile()
if (profileName == "Inherit From Parent"){
	profileName = ts.project.getAuthProfile()
}
log.info "Profile name for current TestSuite: " + profileName
try{
	def profileType = ts.project.getAuthRepository().getEntry(profileName).getType()
	log.info "It is custom profile created from: " + profileType
} catch (e){
	log.info("It is build-in profile")
}

 

To find available classes and methods to use in your Groovy scripts, please refer to API Java Docs: https://support.smartbear.com/readyapi/docs/configure/plugins/dev/background/api.html 

 

Natalie
Customer Care Team

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Occasional Contributor

Re: Oauth 2 - Script to fetch oauth profile name

Thanks for the reply.

 

But am changing to diff Environemnt and diff Oauth2 Profile saved with that.

When that happens I want to see what is associted with env and select that auth profile to check token status.

 

//Get Token Status
def oAuthProfile = project.getAuthRepository().getEntry("Qa-Oauth")
//String oAuthProfile = testRunner.testCase.getTestStepByName("Groovy Script").getSelectedAuthprofile()
def TokenStatus = oAuthProfile.accessTokenStatus.toString()
log.info "Access Token Status is: $TokenStatus"

 

I am trying to change first line with second line, instead of hardcoding the profile name, i want to get that dynamically of what is selected for that env

 

Moderator

Re: Oauth 2 - Script to fetch oauth profile name

Hi @subhaD

 

Instead of your second commented line, you can use this code snippet to get the name of the auth profile, which is applied to a needed service, within the current environment:

 

def env = context.testCase.testSuite.project.getActiveEnvironment()
def restServ = env.getRestServiceAt(0);
restServ.getEndpoint().getConfig().getAuthProfile()

 

Example:

2018-08-17_1040.png

 

About getting service by name rather than by index you can read here: https://smartbear-cc.force.com/portal/KbArticleViewer?name=How-to-work-with-Environments-in-Groovy-s...

 

I hope I understood your task correctly this time.

 

Natalie
Customer Care Team

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Share Your Stories
Read Latest News
Top Kudoed Authors