javascript - How can i wait for each element in a list to update to a certain text using cypress - Stack Overflow

Let´s say that i have a list with items with different colors. That list can be updated to only list bl

Let´s say that i have a list with items with different colors. That list can be updated to only list blue items if i add a parameter. How can i verify that the each item is correct?

cy.addParameter('blue'); //Will send graphQL query to fetch the correct items. 

cy.get('data li').each((item)=> {
         cy.wrap(item).should('have.text', ' blue ');
       });

This will fail because the items in the list has not been updated before i have the possibility to check each item. It´s possible to wait for the request to finnish, but as the queries saves after the first run that "check" won´t work the second time.

Let´s say that i have a list with items with different colors. That list can be updated to only list blue items if i add a parameter. How can i verify that the each item is correct?

cy.addParameter('blue'); //Will send graphQL query to fetch the correct items. 

cy.get('data li').each((item)=> {
         cy.wrap(item).should('have.text', ' blue ');
       });

This will fail because the items in the list has not been updated before i have the possibility to check each item. It´s possible to wait for the request to finnish, but as the queries saves after the first run that "check" won´t work the second time.

Share Improve this question asked Feb 26, 2019 at 10:54 TesterTester 911 gold badge2 silver badges5 bronze badges 7
  • what error are you getting from cypress with this test code? Timeout? – kuceb Commented Feb 27, 2019 at 18:10
  • No. As the list is populated before i add the parameter and it takes a few seconds for it to update, the cy.get('data li').each iterates through the list before it have updated – Tester Commented Feb 28, 2019 at 10:22
  • 1 Yes, so what is the error you're getting from cypress? A failed assertion? Many failed assertions? What does it say? – kuceb Commented Feb 28, 2019 at 12:12
  • It will say that not all items in the list have text blue – Tester Commented Mar 1, 2019 at 7:52
  • 1 am I the only one that finds super weird that Cypress boasts everywhere about "being async aware", "no need to wait everywhere"... BUT at the same time it's unable to elegantly handle this use case? – Lucas Pottersky Commented Aug 2, 2020 at 15:04
 |  Show 2 more ments

2 Answers 2

Reset to default 6

You must write a recursive promise function and check by yourself the color text, try the following

function checkColor(selector, color, maxWait, alreadyWaited = 0) {
  const waitTime = 500;
  // cy.get returns a thenebale
  return cy.get(selector).each((item)=> {
    // it checks the text right now, without unnecessary waitings
    if(!item.text() === color) {
      return false;
    }
    return true;
  }).then(result => {
    if(result) {
      // only when the condition passes it returns a resolving promise
      return Promise.resolve(true);
    }
    // waits for a fixed milliseconds amount
    cy.wait(waitTime);
    alreadyWaited++;
    // the promise will be resolved with false if the wait last too much
    if(waitTime * alreadyWaited > maxWait) {
      return Promise.reject(new Error('Awaiting timeout'))
    }
    // returns the same function recursively, the next `.then()` will be the checkColor function itself
    return checkColor(selector, color, maxWait, alreadyWaited);
  });
}

// then, in your test...
cy.addParameter('blue'); //Will send graphQL query to fetch the correct items. 
checkColor('data li', 'blue', 10000).then(result => {
  cy.get('data li').each((item)=> {
    cy.wrap(item).should('have.text', ' blue ');
  });
  // or
  expect(result).to.be(true);
});

I tried to ment a much as possible the code refactoring the code I developed for an accepted similar question.

Let me know if you need some more help

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744984731a4604544.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信