更改forEach循环中的选择选项

时间:2014-09-12 17:44:14

标签: javascript angularjs protractor

我有以下测试代码:

ID = {
   CreatedBy: { id: 'createdBy' },
   ModifiedBy: { id: 'modifiedBy' }
}

Profile = {
   All: { text: 'All', val: 0 },
   Sys: { text: 'system_system@system.com', val: 1 },
   Test: { text: 'test_user@live.com', val: 2 }
}

changeSelect(dataId: EnumElement[], params: UserEnum[]) {
   dataId.forEach((data) => {
      params.forEach((elem) => {
         var label = data.id+ ' - Check option changed to - ' + elem.text;
         it(label, () => {
               return element(by.xpath('//select[@id="' + data.id + '"]/option[@value = "' + elem.val + '"]')).click();
         });
    });
}

在我的测试中,我使用如下参数调用changeSelect()函数:

changeSelect([ID.CreatedBy, ID.ModifiedBy], [Profile.Sys, Profile.Test]);

正如预期的那样,我的changeSelect()函数将输出:

createdBy - Check option changed to - system_system@system.com
createdBy - Check option changed to - test_user@live.com
modifiedBy - Check option changed to - system_system@system.com
modifiedBy - Check option changed to - test_user@live.com

但这不是我想要的输出。如何调整循环以实现下面的输出?

createdBy - Check option changed to - system_system@system.com
modifiedBy - Check option changed to - test_user@live.com

1 个答案:

答案 0 :(得分:0)

为什么不使用简单的?

changeSelect(dataId, params) {
    for(i = 0; i < dataId.length; i++){
        var data = dataId[i];
        var elem = params[i];
        var label = data.id + ' - Check option changed to - ' + elem.text;
        it(label, () => {
            return element(by.xpath('//select[@id="' + data.id + '"]/option[@value = "' + elem.val + '"]')).click();
        }
    }
}