Salesforce API Testing with Katalon(dynamically handle OAuth token by script)

Hi All,
I hope this article would be helpful for all salesforce automation testers.

Below code for Generating access token and storing access token in the global variable using script for above Post method

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import com.kms.katalon.core.model.FailureHandling as FailureHandling

import com.kms.katalon.core.testcase.TestCase as TestCase

import com.kms.katalon.core.testdata.TestData as TestData

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable

import static org.assertj.core.api.Assertions.*

import com.kms.katalon.core.testobject.RequestObject as RequestObject

import com.kms.katalon.core.testobject.ResponseObject as ResponseObject

import com.kms.katalon.core.webservice.verification.WSResponseManager as WSResponseManager

import groovy.json.JsonSlurper as JsonSlurper

import groovy.json.JsonSlurper

  

  

tokenGen().toString()

  

 def token

String tokenGen() {

    def response = WS.sendRequest(findTestObject('GenerateAccessToken'))

    def jsonSlurper = new JsonSlurper()

    def slurper = new JsonSlurper()

    def result = slurper.parseText(response.getResponseBodyContent())

    def token = result.access_token

WS.verifyResponseStatusCode(response, 200)

println(token)

    GlobalVariable.token = token

}

Creating POST method by configuration

Code for running POST method using script for above POST method

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import com.kms.katalon.core.model.FailureHandling as FailureHandling

import com.kms.katalon.core.testcase.TestCase as TestCase

import com.kms.katalon.core.testdata.TestData as TestData

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable

import com.kms.katalon.core.testobject.TestObjectProperty

import com.kms.katalon.core.testobject.ConditionType

import internal.GlobalVariable as GlobalVariable

import com.kms.katalon.core.testobject.RequestObject

'Get token'

String token = WebUI.callTestCase(findTestCase('Salesforce API Testing/AccessTokenGeneration'), [:], FailureHandling.CONTINUE_ON_FAILURE)

println(token)

'Scope to a project'

def request = (RequestObject)findTestObject('Object Repository/LeadCreation')

'Create new ArrayList'

ArrayList<TestObjectProperty> HTTPHeader = new ArrayList<TestObjectProperty>()

'Send token in HTTP header'

HTTPHeader.add(new TestObjectProperty('Authorization', ConditionType.EQUALS, "Bearer" + token))

HTTPHeader.add(new TestObjectProperty('Content-Type', ConditionType.EQUALS, 'application/json'))

'Set that token'

request.setHttpHeaderProperties(HTTPHeader)

'Get response text'

response = WS.sendRequest(findTestObject('Object Repository/LeadCreation'))

WS.verifyResponseStatusCode(response,200)

ConfiguresalesforcewithKatlon.png

Postmethod2.png

postmethod.png

Postmethodvar.png

12 Likes

+1

1 Like

It worked for me. Thank you so much :slight_smile:

1 Like

Instruction is not clear.

Anson said:

Instruction is not clear.

Could you please explain, what exactly do you want?

1 Like

Hi,
Sorry I’am beginner in katalon - I know what the code above is doing - but where is the place in katalon studio where is necessary to paste it?

thanks Radek

Hi Radek,

Please see following article:

https://docs.katalon.com/katalon-studio/tutorials/create_test_case_using_script_mode.html

And one more about custom keywords.

https://docs.katalon.com/katalon-studio/videos/custom_keywords_and_method_call_statements.html

Thanks Marek,

thanks - after reading articles is OAUTH 2 is working for me.

Radek

Thx for this Siva1

Thanks Siva1,

It is working for me :slight_smile:

Hai , OAUTH 2 is not working for me. what is in postman, i gave same input data. Thanks in Advance.

Hey @pradeep.k,

Please try Authorization type is ‘No Auth’ and pass the value in Http Header section ‘Content-Type’ as ‘multipart/form-data’ header section . Try once again and let me know, if you face any issues.

I tried that, still when i enter anything under form-data and either click run or click save, it just auto-clears everything which was entered and returns below error:

image

Yes, i tried with Content-Type “mutipart/form-data” and still same result. I am not sure why it just clears everything from form-data. I did not face this issue in older version, I am actually considering to downgrade from 7.0 to 6.3.3 version

Hi,

I;m trying to use this example for my project but I’m stuck.
The situation is like this:
I have a web app where the user is login in a window. After the checking of credential a new window will be opened, based on session ID, and after that the user will go through a lot of windows, depending on user interactions, will be opened based on same session ID.

How can i get the session id after login and store it as a variable?

Hi Siva,
In our company we are using OAuth2.0. I gave the credential under authorization tab, getting the token but getting the error message. And when I do adding token for Global variables under Default value (GlobalVariable.null) it’s not taking it. Please suggest me what’s I did wrong with my code?

Reason:
groovy.lang.MissingPropertyException: No such property: token for class: internal.GlobalVariable
at GenerateAccessToken.tokenGen(GenerateAccessToken:41)
at GenerateAccessToken.run(GenerateAccessToken:22)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:398)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute