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


#1

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


New to API Testing,
#2

+1


#3

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


#4

Instruction is not clear.


#5

Anson said:

Instruction is not clear.

Could you please explain, what exactly do you want?


#6

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


#7

Hi Radek,

Please see following article:

And one more about custom keywords.


#8

Thanks Marek,

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

Radek


#9

Thx for this Siva1