k6 icon indicating copy to clipboard operation
k6 copied to clipboard

k6/html Selection.next() doesn't let you find there are no more elements

Open mstoykov opened this issue 2 years ago • 0 comments

Brief summary

If you use next() to get the next selection there is not indication that you have run out of selections - it will continue returning you a selection instead of null/undefined

k6 version

everyone

OS

linux

Docker version and image (if applicable)

No response

Steps to reproduce the problem

run

import http from "k6/http";

export default () => {
    let li = http.get("https://test.k6.io").html().find("li");
    for (; li != null; li = li.next()) {
        console.log(li.html())
    }
}

Expected behaviour

The abovce should stop iterating after 3 iterations

Actual behaviour

It doesn't

in order to make you need to check the selection size

import http from "k6/http";

export default () => {
    let li = http.get("https://test.k6.io").html().find("li");
    for (; li.size() > 0; li = li.next()) {
        console.log(li.html())
    }
}

If that is how jquery works then I guess we should better document it in the docs around next()

mstoykov avatar Sep 13 '22 08:09 mstoykov