Cypress command extension to log into a SSO App through the Microsoft Authentication flow using the MSAL library in the app.
npm install cypress-msal-loginThis package depends on the MSAL browser library to be installed in your application (peerDependency)
``bash`
npm install @azure/msal-browser
Add the cypress package as a dev dependency
`bash`
npm install --dev cypress-msal-login
## Login with MSAL account
The login command requires three parameters. Here is a minimal example:
``
cy.msalLogin(
{
email: 'my-test-user@cypress.com',
password: 'whatever'
},
{
auth: {
clientId: '
authority: 'https://login.microsoftonline.com/
},
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
},
},
['openid', 'profile', 'user', 'offline_access'],
)
- User Credentials
The cypess test user's credentials (email and password). I strongly recommend to create a dedicated Cypress test user with specific rights.
- Configuration
The configuration as provided to your msal instance. If you set the settings using envirenment variables (which you probably should), you might need to adjust the configuration accordingly for Cypress. See example below and take a look at the documentation cypress/environment-variables
`
import { Configuration, BrowserCacheLocation } from '@azure/msal-browser'
const msalConfig: Configuration = {
auth: {
clientId: Cypress.env('MSAL_CLIENT_ID'),
authority: https://login.microsoftonline.com/${Cypress.env('MSAL_TENANT_ID')},`
},
cache: {
cacheLocation: BrowserCacheLocation.LocalStorage,
storeAuthStateInCookie: true,
},
}
The session token (access_token) is exposes as alias sessionToken and you can get it like this:
``
cy.get('@sessionToken').then(sessionToken => {
// ... do whatever you want with it.
})
- Scope
The scope of the token. Make sure you include at least the openid, profile, user and offline_access`