How to integrate Python Automation tests with SalesForce

Reference Sources:

Python and Salesforce

https://pypi.org/project/simple-salesforce/

I. Context

My project is using SalesForce solution for Customer Relationship Management. It manages the customer information, marketing campaigns and the way communicate with customers.

For example, when a new user completes the signup flow, all info of that user such as First name, Last Name, Email, Phone, billing address will be sent to SalesForce.

When automating Sign up scenario, automation test also needs to verify that all info of new Signup User are available on Salesforce.

There is a Python library called Simple-Salesforce which help us easy to access SalesForce and query data needed for the test.

Simple Salesforce is a basic Salesforce.com REST API client built for Python 3.5, 3.6, 3.7 and 3.8. The goal is to provide a very low-level interface to the REST Resource and APEX API, returning a dictionary of the API JSON response.

https://github.com/simple-salesforce/simple-salesforce

II. Setup

What you need to open a connection to SalesForce to access its data:
1. Python module simple_salesforce
2. SalesForce credentials (username, password, security_token)

from simple_salesforce import Salesforcesf = Salesforce(
username='myemail@example.com', 
password='password', 
security_token='token')

You can easily get username and password in your profile setting.

For security token, if you don’t have it yet, you can reset it :

Navigate to the Account settings. under My Personal Information, select Reset My Security Token and click “Reset Security Token” This will be sent to you in the form of an email with an alphanumeric code.

Save this, we’ll need it in the next steps. Let’s start building our Python script.

Get all the information regarding to a Contact:

from simple_salesforce import Salesforce
sf = Salesforce(username='myemail@example.com', password='password', security_token='kdjfghdgfFGJbDFgd36DFGHDfgh')
contact = sf.Contact.get('003e0000003GuNXAA0')
# you also can update or delete SalesForce data
sf.Contact.update('003e0000003GuNXAA0',{'LastName': 'Jones', 'FirstName': 'John'})
sf.Contact.delete('003e0000003GuNXAA0')

If you’d like to enter a sandbox, simply add domain=’test’ to your Salesforce() call.

from simple_salesforce import Salesforce
sf = Salesforce(username='myemail@example.com.sandbox', password='password', security_token='kdjfghdgfFGJbDFgd36DFGHDfgh', domain='test')

Queries

It’s also possible to write select queries in Salesforce Object Query Language (SOQL) and search queries in Salesforce Object Search Language (SOSL).

sf.query("SELECT Id, Email FROM Contact WHERE LastName = 'Jones'")

For more usage and details, please refer official document here: https://pypi.org/project/simple-salesforce/

Leave a comment