How Optimizely used Cypress.io to automate testing within its AppExchange App in Salesforce
By: Mike London
Tags: Salesforce Appexchange, End To End Testing, Cypressio, Optimizely

How Optimizely used Cypress.io to automate testing within its AppExchange App in Salesforce Mike London Follow Oct 30 · 6 min read

This past quarter, Optimizely launched on the Salesforce Appexchange! This was super exciting for the whole team and went smoothly thanks to the e2e testing we had implemented as a Partnerships and QA team. If you’re looking to launch on the AppExchange, it has its own set of challenges, so I wanted to share the Cypress tests I built to do comprehensive testing so you can have a smooth launch on the AppExchange as well! If you have ever been blocked by redirect issues with Cypress, this post should help you out with ways to work around them! The solution may be different but the concepts are still the same!

For those that are unaware of what Cypress.io is, it is primarily an end-to-end (e2e) testing framework used by QA engineers or software developers to write and test their code before shipping these changes to their respective customers. Cypress is integrated directly into your application giving a real feel when running your test suite through a browser of your choice: Chrome, Firefox, or Electron.

In the past, we have primarily used Cypress at Optimizely as a means for regression testing to ensure changes to the system don’t cause previous issues or bugs to reappear. However, we have started to use it to test new features as well due to its ease of use and reliability. We know that if we can test the visual aspects and actions of what our customers will see, we can confidently ship code and features that will deliver a greater experience to them.

In my work as a Software Engineer in Test, I was tasked to pair with our Partnerships team in delivering an e2e test suite on our new product, the Operations Experimentation for Salesforce App (OpX). The new OpX integration makes it easy for customers to run experiments directly in Salesforce Lightning to achieve high-value objectives like quantify the impact of new Salesforce features and test different Salesforce layouts, workflows, and back-end processes.

This was different and exciting for my role because this was the first time I was asked to build a test suite on a product we don’t control (Salesforce). In addition to this, it’s a product being built on a completely different tech stack than our own and by the independent software vendor, CodeScience. In addition to this, I had zero experience working with the Salesforce UI and its authorization flows. We expect all products that have the Optimizely name on them to maintain a high level of quality and OpX is no different. This is where Cypress comes into play.

Cypress makes it very easy to interact with live web pages and getting your first test up and running can take less than 10 minutes. Cypress drove a lot of value by putting a stressor on the product we were delivering. Not only were we able to test the Optimizely Integration itself, but we were able to further validate the Optimizely bucketing mechanism within the integration by running over 10,000 bucketing events through the platform without any veering from expected behavior.

The hardest part of this whole process was the sign inflow. Cypress has a tough time handling cross-domain redirects, which is how Salesforce built it’s login flow. To get around this we had to programmatically log into the Salesforce development environment and store the session data onto the Cypress electron browser. This is done by performing a request (cy.request) to the login endpoint with an access token in hand. We will now go over some steps on how you can get unblocked by the redirect issues.

To do this, you will need to download the SFDX command-line interface (CLI) and connect it to your environment. I used this getting started guide to go through the process.

Once you have installed the CLI to your machine, you can use Cypress to start testing within your Salesforce environment. Here are a couple of ways to do this:

Run an execute command on the Cypress object calling the SFDX CLI instance for your environment. This will warrant you an access token allowing you to directly access the page being the login flow.

Props to @samuel012 on GH for this easy to use solution. https://github.com/cypress-io/cypress/issues/2367#issuecomment-584058927

I had to tweak a couple of the lines, however, this was the general idea we used to approach the testing.

2. If you are getting a malformed request from the above, you can hard-code the access token from the SFDX and create a login function to handle the request (this example uses some environment variables for the values). Within my cypress.json file in the root directory, I added two more properties for the Salesforce accessToken and frontdoorPath.

“salesforceAccessToken”: “<ACCESS TOKEN FROM CLI HERE>”,

“salesforceFrontdoorPath”: “<SALESFORCE DOMAIN HERE>/secur/frontdoor.jsp?sid=”

I created a separate login file to add my login function. This is then requested within the index.js file

Cypress.Commands.add("login", () => { cy.request(Cypress.env("salesforceFrontdoorPath") + Cypress.env("salesforceAccessToken")) });

The one negative of this approach is once the access token expires (Based on your organization’s security settings), you will need to pull a new token from the CLI and paste it into your environment variable. This is not very scalable and potentially makes running in CI/CD impossible.