How to use a while loop in cypress? The control of is NOT entering the loop when running this spec file? The way I am polling the task is correct?

守給你的承諾、 提交于 2020-07-23 07:02:46

问题


The way i am polling tasks for async POST call, is it correct??? Because program control doesn't enter 'while' loop in spec file. Please help! Previous query: How to return a value from Cypress custom command

beforeEach(function () {
    cy.server()
    cy.route('POST', '/rest/hosts').as("hosts")
})


it('Create Host', function () {

    let ts =''
    let regex = /Ok|Error|Warning/mg 

    // Some cypress commands to create a host. POST call is made when I create a host. I want to poll 
    // task for this Asynchronous POST call.

    cy.wait("@hosts").then(function (xhr) {
        expect(xhr.status).to.eq(202)
        token = xhr.request.headers["X-Auth-Token"]
        NEWURL = Cypress.config().baseUrl + xhr.response.body.task
    })


    while((ts.match(regex)) === null) { 
        cy.pollTask(NEWURL, token).then(taskStatus => {
        ts= taskStatus
        })
    }
})

-------------------------

//In Commands.js file, I have written a function to return taskStatus, which I am using it in spec 
 file above

Commands.js -

Cypress.Commands.add("pollTask", (NEWURL, token) => {

    cy.request({
        method: 'GET',
        url: NEWURL ,
        failOnStatusCode: false,
        headers: {
            'x-auth-token': token
        }
    }).as('fetchTaskDetails')


    cy.get('@fetchTaskDetails').then(function (response) {
        const taskStatus = response.body.task.status
        cy.log('task status: ' + taskStatus)
        cy.wrap(taskStatus)
    })

})  

来源:https://stackoverflow.com/questions/61591716/how-to-use-a-while-loop-in-cypress-the-control-of-is-not-entering-the-loop-when

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!