简体   繁体   English

如何拦截网络请求并使用赛普拉斯检查其 requestHeaders

[英]How to intercept a networkrequest and check its requestHeaders with Cypress

I am working with Cypress and I am testing the FrontEnd.我正在与赛普拉斯合作,我正在测试前端。

I would like to check the requestHeaders of the following networkcall.我想检查以下网络调用的 requestHeaders。 The call is already been intercepted and stubbed namely AssessmentStub:该调用已被拦截并存根,即 AssessmentStub:

From the browserDevTools you can see following: I want to assert the x-classification and its value that you can find in the request Headers.在 browserDevTools 中,您可以看到以下内容:我想断言您可以在请求标头中找到的 x 分类及其值。 在此处输入图像描述

This call is a GET call requested by the webaplication.此调用是 webaplication 请求的 GET 调用。 I am testing the UI and want to check if it is requesting the correct x-classification in the request Header.我正在测试 UI 并想检查它是否在请求 Header 中请求正确的 x 分类。

The code looks like the following now, but it is not working:代码现在如下所示,但它不起作用:

       it.only('Is the Frontend requesting the correct API-Endpoint after changing the Classification', () => {
    cy.get('app-assessment-tests-header > app-classification-selector').click()
    cy.intercept('GET', '**/api/assessmenttestreference').as('AssesmentStub')

    cy.get('.popup').contains('HAVO').click()

    cy.get('@AssesmentStub').then((request) => {
        expect(Request.Headers).to.have.property('x-classification', 'f0651c9a-649b-4217-a85f-ce5c79f0d773')
    })

});

You can try this:你可以试试这个:

cy.wait('@AssesmentStub')
  .its('request.headers')
  .should(
    'have.property',
    'x-classification',
    'f0651c9a-649b-4217-a85f-ce5c79f0d773'
  )

The issue with the code you provided is that the cy.get() yields the entire network call, including request and response.您提供的代码的问题是cy.get()产生整个网络调用,包括请求和响应。 The following code should get you closer.下面的代码应该让你更接近。

it.only('Is the Frontend requesting the correct API-Endpoint after changing the Classification', () => {
    cy.get('app-assessment-tests-header > app-classification-selector').click()
    cy.intercept('GET', '**/api/assessmenttestreference').as('AssesmentStub')

    cy.get('.popup').contains('HAVO').click()

    cy.get('@AssesmentStub').then((call) => {
        expect(call.request.Headers).to.have.property('x-classification', 'f0651c9a-649b-4217-a85f-ce5c79f0d773')
    }) // use `call.request` instead of `request`
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM