Как выбрать радио из строки динамической таблицы в Nightwatchjs

1

я пытаюсь выбрать переключатель из строки с тем же значением, что и текст столбца в динамической таблице в Nightwatch.js. но все xpath, рассмотренные в цикле, будут последним циклом xpath.

Как я могу получить xpath для каждого счетчика цикла?

у меня есть код

trList.html(tr является динамическим и может быть добавлен даже после 0005)

<tbody>
                <tr>
                    <td colspan="2">TITLE</td>
                </tr>
                <tr>
                    <td width="20%">check box</td>
                    <td>No.</td>
                </tr>
                <tr>
                    <td><input type="radio" name="checkradio"></td>
                    <td>0001</td>
                </tr>
                <tr>
                    <td><input type="radio" name="checkradio"></td>
                    <td>0002</td>
                </tr>
                <tr>
                    <td><input type="radio" name="checkradio">
                    </td>
                    <td>0003</td>
                </tr>
                <tr>
                    <td><input type="radio" name="checkradio">
                    </td>
                    <td>0004</td>
                </tr>
                <tr>
                    <td><input type="radio" name="checkradio">
                    </td>
                    <td>0005</td>
                </tr>
            </tbody>

testNightwatch.js

var url = "trList.html";

//check box targetNo
var targetNo = "0003";
var elementCount;
var parentPath = "/html/body/table/tbody/";
module.exports = {

  '@disabled': false,

  'tr dynamic selection' : function (client) {
    client
      .url(url)
      .elements('xpath', parentPath +'tr',
        function(result){
          elementCount = Object.keys(result.value).length;
          console.log(elementCount);
          console.log("data is " + String(Number(elementCount)-2) + " rows");
          for(var i=3; i<=elementCount; i++ ){
              //rows xpath
              var xpath = parentPath + "tr[" + i + "]/";
              client
                .useXpath()
                .getText(xpath + "td[2]",
                    function(result){
                        console.log("current row is " + result.value);
                        console.log("current path is [" + xpath + "td[1]/input]");
                        if(targetNo === result.value){
                            console.log("target checkBOX!");
                            client
                                .useXpath()
                                .click(xpath + "td[1]/input");
                        }
                    });
          }
      })
      .pause(1000)
      .end();
  }
};

Консоль вывода

data is 5 rows
current row is 0001
current path is [/html/body/table/tbody/tr[7]/td[1]/input]
current row is 0002
current path is [/html/body/table/tbody/tr[7]/td[1]/input]
current row is 0003
current path is [/html/body/table/tbody/tr[7]/td[1]/input]
target checkBOX!
current row is 0004
current path is [/html/body/table/tbody/tr[7]/td[1]/input]
current row is 0005
current path is [/html/body/table/tbody/tr[7]/td[1]/input]

Желаемая выходная консоль

data is 5 rows
current row is 0001
current path is [/html/body/table/tbody/tr[3]/td[1]/input]
current row is 0002
current path is [/html/body/table/tbody/tr[4]/td[1]/input]
current row is 0003
current path is [/html/body/table/tbody/tr[5]/td[1]/input]
target checkBOX!
current row is 0004
current path is [/html/body/table/tbody/tr[6]/td[1]/input]
current row is 0005
current path is [/html/body/table/tbody/tr[7]/td[1]/input]

заранее спасибо

  • 0
    Вы, вероятно, должны добавить waitForElementPresent () и указать свой путь. То, что происходит сейчас, вероятно, что код, будучи асинхронным, продвигается к своей конечной точке.
Теги:
for-loop
nightwatch.js

1 ответ

0
Лучший ответ

Селектор может быть получен со вторым аргументом waitForElementPresent(). Я смог динамически получить xpath, используя его

testNightwatch.js

  'tr dynamic selection' : function (client) {
    client
      .url(url)
      .elements('xpath', parentPath +'tr',
        function(result){
          elementCount = Object.keys(result.value).length;
          console.log("data is " + String(Number(elementCount)-2) + " rows");

          for(var i=3; i<=elementCount; i++ ){
              //rows xpath
              var xpath = parentPath + "tr[" + i + "]/";
              client
                .useXpath()
                .waitForElementPresent(xpath + "td[2]", function(result,xpath){
                    result.value.map(function(element, err){
                        client.elementIdAttribute(element.ELEMENT, 'innerText', function(res){
                        console.log("current row is " + res.value);
                        console.log("current path is [" + xpath.element.selector.slice(0,29) + "td[1]/input]");
                            if(res.value === targetNo){
                                console.log("target checkBOX!");
                                client.useXpath().click(xpath.element.selector.slice(0,29) + "td[1]/input");
                            }
                        })
                    });
                });
          }
      })
      .pause(1000)
      .end();
  }

Консоль вывода

data is 5 rows
√ Element </html/body/table/tbody/tr[3]/td[2]> was present after 20 milliseconds.
current row is 0001
current path is [/html/body/table/tbody/tr[3]/td[1]/input]
√ Element </html/body/table/tbody/tr[4]/td[2]> was present after 16 milliseconds.
current row is 0002
current path is [/html/body/table/tbody/tr[4]/td[1]/input]
√ Element </html/body/table/tbody/tr[5]/td[2]> was present after 16 milliseconds.
current row is 0003
current path is [/html/body/table/tbody/tr[5]/td[1]/input]
target checkBOX!
√ Element </html/body/table/tbody/tr[6]/td[2]> was present after 37 milliseconds.
current row is 0004
current path is [/html/body/table/tbody/tr[6]/td[1]/input]
√ Element </html/body/table/tbody/tr[7]/td[2]> was present after 20 milliseconds.
current row is 0005
current path is [/html/body/table/tbody/tr[7]/td[1]/input]

OK. 5 assertions passed. (1.768s)

OK. 5  total assertions passed. (3.865s)
Сообщество Overcoder
Наверх
Меню