Salesforce testing
This integration provides assistance for certain user-specific use cases, but it is not officially supported. Compatibility with all user requirements is not guaranteed.
This document shows how to automate Salesforce tests with Katalon Studio. The sample project covers lead, case, account, and dashboard management, including an iframe example.
Salesforce may ask for an MFA or identity verification code when a test opens a new browser. First, set up token-based login for each test user. Then replace the login steps in your tests with the access-token test case and run the Salesforce actions.
| Use case | Actions |
|---|---|
| Lead management | Open Salesforce → create a lead → change its status and owner → find and delete it → close the browser |
| Case management | Open Salesforce → create a case → edit its status → find and delete it by ID → close the browser |
| Account management | Open Salesforce → create an account → close the browser |
| Dashboard management | Open Salesforce → create a dashboard → add components and reports → create a filter → delete it → close the browser |
Before you begin​
- Install Katalon Studio and have an active Salesforce account for each test user.
- Get permission to create a Salesforce External Client App, or use an existing Connected App, and permission to make the test users eligible through profiles or permission sets. Since the Spring '26 release, Salesforce no longer allows creating new Connected Apps by default.
- Find your org's My Domain URL, such as
https://yourdomain.my.salesforce.com. For a sandbox, usehttps://yourdomain--sandboxname.sandbox.my.salesforce.com. Replace the example domain in every request below. - Download the Salesforce sample project if you want to adapt its recorded tests. Its existing login steps use a username and password, so replace them as described in Automate Salesforce test cases.
Set up login without MFA prompts​
Salesforce asks for multi-factor authentication (MFA) or an identity verification code whenever someone logs in through the Salesforce login page from a browser it doesn't recognize. Every Katalon Studio test run opens a new browser, so Salesforce asks a test that logs in through the login page for a code on every run.
Token-based login skips the Salesforce login page. Log in once as each test user, including MFA or SSO, to authorize your app and obtain a refresh token. At the start of each test run, exchange that token for an access token and open Salesforce through frontdoor.jsp. This does not turn off MFA or SSO.
| Token | How long it lasts | When to get another |
|---|---|---|
| Authorization code | A few minutes; single use | When obtaining a refresh token for a test user |
| Refresh token | Until revoked or expired by your org's policy | Repeat Get a refresh token for each test user |
| Access token | Your Salesforce session timeout; two hours of inactivity by default | At the start of every test run |
Create a Salesforce app​
If you already have a Connected App, open it from Setup > App Manager and apply the callback URL, scopes, and policies below. Otherwise, create an External Client App:
-
Go to Setup in Salesforce, enter
External Client App Managerin Quick Find, and click New External Client App. -
Enter a name and a contact email, and set Distribution State to Local.
-
Expand API (Enable OAuth Settings) and select Enable OAuth.
-
Enter your My Domain URL followed by
/services/oauth2/successin Callback URL, for examplehttps://yourdomain.my.salesforce.com/services/oauth2/success. Do not use the home page or root URL: Salesforce redirects those pages, and you lose the authorization code. -
Add Manage user data via Web browsers (web), Manage user data via APIs (api), and Perform requests at any time (refresh_token, offline_access) to OAuth Scopes.
-
Select Require secret for Web Server Flow and Require secret for Refresh Token Flow in Security, and leave Enable Refresh Token Rotation cleared.
If Salesforce locks Require Proof Key for Code Exchange (PKCE) or Enable Refresh Token Rotation on, leave them. The next stages cover the PKCE step and the Rotating refresh token script.
-
Click Create.
-
Click Edit on the Policies tab and expand OAuth Policies.
-
Select Admin approved users are pre-authorized in Permitted Users, and add the profiles or permission sets of your test users. To only try out the setup, you can select All users can self-authorize instead.
-
Set Refresh Token Policy to Refresh token is valid until revoked and click Save.
-
Expand OAuth Settings on the Settings tab and click Consumer Key and Secret.
-
Copy the Consumer Key and the Consumer Secret. Keep the secret out of source control.
Get a refresh token for each test user​
Run these requests manually, once for each test user. Repeat them if the refresh token expires or is revoked.
-
PKCE-enabled apps only: Generate a code verifier and challenge by running this script in a Katalon Studio test case. Keep both values for the authorization and token requests below.
import java.security.MessageDigest
import java.security.SecureRandom
byte[] random = new byte[32]
new SecureRandom().nextBytes(random)
String verifier = Base64.getUrlEncoder().withoutPadding().encodeToString(random)
String challenge = Base64.getUrlEncoder().withoutPadding().encodeToString(
MessageDigest.getInstance('SHA-256').digest(verifier.getBytes('US-ASCII')))
println "code_verifier = ${verifier}"
println "code_challenge = ${challenge}" -
Open a new or existing project in Katalon Studio.
-
Create a new web service request of type RESTful and name it
Retrieve Authorization code to access bearer token and refresh token. If you change this name, update any references to it. -
Set the method to
GET, enterhttps://yourdomain.my.salesforce.com/services/oauth2/authorizeas the URL, and add the following query parameters.Parameter Value response_typecodeclient_idThe Consumer Key of your app. redirect_uriThe callback URL of your app, for example https://yourdomain.my.salesforce.com/services/oauth2/success.code_challengeOnly if your app requires PKCE. Use the challenge from the PKCE step above. code_challenge_methodOnly if your app requires PKCE. Enter S256. -
Clear the Follow redirects checkbox. Katalon Studio selects this option by default in projects created with version
9.7.0or later. If it stays selected, the request returns the Salesforce login page instead of the URL you need.
-
Click Test Request. The response status is
302. -
Copy the value of
Locationon the Header tab. -
Paste the copied URL into the address bar of your browser.
-
Log in as the test user, including MFA or SSO. If Salesforce asks you to allow access, click Allow.
-
Copy the authorization code from the address bar. The browser stops on a page titled Remote Access Application Authorization, and the address bar shows
https://yourdomain.my.salesforce.com/services/oauth2/success?code=.... The authorization code is the value aftercode=. -
Decode the code in your browser. Press F12, open the Console tab, and run
decodeURIComponent("paste the code here"). For example,aPrxsdssXvusdaaJ4hOFlX4ZHYQMhdjsi9HOTTTw6mQZ%3D%3DbecomesaPrxsdssXvusdaaJ4hOFlX4ZHYQMhdjsi9HOTTTw6mQZ==.cautionThe authorization code is a credential, so don't paste it into third-party decoding tools. The code works only once and expires within minutes. Continue with the next step right away.
-
Create a new web service request and name it
Retrieve Bearer Token and Refresh Token for the first time using Authorization code. -
Set the method to
POSTand enterhttps://yourdomain.my.salesforce.com/services/oauth2/tokenas the URL. -
Select x-www-form-urlencoded on the HTTP Body tab and add the following parameters.
Parameter Value grant_typeauthorization_codecodeThe decoded authorization code. client_idThe Consumer Key of your app. client_secretThe Consumer Secret of your app. redirect_uriExactly the same value as in the authorization request. code_verifierOnly if your app requires PKCE. The verifier from the PKCE step above.
-
Click Test Request. The response contains
access_token,refresh_token, andinstance_url. -
Store the
refresh_tokenoutside the project. For a fixed token, provide it as theSF_REFRESH_TOKENenvironment variable to the Katalon Studio process. For rotation, use the file described below. Clear the authorization code and Consumer Secret from the one-time request before saving the project. Tokens give access as the test user; keep them and the Consumer Secret out of source control.
Open Salesforce with an access token​
-
Create a RESTful request named
Retrieve Bearer Token using Refresh Token. Set its method toPOSTand URL tohttps://yourdomain.my.salesforce.com/services/oauth2/token. -
In HTTP Body tab, select x-www-form-urlencoded and add:
Parameter Value grant_typerefresh_tokenclient_id${clientId}client_secret${clientSecret}refresh_token${refreshToken}
-
On Variables, add
clientId,clientSecret, andrefreshTokenwith empty default values. The test case supplies them at runtime. If you use Test Request manually, supply values only for that test and clear them before saving the request. A refresh token saved as a default is stored in the project. -
Create a test case named
Open Salesforce with Access Token, switch to Script view, and choose the script that matches your app's refresh-token policy. ProvideSF_CLIENT_IDandSF_CLIENT_SECRETas environment variables for the Katalon Studio process. For a fixed token, also provideSF_REFRESH_TOKEN. Do not print these values or store them in the project.
- Fixed refresh token
- Rotating refresh token
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import groovy.json.JsonSlurper
String clientId = System.getenv('SF_CLIENT_ID')
String clientSecret = System.getenv('SF_CLIENT_SECRET')
String refreshToken = System.getenv('SF_REFRESH_TOKEN')
assert clientId && clientSecret && refreshToken : 'Set SF_CLIENT_ID, SF_CLIENT_SECRET, and SF_REFRESH_TOKEN'
RequestObject request = (RequestObject) findTestObject('Object Repository/Retrieve Bearer Token using Refresh Token',
[('clientId'): clientId, ('clientSecret'): clientSecret, ('refreshToken'): refreshToken])
ResponseObject response = WS.sendRequest(request)
WS.verifyResponseStatusCode(response, 200)
Map tokens = (Map) new JsonSlurper().parseText(response.getResponseText())
String instanceUrl = tokens.get('instance_url')
String accessToken = tokens.get('access_token')
WebUI.openBrowser("${instanceUrl}/secur/frontdoor.jsp?sid=${accessToken}&retURL=%2Flightning%2Fpage%2Fhome")
Create salesforce-refresh-token.txt in your home folder, such as C:\Users\your-name or /Users/your-name, and put the refresh token from the previous stage in it. Restrict access to the file. Each refresh response replaces its token, so this script saves the new one for the next run.
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import groovy.json.JsonSlurper
String clientId = System.getenv('SF_CLIENT_ID')
String clientSecret = System.getenv('SF_CLIENT_SECRET')
assert clientId && clientSecret : 'Set SF_CLIENT_ID and SF_CLIENT_SECRET'
File tokenFile = new File(System.getProperty('user.home'), 'salesforce-refresh-token.txt')
String refreshToken = tokenFile.text.trim()
RequestObject request = (RequestObject) findTestObject('Object Repository/Retrieve Bearer Token using Refresh Token',
[('clientId'): clientId, ('clientSecret'): clientSecret, ('refreshToken'): refreshToken])
ResponseObject response = WS.sendRequest(request)
WS.verifyResponseStatusCode(response, 200)
Map tokens = (Map) new JsonSlurper().parseText(response.getResponseText())
String instanceUrl = tokens.get('instance_url')
String accessToken = tokens.get('access_token')
String newRefreshToken = tokens.get('refresh_token')
assert newRefreshToken : 'The response did not contain a rotated refresh token'
tokenFile.text = newRefreshToken
WebUI.openBrowser("${instanceUrl}/secur/frontdoor.jsp?sid=${accessToken}&retURL=%2Flightning%2Fpage%2Fhome")
Do not run test cases that share this refresh token at the same time. The first run replaces it, causing the others to fail.
- Save and run the test case. Salesforce should open without its login page or a verification code. For a manual check, open
https://yourdomain.my.salesforce.com/secur/frontdoor.jsp?sid=followed by a newly issued access token in a private browser window. The access token appears in the Open Browser URL and may be visible in execution logs; restrict access to those logs.
Automate Salesforce test cases​
The sample project still opens the Salesforce login page and uses USERNAME and PASSWORD in its Default profile. Adapt its test cases before running them with the token-based login above:
-
Go to Project > Settings > Test Design > Web UI and change the priority of XPath locators, if the recorded objects need it.
-
If Chrome shows a Show Notification prompt, go to Project > Settings > Desired Capabilities > WebUI > Chrome. Set
prefsto a dictionary containingprofile.default_content_setting_values.notificationswith integer value2. See Set up Desired Capabilities for WebUI Testing. -
Use the Record Web utility in Katalon Studio to record Salesforce actions, then save the objects and test cases.
-
At the start of each test case, replace the recorded browser-opening and username/password login steps with a call to the access-token test case. In Script view, add:
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.callTestCase(findTestCase('Test Cases/Open Salesforce with Access Token'), [:])Adjust the repository path if you saved the test case in a folder. Keep the Salesforce actions after this call.
Troubleshooting​
| Problem | Solution |
|---|---|
| After login, the browser shows the Salesforce home page and no code. | Set the callback URL to /services/oauth2/success as described in Create a Salesforce app. |
The authorization request returns 200 and a login page instead of 302. | Clear Follow redirects in the request. |
| The address bar shows an error about a code challenge. | Follow the PKCE step in Get a refresh token for each test user. |
redirect_uri_mismatch | Match redirect_uri exactly to the app's callback URL. |
invalid_grant with expired authorization code | Get a new code and send the token request right away; each code works once. |
The token response has no refresh_token. | Add Perform requests at any time (refresh_token, offline_access) to the app's scopes. |
invalid_grant when you refresh a token a second time | If your app uses rotation, use the Rotating refresh token script in Open Salesforce with an access token. |