英文:
How Do I Preserve My AWS Amplify Login When Running Cypress Tests?
问题
以下是代码部分的翻译:
describe('Candidate Pages', () => {
beforeEach(() => cy.loginByCognito(Cypress.env("candidate_username"), Cypress.env("candidate_password")));
candidateRoutes.forEach((page) => {
it(`No Console Errors for ${page}`, () => checkConsole(page, "error"));
it(`No console.warn for ${page}`, () => checkConsole(page, "warn"));
});
});
英文:
Currently, I have a beforeEach()
that logs cypress in before the test starts. However, this is rather slow. How do I sustain that logged in user, between tests?
My goal is to navigate through all the pages of the logged in user, and to check for console warnings and errors.
Code:
describe('Candidate Pages', () => {
beforeEach(() => cy.loginByCognito(Cypress.env("candidate_username"), Cypress.env("candidate_password")));
candidateRoutes.forEach((page) => {
it(`No Console Errors for ${page}`, () => checkConsole(page, "error"));
it(`No console.warn for ${page}`, () => checkConsole(page, "warn"));
});
});
答案1
得分: 2
在你找到 cy.loginByCognito()
的页面上,你会看到以下会话示例:
最后,我们可以重构我们的登录命令,利用 cy.session() 来存储已登录的用户,这样我们就不必在每个测试中重新进行身份验证。
如果你还没有读到这一部分,我建议你添加它。
// cypress/support/auth-provider-commands/cognito.ts
// Amazon Cognito
Cypress.Commands.add('loginByCognito', (username, password) => {
cy.session(
`cognito-${username}`,
() => {
return loginToCognito(username, password)
},
{
validate() {
cy.visit('/')
// 重新验证我们的会话,确保我们已登录
cy.contains('Get Started').should('be.visible')
},
}
)
})
英文:
If you look on the same page where you found cy.loginByCognito()
, you see the session example
> Lastly, we can refactor our login command to take advantage of cy.session() to store our logged in user so we don't have to reauthenticate with everything test.
If you didn't read that far, I suggest you add it in.
// cypress/support/auth-provider-commands/cognito.ts
// Amazon Cognito
Cypress.Commands.add('loginByCognito', (username, password) => {
cy.session(
`cognito-${username}`,
() => {
return loginToCognito(username, password)
},
{
validate() {
cy.visit('/')
// revalidate our session to make sure we are logged in
cy.contains('Get Started').should('be.visible')
},
}
)
})
答案2
得分: 1
这与任何设置令牌的登录相同,使用 cy.session()
一旦创建,对于给定 id 的会话将在规范文件的整个持续时间内进行缓存。
const login = (name) => {
cy.session(name, () => {
cy.visit('/login')
cy.get('[data-test=name]').type(name)
cy.get('[data-test=password]').type('s3cr3t')
cy.get('#submit').click()
cy.url().should('contain', '/home')
})
cy.visit('/home')
}
beforeEach(() => {
login('user')
}
此外,
要在多个规范之间保留会话,使用选项 cacheAcrossSpecs=true
。
在这种情况下,请将代码移至 /support/e2e.js
。
英文:
It's the same as any login that sets a token, use cy.session()
> Once created, a session for a given id is cached for the duration of the spec file.
const login = (name) => {
cy.session(name, () => {
cy.visit('/login')
cy.get('[data-test=name]').type(name)
cy.get('[data-test=password]').type('s3cr3t')
cy.get('#submit').click()
cy.url().should('contain', '/home')
})
cy.visit('/home')
}
beforeEach(() => {
login('user')
})
Also
> To persist a session across multiple specs, use the option cacheAcrossSpecs=true.
In this case, move the code to /support/e2e.js.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论