繁体   English   中英

如何在错误后停止循环“FOR”

[英]How can I stop cycle “FOR” after mistake

这是我的javascript代码(在Selenium IDE中,为了清楚起见添加了换行符和缩进):storeEval |

var input = window.document.getElementsByTagName('input');
for(var i = 0; i<input.length; i++)  {
    if(window.document.defaultView.getComputedStyle(input[i]).getPropertyValue('background-color') == 'rgb(204, 230, 255)') {
        testResult='passed';
    } else {
        testResult='failed';
    }
} 

| 测试结果

我需要检查所有输入的颜色。 但Selenium Ide仅从最后一个“INPUT”存储测试结果。 我确定在测试过程中有错误。 请帮助。 对不起,我的英语不好

var input = window.document.getElementsByTagName('input');
var testResult = 'passed';
for(var i = 0; i<input.length; i++)  {
  if(window.document.defaultView.getComputedStyle(input[i]).getPropertyValue('background-color') != 'rgb(204, 230, 255)') {
    testResult = 'failed';
    break;
  }
}

你应该修改什么:

  • testResult被赋予一个默认值
  • 测试失败时添加break以停止for循环
  • 区分您是在处理收音机,复选框还是其他类型的输入

这是你的代码更新:

var input = window.document.getElementsByTagName('input'); 
testResult = 'passed'; // <-- set a default value for 'testResult'
for (var i = 0; i < input.length; i++) {
    var bgColor = window.document.defaultView.getComputedStyle(input[i]).getPropertyValue('background-color');
    var inputType = input[i].type;

    if (inputType === 'radio' || inputType === 'checkbox') {
        if (bgColor !== 'rgb(r, g, b)') { // change the value to the desired one
            testResult = 'failed';
            break; // <-- this is what you need to break the 'for' loop
        }
    }
    else {
        if (bgColor !== 'rgb(204, 230, 255)') {
            testResult = 'failed';
            break; // <-- this is what you need to break the 'for' loop
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM