Транспортир Javascript Получить все значения из столбца и суммировать их - PullRequest
0 голосов
/ 21 сентября 2018

Я хочу получить все значения из столбца в таблице на веб-странице, пока столбец не содержит элемент, значения суммы и не возвращает сумму в качестве обещания.

   TablePage.prototype.getPriceValuesFromList = function () {
      for (number = 1; number < 100; number++) {
          var locator = '//*[@id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
            browser.findElement(By.xpath(locator)).then(function (success) {
            if (success) {
                var locator = '//*[@id="root"]/main/section/table/tbody/tr[' + number + ']/td[3]/div[2]';
                return element(By.xpath(locator)).getText().then(function (text) {
                    numb = text.replace(/,/g, '');
                    numb = numb.replace(/\$/g, '');
                    numb = numb.replace(/\./g, '');
                    prices[number] = parseInt(numb);
                    console.log(prices[number]);
                    var sum = prices.reduce(function(a, b) { return a + b; }, 0);
                    console.log(sum);
                    return sum
                })
            }
            else {
                break;
                }
            })
      }
    };

Я получаю: "Недопустимый оператор разрыва"

Как на самом деле это сделать?

Нужно ли мне определять "локатор" 2 раза?

1 Ответ

0 голосов
/ 23 сентября 2018

Вы можете использовать element.all(), чтобы найти все td 3-го столбца, затем используйте map(), чтобы преобразовать каждое текстовое значение td в Number.

TablePage.prototype.getPrice2List = function(){

    return element
        .all(by.css('#root table tbody tr > td:nth-child(3)'))
        .map(function(item){
            // replace all non-numeric,non-dot, not-dash to ''
            return Number(item.replace(/[^\d\.-]/g, '').trim());
        });
};

tablePage.getPrice2List().then(function(prices){
    console.dir(prices);
})
...