2017-03-26 25 views
1

我正在做一個包含我需要迭代的表格的e2e測試,「直到找到一個不會失敗的單擊它時。量角器:過濾器直到找到第一個有效元素

我嘗試了用filter,它是工作:

this.selectValidRow = function() { 
    return Rows.filter(function (row, idx) { 
     row.click(); 
     showRowPage.click(); 
     return errorMessage.isDisplayed().then(function (displayed) { 
      if (!displayed) { 
       rowsPage.click(); // go back to rows Page, all the rows 
       return true; 
      } 
     }); 
    }).first().click(); 
}; 

這裏的問題是,它是迭代所有可用行,而我只需要第一個是有效的(即不顯示一個errorMessage)。

我目前的方法存在的問題是,它耗時太長,因爲我當前的表可能包含數百行。

是否有可能filter(或不同的方法),並停止迭代時出現第一個有效的發生?或可能有人想出一個更好的方法?

+0

但你怎麼知道是一個有效的行?只要點擊它,看到沒有錯誤? – Hosar

+0

是的,在我的情況下,我真的需要訪問這些行中的一個頁面,並且其中一些行會給出錯誤(因爲它們各自的頁面不存在或某些內容)。我只想要一種方法來找到一個不會給我一個錯誤的行。 – eLRuLL

回答

1

你是對的,filter()和其他內置的量角器「函數式編程」方法不會解決「停止迭代時出現第一個有效發生」的情況。您需要「在某些條件評估爲真時採取某些元素」(如Python世界中的itertools.takewhile())。

幸運的是,你可以擴展ElementArrayFinder(在onPrepare()最好),並添加takewhile()方法:

請注意,我建議它是內置的,但功能請求仍然打開:

1

如果您偏​​好處理這種情況的非量角器方法,我會建議async.whilst。異步是一個非常流行的模塊,它很可能是您的應用程序正在使用它。我在編輯器中編寫了下面的代碼,但它應該可以工作,您可以根據您的需求對其進行自定義。希望你能瞭解我在這裏做什麼。

var found = false, count = 0; 
async.whilst(function iterator() { 
    return !found && count < Rows.length; 
}, function search(callback) { 
    Rows[count].click(); 
    showRowPage.click(); 
    errorMessage.isDisplayed().then(function (displayed) { 
     if (!displayed) { 
      rowsPage.click(); // go back to rows Page, all the rows 
      found = true; //break the loop 
      callback(null, Rows[count]); //all good, lets get out of here 
     } else { 
      count = count + 1; 
      callback(null); //continue looking 
     } 
    }); 
}, function aboutToExit(err, rowIwant) { 
    if(err) { 
     //if search sent an error here; 
    } 
    if(!found) { 
     //row was not found; 
    } 
    //otherwise as you were doing 
    rowIwant.click(); 
});