java - 如何累计for循环内的while循环的总执行次数?

标签 java selenium selenium-webdriver

我想累积 for 循环内的条件 while 操作循环的总尝试次数,以重复测试 while 循环操作的稳定性。 (使用Selenium框架)

尝试过改变循环方式或者设置变量,用尽了所有能想到的方法,查遍了社区,似乎没有人遇到类似的情况。

public static void main(String[] args) throws Exception {
  for (int loop = 1; loop <= 10; loop++) {
    System.out.println("Loop count: " + loop);
    boolean retry = true;
    int i = 1;
    while (retry == true) {
      int t = i++;
      //...
      System.out.println("Test case 1 - Start Run " + t);
      try {
         // my testing script of actions
        System.out.println("Test case 1 - Run Success, finished by " 
            + t + " execution(s)");
        retry = false;
        break;
      } catch (Exception e) {
        System.out.println("Test case 1 - Run " 
            + t + " Failed, retry...");
      } 
      driver.quit();
    }
  }
}

该代码是通过循环整个脚本并查看有多少次尝试来测试 ActionScript (在 Selenium webdriver 框架中)的稳定性,当出现异常错误时(通常会完全停止脚本),自动重试而无需人为中断它实际上需要完成 10 次成功运行(在每个 for 循环计数结束时打印并累加总 t)。 换句话说,我想计算(在重试循环内)达到 10 次成功运行(for 循环)所需的总尝试次数。

最佳答案

为什么不将总计数保留在两个循环之外?

int totalCount = 0;

for (int i = 0; i < 10; i ++) {

  while (retry) {
    totalCount ++;
    // do your thing
  }
}

System.out.println("Took " + totalCount + " attempts to get 10 successfully");

关于java - 如何累计for循环内的while循环的总执行次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488061/

相关文章:

python - 网页抓取 - 标签中的文本无法实现

java - 读取和操作 CSV 文件

java - 使用 Webdriver selenium 单击下一个列表元素 - Java

bash - 自动启动 Selenium RC 服务器

javascript - Selenium - 能够获取在网页中呈现和显示的 Webelements 列表

python - 无法使用 send_keys 或 requests 上传 pdf 文件

java - Selenium Chromedriver : Unable to find an exact match for CDP version 98, 所以返回找到的最接近的版本:97

java - Tomcat6 response.sendRedirect() 404错误

java - PHP:如何从同一 MYSQL 数据库的不同表中用 PHP 编写变量

java - JSmooth Java 1.4 或更高版本未找到错误