Skip to content

Instantly share code, notes, and snippets.

@Nxtra
Created October 24, 2020 15:48
Show Gist options
  • Save Nxtra/6fd5bcbdda91e2dc25e8cc8840d99d97 to your computer and use it in GitHub Desktop.
Save Nxtra/6fd5bcbdda91e2dc25e8cc8840d99d97 to your computer and use it in GitHub Desktop.
Create a Cypress command to signIn with AWS Amplify before executing your tests! Repository: https://github.com/Nxtra/cypress-amplify-auth-test
const Auth = require("aws-amplify").Auth;
import "cypress-localstorage-commands";
const username = Cypress.env("username");
const password = Cypress.env("password");
const userPoolId = Cypress.env("userPoolId");
const clientId = Cypress.env("clientId");
const awsconfig = {
aws_user_pools_id: userPoolId,
aws_user_pools_web_client_id: clientId,
};
Auth.configure(awsconfig);
Cypress.Commands.add("signIn", () => {
cy.then(() => Auth.signIn(username, password)).then((cognitoUser) => {
const idToken = cognitoUser.signInUserSession.idToken.jwtToken;
const accessToken = cognitoUser.signInUserSession.accessToken.jwtToken;
const makeKey = (name) =>
`CognitoIdentityServiceProvider.${cognitoUser.pool.clientId}.${cognitoUser.username}.${name}`;
cy.setLocalStorage(makeKey("accessToken"), accessToken);
cy.setLocalStorage(makeKey("idToken"), idToken);
cy.setLocalStorage(
`CognitoIdentityServiceProvider.${cognitoUser.pool.clientId}.LastAuthUser`,
cognitoUser.username
);
});
cy.saveLocalStorage();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment