Following Cypress recommendations we use direct api calls to login in most of our tests - basically if it’s not explicitly testing the login UI we bypass it, it’s much faster that way and we tend to login rather a lot to ensure we have good role based test coverage.
Based on examples in threads like this one https://github.com/cypress-io/cypress/issues/4416, we have this login code:
cy.request({
method: 'POST',
url: `${OKTA_URL}/api/v1/authn`,
body: {
username,
password,
options: {
warnBeforePasswordExpired: 'true',
},
},
})
.then((resp) => {
const sessionToken = resp.body.sessionToken
const qs = {
client_id: OKTA_CLIENT_ID,
scope: 'openid',
nonce: getRandomString(),
state: 'test', // fyi state is used to link requests and responses, we don't care here
code_challenge: 'test',
redirect_uri: OKTA_REDIRECT_URI,
response_type: 'id_token token',
sessionToken: sessionToken,
}
cy.request({
method: 'GET',
url: `${OKTA_URL}oauth2/default/v1/authorize`,
form: true,
followRedirect: false,
qs,
}).then((respWithToken) => {
const url = respWithToken.redirectedToUrl
cy.visit(url).then(() => {
cy.visit('/')
})
})
})
.then((res) => {
Cypress.log({ message: `okta login success as ${username}` })
})
And it works, or at least it works in Electron and Firefox.
In Chrome, it logs in, then the screen refreshes and we’re back at the okta login page.
I should add that the UI is using a pretty bar oidc-client implementation and not the okta-js pacakge if that’s relevant.
Any suggestions?