如何从Nightwatchjs中的动态表行中选择广播

时间:2018-12-28 02:53:03

标签: javascript for-loop nightwatch.js

我正在尝试从行中选择与Nightwatch.js中动态表中的列文本具有相同值的单选按钮。 但是,循环中检查的所有xpath都是最后一个循环xpath。

如何为每个循环计数器获取xpath?

我有验证码

trList.html(tr是动态的,甚至可以在0005之后添加)

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();
  }
};

testNightwatch.js

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]

所需的输出控制台

db.coll.find({...}).sort({"subItems.value"})

预先感谢

1 个答案:

答案 0 :(得分: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)