Skip to content

Instantly share code, notes, and snippets.

@kevinold
Created August 31, 2021 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinold/f1520d8f95651c7c6ef05258511c904b to your computer and use it in GitHub Desktop.
Save kevinold/f1520d8f95651c7c6ef05258511c904b to your computer and use it in GitHub Desktop.
const { SF_username, SF_password, SF_client_id, SF_client_secret, SF_security_token } = Cypress.env()
describe('Programmatically log in to SF app', () => {
it('makes XHR req & loads dashboard', () => {
Cypress.log({name: 'loginViaOAuth'});
// request access token from SF OAuth endpoint
const oAuthTokenOptions = {
method: 'POST',
url: 'https://login.salesforce.com/services/oauth2/token',
body: {
grant_type: "password",
client_id: SF_client_id,
client_secret: SF_client_secret,
username: SF_username,
password: `${SF_password}${SF_security_token}`
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:89.0) Gecko/20100101 Firefox/89.0'
}
}
cy.request(oAuthTokenOptions)
.then(({ body }) => {
const { access_token, instance_url } = body
// Add access_token as "sid" to "secur/frontdoor.jsp" per community suggestion
// https://github.com/cypress-io/cypress/issues/2367#issuecomment-584058927
cy.request(`${instance_url}/secur/frontdoor.jsp?sid=${access_token}`)
cy.setCookie('sid', access_token, {
"path": "/",
"secure": true,
"httpOnly": false,
"domain": "mindful-koala-78jg04-dev-ed.lightning.force.com",
"sameSite": "no_restriction"
})
cy.visit(`${instance_url}/lightning/page/home`)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment