Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
How to Receive cypress interception fixture twice but in different format?

I'm a beginner with Cypress and I am stuggeling with the following:

I am calling the following from my test file:

cy.SetupClassificationsStubFixture(1);

This refers to the following command:

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')


cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications/*', slicedClassifications)
}).as('classifications') });

As you can see this customcommand is intercepting 2 api request. Namely:

  1. ** /api/classifications
  2. ** /api/classifications/ *

The first interception is with [] The second intercecption needs exactly the same response buth without brackets []

But you already understand that both respons are now with brackets '[]'. I tried to make a second fixture file without [], but the the slice function is not working.

So I need: The second intercept like:

{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}

But I get:

[{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}]

How could I get this array back without []? Thankyou indeed!

UPDATE: I am trying now the following:

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')


cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    const arr2 = slicedClassifications
    const obj5 = Object.fromEntries(arr2)
    
   
    cy.intercept('GET', '**/api/classifications/*', obj5)
}).as('classification')});

But this give me a emtpy .json file. It the respons is not just ' {} '

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

There looks to be some brackets out of place in the code.

I would also recommend using different alias names, otherwise the calls to cy.wait('@classifications') may not work as you expect.

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

  cy.fixture('ClassificationsStub.json').then(classificationsStub => {

    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
      .as('classifications')

    const firstClassification = slicedClassifications[0]  // just first item
    cy.intercept('GET', '**/api/classifications/*', firstClassification)
      .as('firstClassification') 

  })
});
about 4 years ago · Santiago Trujillo Relatório

0

So in case, your data looks like this:

var data = [{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}]

So to extract the data with just curly braces you can do data[0].

{
    "id": "9d4a9c14-ef37-4a64-a1eb-63ab45cdf530",
    "name": "CypressTest",
    "userRole": "Editor",
    "childClassifications": []
}

Working example console screenshot:

console sccreenshot

about 4 years ago · Santiago Trujillo Relatório

0

Finally solved with:

Cypress.Commands.add("SetupClassificationsStubFixture", (amount) => {

cy.fixture('ClassificationsStub.json').then(classificationsStub => {
    const slicedClassifications = classificationsStub.slice(0, amount)
    cy.intercept('GET', '**/api/classifications', slicedClassifications)
}).as('classifications')


cy.fixture('ClassificationsStub.json').then(classificationsStub => {
   
    cy.intercept('GET', '**/api/classifications/*', (req) => {
        const slicedClassifications = classificationsStub.slice(0, amount)
        var selectedClassification = req.url.replace(new RegExp('.*api/classifications/'), '');
        let foundClassification = FindChildClassification(selectedClassification, slicedClassifications);

        //If not found return first always
        req.reply({
                    statusCode: 200,
            body: foundClassification ?? slicedClassifications[0]
                })
    })
}).as('classification')

And

export function FindChildClassification(id, classificationArray) {
for (let i = 0; classificationArray.length - 1 >= i; i++) {
    if (classificationArray[i].id == id) {
        return classificationArray[i];
    } else {
        if (classificationArray[i].childClassifications.length > 0) {
            let childClassification = FindChildClassification(id, classificationArray[i].childClassifications);
            if (childClassification != null) { return childClassification }
        }
    }
}

return null;

}

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda