Home > database >  Unable to get an alert after the API response
Unable to get an alert after the API response

Time:08-12

I'm trying to get the alert to assert the text, but the problem is that when the cy.get(.alert) command is executing the alert is not visible anymore for some reason, even tho it's displayed on the page for several seconds, and after the cy.get(.alert)

The runner: At cy.wait enter image description here At cy.get('.alert') enter image description here After cy.get('.alert') enter image description here HTML enter image description here

My code:


   cy.intercept('POST', Cypress.env('backdev3URL') '/bap/one_sst_bap/create').as('createResponse')
    cy.readFile('cypress/fixtures/BAPdata.json').then(BAPdata => {
        cy.readFile('cypress/fixtures/PurchaseData.json').then(PurchaseData => {
            
            cy.wait('@tableRequest')
           
            cy.get('#subcontractors_datatable tbody').find('tr td:nth-child(1)').each((row,index) => {
                if(row.text().includes("ABJ MENUISERIE")){
                    cy.get('#subcontractors_datatable tbody tr').eq(index).find('td:nth-child(2)').then(Nb => {
                        BAPdata.BAPnumber = Nb.text()
                        cy.writeFile('cypress/fixtures/BAPdata.json', BAPdata)
                    })
                    cy.get('#subcontractors_datatable tbody tr').eq(index).contains('button', 'BAP').click()   
                }
            })
            cy.wait('@createResponse').then(() => {
                cy.get('.alert').should('contain',
                'Le BAP N°' PurchaseData.BDCid year month day ' pour le sous traitant avec ID ' PurchaseData.BDCid ' a été créé avec succés')
            })
          
            var date = PurchaseData.BDCdate.split(' ')[0].replace(/\//g, ' ')
            var day = date.split(' ')[0] '1'
            var month = date.split(' ')[1]
            var year = date.split(' ')[2].replace(/20/, '')
            const creationDate = require('dayjs')

            BAPdata.BAPid = Number(BAPdata.lastBAPid) Number(1)  
            BAPdata.SstBAPnumber = PurchaseData.SSTid year month day
            BAPdata.BAPdate = creationDate().format('DD/MM/YYYY')

            cy.writeFile('cypress/fixtures/BAPdata.json', BAPdata)


        })
    })
                  

Thanks!

CodePudding user response:

The assertion is failing because there are differences in the data displayed in the alert

  • PurchaseData.BDCid is 642705 in the fixture but 20577 in the alert

  • PurchaseData.BDCid year month day has some undefined values

You could probably just check part of the string to ensure the alert occurs and is the right one.

cy.get('.alert')
  .should('contain', 'pour le sous traitant avec ID')  // enough to identify 

But if you want to test with the actual data, you should intercept and stub the POST request with the same values in the fixture PurchaseData.json.

Also get rid of cy.wait(1000) before cy.get('.alert').

  • Related