amazon-cognito - Cypress AWS Cognito 集成

标签 amazon-cognito cypress

Cypress 有没有办法与 Cognito 集成?我有一个没有登录页面的应用程序,但使用来自另一个具有 Cognito 登录名的网站的 cookie。 (使用cookies)

无论如何,是否可以在不访问该页面的情况下从 3rd 方应用程序进行“登录”?我还尝试向登录端点发出 API 请求,但它也给了我一个跨域问题。

任何想法表示赞赏!

最佳答案

Cypress 提供了创建 custom commands 的能力,可用于执行 Amplify/Cognito 命令。例如,在测试经过身份验证的页面时,我使用登录命令而不是使用 UI 登录,因为这被视为 anti pattern .
如果您能够获取 cognito 配置,则可以使用自定义命令执行登录,如下所示。
在 cypress/support/commands.js 中创建自定义命令

import Amplify, { Auth } from 'aws-amplify';
Amplify.configure({
  Auth: {
    mandatorySignIn: true,
    region: "eu-west-1",
    userPoolId: Cypress.env("userPoolId"),
    identityPoolId: Cypress.env("identityPoolId"),
    userPoolWebClientId: Cypress.env("appClientId"),
    oauth: {
        domain: Cypress.env("domain"),
        scope: ['email', 'profile', 'aws.cognito.signin.user.admin', 'openid'],
        redirectSignIn: Cypress.env("redirect"),
        redirectSignOut: Cypress.env("redirect"),
        responseType: 'code',
        options: {
            AdvancedSecurityDataCollectionFlag: false
        }
    }
  }
});

Cypress.Commands.add("login", (email, password) => {
  return Auth.signIn(email, password)
      .then(user => {
        console.log('===> user', user);

        let session = Auth.currentSession();

        console.log('===> session', session);
      })
      .catch(err => console.log('===> err', err));
})
在规范文件中使用 cy.login() 命令:
describe('Authenticated page test', () => {
  it('should be logged in', () => {
    cy.login('test.user@email.com', 'Password')
    
    cy.visit('/')
    cy.contains('some page content')
  })
})

关于amazon-cognito - Cypress AWS Cognito 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62179859/

相关文章:

javascript - 使用 Cypress 在 SVG 上测试 onclick 事件

javascript - TypeError : selector. 拆分不是 Cypress 的函数

javascript - 在 javascript 中将响应正文 blob 转换为 json 或纯文本

javascript - Cypress 通过包含的文本过滤选定的元素

amazon-web-services - AWS Cognito Google 身份提供商获取个人资料名字和姓氏

amazon-web-services - 更改 cognito 用户池用户状态

ios - 为什么使用开发者身份或第三方通过后端进行身份验证

ios - swift: 在将 facebook 当前 token 发送到 AWS Cognito 后接收 AWS id_token?

javascript - AWS Cognito 身份池 : AWS Credentials Expiration/Renewal

continuous-integration - 如何重试失败的测试?