如何从 Nightwatchjs 中的动态 table 行 select 收音机

How to select radio from dynamic table row in Nightwatchjs

我正在尝试 select 行中的单选按钮,其值与 Nightwatch.js 中动态 table 中的列文本相同。 但是,循环中检查的所有 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>

测试Nightwatch.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]

提前致谢

选择器可以通过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)