Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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