Не удалось получить данные таблицы в nightwatch js, так как метод обратного вызова getText выполняется после forloop - PullRequest
0 голосов
/ 22 февраля 2020

Я пытаюсь прочитать содержимое таблицы, и когда я нахожу текст, который я ищу, я хочу щелкнуть строку c и перейти к следующей странице.

rowLength равно 10, а colLength равно 3

for(var row = 1; row <= rowLength ; row++ ){
              for(var col = 1; col <= colLength ; col++ ){
                var colSelector = localSelector  + ` > tr:nth-child(${row}) > td:nth-child(${col})`;
                console.log("colSelector " + colSelector);

                this.getText(colSelector,function(result){
                    console.log("Row" + row + "-" + "Col" + col);
                    if(result.value === serviceName){
                        console.log("Loop Condition ColSelector " + colSelector);
                        return this.waitForElementVisible(colSelector).click(colSelector);
                    } 
                });
            }
        }

Вот результат:

colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(1) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(1) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(1) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(2) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(2) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(2) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(3) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(3) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(3) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(4) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(4) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(4) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(5) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(5) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(5) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(6) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(6) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(6) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(7) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(7) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(7) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(8) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(8) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(8) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(9) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(9) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(9) > td:nth-child(3)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(10) > td:nth-child(1)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(10) > td:nth-child(2)
colSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(10) > td:nth-child(3)

Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4
Row11-Col4

Loop Condition ColSelector div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(10) > td:nth-child(3)
√ Element <div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr:nth-child(10) > td:nth-child(3)> was visible after 46 milliseconds.

Вопросы: Если мы посмотрим на выводить все операторы colSelector печатаются до операторов Row-Col?

В идеале вывод должен выглядеть примерно так, как показано ниже ... один оператор colSelector, один оператор Row-Col Если выполняется сравнение, а затем возвращается ....

Похоже, что метод getText выполняется после for l oop, интересно, как я могу решить эту проблему? или есть какой-нибудь альтернативный способ получить текст без обратного вызова .... любая помощь очень ценится.

Весь код для справки:

module.exports.command = function(selector,serviceName){

    var rowLength;
    var colLength;
    var trservice;
    var localSelector = 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody';

    var rowSelector = localSelector + ' > tr'; 
    this.elements('css selector', rowSelector, function (result) { 
        rowLength = result.value.length;
    }).perform(function(){
        console.log("Row Length is  - " + rowLength);

        var firstRow ='1';
        var colSelector = localSelector + ` > tr:nth-child(${firstRow}) > td`;
         this.elements('css selector', colSelector, function (results){
            colLength = results.value.length;
        }).perform(function(){
            console.log("Row Length - Col Length " + rowLength + "-" + colLength);

            for(var row = 1; row <= rowLength ; row++ ){
                  for(var col = 1; col <= colLength ; col++ ){
                    var colSelector = localSelector  + ` > tr:nth-child(${row}) > td:nth-child(${col})`;
                    console.log("colSelector " + colSelector);

                    this.getText(colSelector,function(result){
                        console.log("Row" + row + "-" + "Col" + col);
                        if(result.value === serviceName){
                            console.log("Loop Condition ColSelector " + colSelector);
                            return this.waitForElementVisible(colSelector).click(colSelector);
                        } 
                    });
                }
            }
        });
    });
};

Большое спасибо !!

...