【问题标题】:How do I wait for value to be set in a cypress?我如何等待在柏树中设置值?
【发布时间】:2021-09-15 16:40:47
【问题描述】:

我遍历一个表并遍历这些表的数据以比较它们。但是,如果其中一个表数据不匹配,我会尝试使用 break 来防止进一步检查。

我在这里遇到了一个问题,因为中断将在 cy.then 内部发生,这是行不通的。所以现在我正在尝试找到一种方法来允许 cy.then 设置值,然后继续同步检查其他所有内容。

这是我迄今为止尝试过的无济于事

cy...then((body)=>{
 for (let row = 0; row < body.length; row++) {
   //Body is the table, and we get the table len
   for (let col = 0; col < 4; col++) {
     //Begin looping thru each row's col and compare
     // Here I attempt to create sync code because I want to not continue
     // to loop thru all 4 col if one doesnt match
     let val;
     new Cypress.Promise((resolve)=>{
       cy.get('...').eq(row)...eq(col).then((txt)=>{
           val = txt
           resolve(txt
       })
     })
     //Begin to compare using val and break child loop if needed
 
   }
 }
 })

我不断将 val 设为未定义。我是柏树的新手,所以任何指针都会很酷。 谢谢。

我尝试过的 pt2: 在 at 中使用 async/await

cy...then(async(body)

并等待 cypress 承诺,收到超时 4000 毫秒错误,将超时增加到 10000 毫秒,但仍然超时。

【问题讨论】:

    标签: cypress


    【解决方案1】:

    编辑:根据您的回答,如果您的代码正确,您可以使用should 而不是then,因为可能存在可重复性:

    cy...should((body)=>{ //over here
     for (let row = 0; row < body.length; row++) {
       //Body is the table, and we get the table len
       for (let col = 0; col < 4; col++) {
         //Begin looping thru each row's col and compare
         // Here I attempt to create sync code because I want to not continue
         // to loop thru all 4 col if one doesnt match
         let val;
         new Cypress.Promise((resolve)=>{
           cy.get('...').eq(row)...eq(col).should((txt)=>{ //or over here
               val = txt
               resolve(txt
           })
         })
         //Begin to compare using val and break child loop if needed
     
       }
     }
     })
    

    【讨论】:

    • 嗨,val 变量放置正确,因为检查也在子 for 循环中,但我还是按照你说的做了。它仍然没有工作, val 未定义。我有点需要子 for 循环中的 val,因为检查还需要 col 索引。我也尝试添加 await 并不起作用。
    • 我明白了,我的想法是在外部循环中需要 val,这就是它可能不起作用的原因。我会试着为内心的人寻找一个专门的想法
    猜你喜欢
    • 2018-09-08
    • 2019-05-09
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多