Created
September 8, 2020 13:14
-
-
Save vittorio-nardone/a9b388ff7388a4ae7343cd6a2758bc4b to your computer and use it in GitHub Desktop.
Test AWS Amplify login / logout (Cognito) using Cypress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const labels = { | |
// Auth | |
signInLabel: 'Sign In', | |
signOutLabel: 'Sign Out', | |
signInHeader: 'Sign in to your account', | |
signInResetPassword: 'Reset password', | |
signInCreateAccount: 'Create account', | |
} | |
export const selectors = { | |
// Auth component classes | |
signInSlot: '[slot="sign-in"]', | |
signInHeader: '[data-test="sign-in-header-section"]', | |
signInResetPasswordLink: '[data-test="sign-in-forgot-password-link"]', | |
signInCreateAccountLink: '[data-test="sign-in-create-account-link"]', | |
signInEmailInput: '[data-test="sign-in-email-input"]', | |
signInPasswordInput: '[data-test="sign-in-password-input"]', | |
signInSignInButton: '[data-test="sign-in-sign-in-button"]', | |
signOutButton: '[data-test="sign-out-button"]', | |
} | |
export const login = { | |
username: Cypress.env('username'), | |
password: Cypress.env('password') | |
} | |
function SignIn() { | |
// it is ok for the username to be visible in the Command Log | |
expect(login.username, 'username was set').to.be.a('string').and.not.be.empty | |
// but the password value should not be shown | |
if (typeof login.password !== 'string' || !login.password) { | |
throw new Error('Missing password value, set using CYPRESS_password=...') | |
} | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInEmailInput, { includeShadowDom: true }) | |
.type(login.username, {log: false, force: true}); | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInPasswordInput, { includeShadowDom: true }) | |
.type(login.password, {log: false, force: true}); | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInSignInButton, { includeShadowDom: true }) | |
.contains(labels.signInLabel) | |
.click(); | |
}; | |
describe('Authenticator:', function() { | |
// Step 1: setup the application state | |
beforeEach(function() { | |
cy.visit('/'); | |
}); | |
describe('Sign In', () => { | |
it('check sign in page', () => { | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInHeader, { includeShadowDom: true }) | |
.contains(labels.signInHeader) | |
.should('be.visible'); | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInResetPasswordLink, { includeShadowDom: true }) | |
.contains(labels.signInResetPassword) | |
.should('be.visible'); | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInCreateAccountLink, { includeShadowDom: true }) | |
.contains(labels.signInCreateAccount) | |
.should('be.visible'); | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInEmailInput, { includeShadowDom: true }) | |
.should('be.visible') | |
.should('be.enabled'); | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInPasswordInput, { includeShadowDom: true }) | |
.should('be.visible') | |
.should('be.enabled'); | |
cy.get(selectors.signInSlot) | |
.find(selectors.signInSignInButton, { includeShadowDom: true }) | |
.contains(labels.signInLabel) | |
.should('be.visible'); | |
}); | |
it('allows a user to signin', () => { | |
// Take an action (Sign in) | |
SignIn(); | |
// Make an assertion (Check for sign-out text) | |
cy.get(selectors.signOutButton, { includeShadowDom: true, timeout: 10000 }) | |
.contains(labels.signOutLabel) | |
.should("be.visible"); | |
}); | |
}); | |
describe('Sign Out', () => { | |
it('allows a user to signout', () => { | |
// Take an action (Sign in) | |
SignIn(); | |
// Click on sign-out button | |
cy.get(selectors.signOutButton, { includeShadowDom: true, timeout: 10000 }) | |
.contains(labels.signOutLabel) | |
.click(); | |
// Make an assertion (Check for sign-in button) | |
cy.get(selectors.signInSlot).find(selectors.signInSignInButton, { includeShadowDom: true }) | |
.contains(labels.signInLabel) | |
.should("be.visible"); | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you please signup with mock data it would be really appreciated