java - 如何累积 for 循环内的 while 循环的总执行次数?
问题描述
我想累积在 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();
}
}
}
该代码是通过循环整个事情并查看它实际需要多少次尝试来测试动作脚本(在 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");
推荐阅读
- shell - 将密码传递给 sudo 以获取需要输入的命令
- c++ - 声明变量时使用 auto 无效 C++, C++11
- php - 带有登录表单的引导警报
- angular - Angular Material v6 Mat-Footer - “无法读取未定义的属性‘模板’”
- json - D3 - 更新 Choropleth csv 以更改颜色映射
- swift - 以编程方式创建自动调整大小的 TableView
- python-2.7 - python - 安装 spaCy 时出错 (UnicodeDecodeError)
- c# - 如果我以任何 CPU 或 x64 为目标,则 C# Winform 应用程序错误
- python - 使用 tensorflow 将占位符与另一个占位符一起馈送
- android - 警报管理器仅触发 Android 中的最后一个计划任务