[英]Protractor - How to jump to the next row in a grid and return the found value
需要量角器快速幫助。
我需要獲取不同於0的第一個單元格文本,然后從第0列和第4列的行數據中獲取
這樣我就可以正確地從第4列獲取第一個值
var resultsTableSummary = element(by.id('lvTable')); //Results table
var rows = resultsTableSummary.all(by.tagName("tr")); // get rows
var cells = rows.all(by.tagName("td")); // get cell values
cells.get(4).getText().then(function(result)
{
if (result > 0)
{
console.log('for Result value '+ result);
}
})
但我想知道兩件事:
1-萬一第一行沒有值,如何移動到下一個tr(行)? 我試圖用元素創建一個數組,但沒有那么成功,有什么想法嗎?
2-如何從該承諾中返回值? 函數從第4列獲取值並在屏幕上為我顯示,但我想在代碼的后續步驟中使用它
謝謝!!
我建議僅保留行元素並根據您的需要過濾它們。 這樣一來,您將始終可以訪問每一行,因為您總是在處理每一行。 如下:
getFirstValidRowData(rows: ElementArrayFinder): promise.Promise<string[]> {
return rows.filter(function (row) {
// filter rows by its values. IF one value is greater than 0 row is valid
return row.all(by.tagName('td')).filter(function (cell) {
// filter a row by valid cells
return cell.getText().then(function (text) {
return parseInt(text) > 0;
});
// if there are cells found row is valid
}).then(function (cells) {
return cells.length > 0;
});
// take valid row and get cells 1 and 4
}).first().all(by.css('td')).filter(function (cell, index) {
return index === 0 || index === 3;
// return values of these cells
}).map(function (cell) {
return cell.getText();
});
}
在測試中,您可以執行以下操作:
it('should have one valid row', async function() {
var resultsTableSummary = element(by.id('lvTable')); //Results table
var rows = resultsTableSummary.all(by.tagName("tr"));
var validRowsData = await getFirstValidRowData(rows);
// do whatever you want with your data from cell 1-4
});
我沒有對此進行測試,但是從語法上講這是正確的。 這應該符合您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.