我想累积 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/