简体   繁体   English

数组排序方法在 cypress 外部不起作用,但在内部起作用

[英]array sort method doesn't work outside cypress each but works inside

Here is the program piece where you can see that:这是您可以看到的程序片段:

  1. var likes = [] is defined before cy.get: var likes = [] 在 cy.get 之前定义:<\/li>
  2. likes is populated inside the each () method likes 填充在 each () 方法中<\/li>
  3. likes is sorted inside the each method喜欢在每个方法中排序<\/li><\/ol>

    The problem is that I like to sort this likes array outside cy.get(..).each() as next instruction not inside.问题是我喜欢在 cy.get(..).each() 之外对这个 like 数组进行排序,因为下一条指令不在里面。 But that is not working and I have no clue as to why.但这不起作用,我不知道为什么。 Please could you help in here ?请问你能帮忙吗?

     var likes = [] cy.get('.blogs').each(($el, index) => { const bcy = cy.wrap($el) bcy.get('#viewBlog').click() const like = Math.floor(Math.random() * 10) likes[index] = like for(let i=like;i--; ) bcy.get('.blogsDetail').eq(index).contains('likes').click() likes.sort((a, b) => b - a) })<\/code><\/pre>"

Cypress commands are asynchronous.赛普拉斯命令是异步的。 To sort the array after the each<\/code> command, you have to use a then<\/code> callback:要在each<\/code>命令之后对数组进行排序,您必须使用then<\/code>回调:

var likes = []
  cy.get('.blogs').each(($el, index) => {
    const bcy = cy.wrap($el)
    bcy.get('#viewBlog').click()
    const like =  Math.floor(Math.random() * 10)
    likes[index] = like
    for(let i=like;i--; )
      bcy.get('.blogsDetail').eq(index).contains('likes').click()
  }).then(() => {
    likes.sort((a, b) => b - a)
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM