如何在Cypress中使用while循环?运行此规范文件时,控件是否未进入循环?我轮询任务的方式正确吗? [英] 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?

查看:156
本文介绍了如何在Cypress中使用while循环?运行此规范文件时,控件是否未进入循环?我轮询任务的方式正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我轮询异步POST呼叫的任务的方式是否正确???因为程序控制未在spec文件中输入"while"循环.请帮忙!上一个查询:如何从赛普拉斯自定义命令中返回值

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)
    })

})  

推荐答案

while循环对我不起作用,因此,作为一种解决方法,我做了一个for循环,一种while循环,但重试超时

while loop is not working for me, so as a workaround I did a for loop, a sort of while loop with a timeout of retries

let found = false
const timeout = 10000
for(let i = 0; i<timeout && !found;i++){
      if(..){
           // exiting from the loop
           found = true
      }
}

这对所有人都没有帮助.

it is not helpful for everyone, I know.

这篇关于如何在Cypress中使用while循环?运行此规范文件时,控件是否未进入循环?我轮询任务的方式正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆