在cypress中使用for循环和if else [英] Using for loop and if else in cypress

查看:1296
本文介绍了在cypress中使用for循环和if else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于需要使用for循环,而如果在 cypress

I am in a situation where I need to use for loop and If else block in cypress

场景: 登录到应用程序后,我需要阅读一个元素的文本,该文本在下面的屏幕截图中四舍五入.

Scenario: Once I login to an application, I need to read an element's text which is rounded in the below screenshot.

刷新屏幕后,该元素将在登录后20-90秒内显示.因此,我需要编写这样的内容,等待元素,如果出现则读取文本并返回值,如果不等待10秒钟,则重新加载页面并再次执行该过程.

This element will appear within 20-90 seconds after I log in, when I refreshed the screen. so I need to write something like this, wait for element, if it appears reads the text and returns the value, if not wait for 10 seconds reload the page and do the process again.

     function waitAndreload() {
    for (let i = 0; i < 10; i++) {
      cy.get("#ele").then(ele => {
        if (ele.text()) {
          return ele.text();
        } else {
          cy.wait(10000);
          cy.reload();
        }
      });
    }
  }

如何在cypress中编写代码,因为cypress不支持if-else或for循环

推荐答案

以下是一种技术解决方案,但首先要解释一下我认为是更好的解决方案.

Below is a technical solution, but first is want to explain what I believe is a better solution.

在@dwelle评论中,您尝试做的事情似乎并不是测试设计的最佳实践.测试应设计为确定性的,并控制可能影响预期结果的所有相关输入.

Adding to @dwelle comment, it seems like what you're trying to do is not a best-practice in terms of the the design of the test. Tests should be designed to be deterministic and should control all relevant inputs that may affect the expected result.

更具体地说,这段文字是真实用户应该看到和使用的东西,还是仅仅是开发人员出于调试或测试目的而放置的东西?如果是真正的用户,那么什么决定了是否应该显示它呢? (是纯随机的吗?,如果是,请参见下文)是否用于测试或调试,请与开发人员联系,并提出一种更好的解决方案,在该解决方案中,您可以控制是否直接显示此文本.如果这是用户应该看到的东西,并且不是随机的,那么请考虑应满足什么条件才能使文本出现,并通过控制实际必要的先决条件或通过以下方式设计使该条件为真的测试:使用模拟来模拟这种情况.再次,我建议您咨询开发人员,以帮助您找到最佳方法.

More specifically, is this text something that a real user should see and use, or only something that the developers put for debugging or testing purposes? If it's for a real user, then what determines if it should appear or not? (Is it purely random?, If so, see below) if it's for testing or debugging purposes, talk to the developers and come up with a better solution in which you can control whether this text appears or not directly. If it's something that the user should see and it's not random, then consider what conditions should be met in order for the text to appear, and design the test in a way that makes this condition true, either by controlling the actual necessary preconditions or by using mocks to simulate that condition. Again, I recommend that you consult with the developers to help you find the best approach.

如果它是纯"随机的,则要求开发人员提供一种方法来指定随机生成器的 seed ,然后您也可以对其进行控制.

In case it is "purely" random, then ask the developers to provide a way for you to specify the seed of the random generator, and then you'll be able to control it too.

如所承诺的那样,如果您仍然需要针对特定​​问题的技术解决方案,而无需重新设计测试,则可以使用递归.像这样:

As promised, in case you still want the technical solution for the specific problem, without redesigning the test, then there trick is to use recursion. Something like this:

function getEnvironment() {
   function getEnvironmentInternal(retires) {
        if (retires == 0)
            throw "text didn't appear after the specified retires";
        return ele.text().then(text => {
            if(text)
                return cy.wrap(text);

           cy.wait(10000);
           cy.reload();
           return getEnvironmentInternal(retires-1);
        });
    )};

    return getEnvironmentInternal(10);
}

// usage:
getEnvironment().then(text => {
    // do something with text...
}

这篇关于在cypress中使用for循环和if else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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