Build a Simple Salesforce API Test Automation Framework
In this tutorial blog post, we will learn how to build a simple Salesforce API test automation framework using Rest Assured-Java, Maven and TestNG.
Let’s get started right away. Log into to your Salesforce environment (I am using my trailhead org account for this tutorial). After logging in, the first thing you need to do is reset your Salesforce security token.
Got to Profile > Settings > Quick Find > Type “Reset” > Reset My Security Token
Security token will be emailed to your registered email address. Copy this token and keep it safe for it to be used later.
Now we need to create a Connected App in Salesforce. A connected app enables external applications to interact with Salesforce using APIs. In our case, the external application is the test framework
To create a Connected App, Go to Set up > Apps > App Manager > New Connected App.
On the New Connected App page, fill the following required fields under Basic Information.
- Connected App Name. For example, Test Automation Integration.
- API name. For example, Test Automation Integration.
- Contact Email.
- Go to API (Enable OAuth Settings), and select Enable OAuth Settings.
- In the Callback URL field, enter https://login.salesforce.com/.
- In the Selected OAuth Scopes field, select Full Access, and then click Add.
- Save > Continue
- Copy Consumer Key and Consumer Secret and keep it safe for it to be used later.
- Manage > Edit Policies > Ip Relaxation > Relax IP restrictions > Save.
With that, we are done with the Salesforce configuration. Now let’s code.
Open Visual Studio Code. Open Extensions and install Java Extension pack. It comes with Maven and Java Test Runner pre-installed. Restart VS Code.
To create a Maven Project, click the + icon on Maven tab.
Create Maven Project > architype-quickstart-jdk 8 > Enter > Select destination folder.
Enter Snapshot, GroupId and ArtifactId if requested.
Open the newly created Maven project folder.
Open pom.xml. You will see Junit dependency added as default. We will be using TestNG instead, so let’s replace it with TestNG.
Open Rest-Assured maven dependency website: https://mvnrepository.com/artifact/io.rest-assured/rest-assured, find the latest version. Copy the Maven dependency and paste it in pom.xml
Repeat the same steps for TestNG dependency (https://mvnrepository.com/artifact/org.testng/testng) and for JSON dependency (https://mvnrepository.com/artifact/org.json/json)
Remove <scope>test</scope> from the dependencies.
Delete the dummy classes in main and test folders.
Under src > main, create a new class named EstablishSalesforceConnection.java
This class establishes a connection to Salesforce using the credentials passed as parameters and then asserts that the connection is successful (200).
Now let’s write the Rest-Assured code to test the connection to Salesforce via the Connected App we created.
EstablishSalesforceConnection.java will have the below code
To test this, create a test class under src > test named TestConnection.java
TestConnection.java class will have the below code.
Click on the play/run button next to the @Test annotation to run the test. Upon successful test execution, on the Debug Console can see the below output.
We have successfully made a connection to Salesforce from our framework.
Copy the instance_url from the response for it to be used later.
It’s now time to code the actual test. We will be inserting an account, a contact and an opportunity in Salesforce and asserting field values of the inserted records.
If you open Account object in Salesforce, you can see Account is a standard object with API name Account__c. The only required field on Account object is Account Name with API name Name. In our test, let’s also add a phone number to the record using the field Phone.
First, let’s write the code to insert an Account record in Salesforce.
Under src > main, create a new class named InsertAccount.java
InsertAccount.java will have the below code.
After logging in, API requests should be made to the instance url and not login url. For example, my instance url here is
https://afsal-dev-ed.my.salesforce.com , so I replaced https://login.salesforce.com in the POST request with my instance url.
To test account insertion, create a test class under src > test with the name TestSalesInsert.java
TestSalesInsert class will have the below code.
Output:
As a next step, let’s add a Contact to this Account record.
Last Name is the only required field for Contact. To link the new contact to the account record, Account Name lookup field should be populated with the account record’s Id we created earlier. Contact object fields API names are AccountId and LastName.
Under src > main, create a new class named InsertContact.java
InsertContact.java will have the below code.
Notice that the only thing that changed here is the POST request URL and the object required fields. We can insert any object record by passing the object name in the URL and values to its required fields.
To insert an opportunity, under src > main, create a new class named InsertOpportunity.java
Required fields of Opportunity object are Account id, Opportunity name, Opportunity stage and Closed date.
InsertOpportunity.java will have the below code.
Now let’s add the code to test contact and opportunity insertion in TestSalesInsert.java class.
Now let’s run the test and check the output.
Output:
We have successfully inserted an Account, a Contact and an Opportunity and linked it together. Open the Account record, Contact record and Opportunity record and manually verify the records are linked to each other.
- Verify Account has the supplied Account name.
- Verify Contact has the supplied Account name.
- Verify Opportunity has the supplied Stage name.
Now let us convert the above manual steps into API tests. We will be verifying these fields using SOQL. For that, let’s create a class TestRecordsUsingSOQL.java under src > main.
TestRecordsUsingSOQL.java will have the following code.
Parameter soql is the SOQL query to execute and parameter fieldName is the object’s field name from which field value has to be extracted. In the GET request, we pass the SOQL query. From the response, required field value is fetched and stored in String fieldValue.
We can now call this method from our test class TestSalesInsert.java by appending the below code.
As you can see, we are passing three different SOQL queries for Account, Contact and Opportunity which extracts Account name, Contact name and Opportunity Stage name. We then assert the actual and expected values.
Run the test.
Output:
Test inserted the records and used SOQL queries to assert actual result and expected result.
With that we are done with the framework. You can now go ahead and improve the reusability and maintainability of the framework by creating a separate class for API endpoints and SOQL queries, add a reporting feature, etc.
You can download the project code from my Github repo:
https://github.com/afsal-backer/Salesforce-API-Test-Automation-Framework
Tutorial video: https://youtu.be/H21mWe4UFr8