【问题标题】:How to log cypress.io, cy.request into a file如何将 cypress.io、cy.request 记录到文件中
【发布时间】:2019-10-01 08:07:14
【问题描述】:

我有以下工作代码来记录响应。现在,试图找出一种记录请求的方法。我们如何记录它?它只是返回未定义。

我尝试在 cy.request 上设置 log=true,但它只在浏览器控制台上记录。最好写入文件。

it('Query Endpoint', () => {
    request = cy.request(
        {
            method: 'POST',
            url: '/api/service', 
            auth: {
                username: Cypress.env('username'),
                password: Cypress.env('password')
            },
            headers: {
                'Content-Type': 'application/json',
            },
            body: {},
            log: true
        })
    .should((response) => {
        
        cy.log(request.body) // <-- how can you log the request?
        cy.writeFile('cypress/responses/api-service.json', request.body) // <-- how can you log the request?

        expect(response.status).to.eq(200)
        cy.writeFile('cypress/responses/api-service.json', response.body)
    })

})

感谢任何帮助。

【问题讨论】:

    标签: javascript json cypress


    【解决方案1】:

    您可能需要使用.then 而不是should,并且request 应该在promise 内部解析。它正在夹具文件夹中为我编写一个文件。但在答案中,我留下的答案与您的问题中给出的 path 相同。

    it('Query Endpoint', () => {
            cy.request(
                {
                    method: 'POST',
                    url: '/api/service', 
                    auth: {
                        username: Cypress.env('username'),
                        password: Cypress.env('password')
                    },
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: {}
                })
                .then((request) => {
                    const someRequest =  JSON.stringify(request);
                    cy.writeFile('cypress/fixtures/api-service.json', someRequest)
                    //...rest of the code 
              })
    
        })
    

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2019-01-18
      • 2012-03-27
      • 2018-03-13
      • 1970-01-01
      相关资源
      最近更新 更多