2

我正在尝试创建一个函数来为用户从 Auth0 获取登录令牌,因此我不必在每个测试场景之前使用登录测试(无论如何这都不起作用),而是我想要一个存储令牌并使用它来验证用户,以便我可以测试应用程序。我不是开发人员(甚至不是测试中的开发人员)。我是一名 QA,正在努力学习足够的 Javascript,以便使用 Cypress 为我们新的内部风险评估应用程序创建测试场景。我们有一个新应用的用户列表,所有这些都将通过 Auth0 进行验证。所有用户都是我们公司的内部用户,并且基于我们链接到 Microsoft 帐户的电子邮件。

下面是我按下登录按钮的登录测试,然后将其重定向到 Auth0,然后输入我的电子邮件地址以验证登录。这是成功的,只是它实际上并没有加载应用程序。

```
    describe('My Login Test', function (){
    it('Visit Risk App Landing Page', function (){
        const typedText = 'adam.allford@landmark.co.uk'
        cy.visit('https://bvt-riskassessment.lmkcloud.net')
        cy.get('button').click()
        cy.get('input.auth0-lock-input').first()
        .type(typedText)
        .should('have.value', typedText)
        cy.get('button').click()
        cy.url().should('eq','http://bvt-riskassessment.lmkcloud.net/workflow')
         })
    })
```

我在 Gitter 论坛上收到了一个有类似问题并使用下面显示的内容(或类似内容)尝试登录的人的回复。我用我需要的相关详细信息对其进行了编辑,并将其与所示位置中的 loginuser.json(包含用户名和密码)一起放入 command.js,然后将 beforeEach 包含在测试场景中。

```

    Cypress.Commands.add('login', (userType, options = {}) => 
    {cy.readFile(`cypress/fixtures/loginUser.json`).then((json) => {
    const { email, password } = json
       const dataToSend = {
           email,
           password,
       }
       cy.request({
           url: `https://lmcorp.eu.auth0.com/userinfo`,
           method: 'POST',
           body: dataToSend,
           form: true
       }).then((response) => {
           const { status, body } = response
           expect(status).to.eq(200)
           expect(body).to.have.property('success', 1)
           cy.visit(url)
      })
    })

    //and use it like :
    beforeEach(() => { login('url') })
```

…然后将 beforeEach 包含在测试场景中。

```
    describe('My First Test', function (){
    it('Visit Risk App Landing Page', function (){
        beforeEach(() => { login('https://lmcorp.eu.auth0.com/')})
        cy.visit('http://localhost:3000/workflow')
        cy.contains('Site Solutions Combined Assessments')
        cy.contains('To Do')
        cy.contains('Assessing')
        cy.contains('Reviewing')
        cy.contains('Done')
        cy.get('button').click()
        cy.contains('Assessments')
        cy.contains('Site Solutions Combined')
        cy.contains('Flood')
        cy.contains('Whatever Next')
    })
    })

```

但我在命令控制台上收到以下消息。

![替代] https://i.imgur.com/cJljZzm.png

我完全被困住了,不知道从这里去哪里。我的问题是:我想创建一个功能来调用我们的 Auth0 url 并获取登录身份验证令牌,该令牌可用于允许访问每个测试场景的应用程序。我可以更改此处的内容以使其正常工作,还是有人对如何创建新功能以获取 Auth0 令牌有任何建议?

4

0 回答 0