Nightwatch执行命令,迭代

时间:2017-09-21 21:39:03

标签: javascript nightwatch.js

我想知道是否可以使用execute()提供的Nightwatch.js命令来实现我想要的功能。

对于Nightwatch.js,我想基于它所拥有的行数多次对表元素执行操作。显然,我的代码不起作用,任何人都有更好的想法如何做到这一点?

// here is the execute() command
.exeute(
  function() {
    const rows = document.querySelectorAll('table tbody tr');
    return rows.length;  // suppose we have 3 rows in the table
  }, 
  [],
  function(result) {
    // here I want to perform an action to all the 3 rows, but I 
    // could not find a way to do that, here is my original thought
    for (let i = 1; i <= result; i++) {
      client.element('css selector', `table tbody tr:nth-child(${i})`, function() {
        // Action, do something here
      });
    }

  }
)
.waitForElementPresent('.mainpage', 15000)

2 个答案:

答案 0 :(得分:1)

使用.elements获取匹配结果的列表并迭代它。

package com.example.udp;


import android.util.Log;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSender {
    private final static String TAG = "UDPSender";
    private final static int PORT = 4000;
    private static byte[] PKT = "hello".getBytes();
    private DatagramSocket dgram;
    private Thread thread;
    public UDPSender() {
        try {
            this.dgram = new DatagramSocket();
            this.dgram.setSoTimeout(200);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        this.thread = new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] bArr = new byte[1024];
                while (true) {
                    DatagramPacket datagramPacket = new DatagramPacket(bArr, 1024);
                    try {
                        dgram.receive(datagramPacket);
                        Log.d(TAG, "recv something from" + datagramPacket.getAddress().toString());
                    } catch (Exception e) {

                    }
                }
            }
        });
        this.thread.start();
    }

    public void send(InetAddress addr) {
        try {
            long startTime = System.currentTimeMillis();
            this.dgram.send(new DatagramPacket(PKT, PKT.length, addr, PORT));
            long endTime = System.currentTimeMillis();
            Log.d(TAG, "send to " + addr.toString() + " " + (endTime - startTime) + " milliseconds");
        }catch (Exception e) {
            Log.d(TAG, e.toString());
        }
    }
}

答案 1 :(得分:0)

为什么不在execute执行的函数中完成所有操作?

// here is the execute() command
.exeute(
  function() {
    const rows = document.querySelectorAll('table tbody tr');
    for (let i = 0; i < rows.length; i++) {
      let row = rows[i];
      // Action, do something here
    }
  }
)
.waitForElementPresent('.mainpage', 15000)

如果您更新为引用result.value循环中的for以获取execute来电的返回值,原始代码可能会有效。