-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
k6/html: Have a way to get the selection out of an element #2683
Comments
@mstoykov i would like to work on this issue. can you please assign it to me? |
I would like to go ahead! and add my proposal here, but before that I think it is also beneficial to discuss the first proposal by @mstoykov : Challenges
$('li').each(function(index, element) {
if (index == 0)
console.log(element.text())
}) instead of simply writing AlternativesAs an alternative to the suggested solution, I would propose to use a different syntax for getting the selection out of an element, instead of adding a new method to the The ChallengesEven though this solution won't have the downsides of the first proposal, it would have some drawbacks too: Improvements
what do you think? |
I am glad you want to work on this @Azhovan 🎉 I am not certain we can use
I have to reiterate that k6 just isn't a browser and does not open pages or run it's javascript within their "scope". And you can have multiple pages that you have just "downloaded". Namely using somethign like http.batch. So in those cases a global documetn is nto ... possible. All in all I would prefer if we do not even try to make a global document as that will just make things more confusing. Given the changes in the last 1 year around async and group and ... else (see #2728 for a very long discussion) ... I don't think having magical Arguably a
But again I am not certain how much better this is from |
I've never seen a
I see no need to try and be jQuery. We have the Selection API. Reading the forum post, I don't think the author even expects that the API would be using Why not just a method doc.find("li")
.each((_, element) => {
const listEl = Selection.fromElement(element)
.closest('ul')
}) We're not married to WHATWG or W3C either, so maybe doc.find("li")
.each((_, element) => {
const listEl = element.closest("ul")
}) @mstoykov's alternative is also good: element.selection()
element.toSelection()
element.asSelection() |
Would be neat if DOM traversal in |
@w1kman can you expand on this, please. |
import http from 'k6/http';
import { Browser } from 'k6/experimental/browser';
// Using k6/html module
export default function () {
let res = http.get('https://abcd.com');
// ...
// ...
let dom = res.html();
let title = dom.querySelector('title');
let description = dom.querySelector('meta[name="description"]');
console.log('Title: ' + title.text());
console.log('Description: ' + description.attr('content'));
}
// Using k6/experimental/browser module
export default function () {
let browser = new Browser();
let page = browser.open('https://abcd.com');
page.wait();
let title = page.querySelector('title');
let description = page.querySelector('meta[name="description"]');
console.log('Title: ' + title.text());
console.log('Description: ' + description.attr('content'));
browser.close();
} If I'm not mistaken you mean that e.g. the codes above are very similar the only diff is between Im not sure how much this experimental api is being used by users. but overall I think this is a good idea! |
what do you think @mstoykov ? |
In Personally I'd rather use vanilla |
@mstoykov |
Feature Description
As reported in https://community.k6.io/t/create-selection-from-element/4643 it is comment in jquery to do
selection.each
and then get the selection of the element thateach
will give you and continue selecting.This is currently not possible in k6, but we do have the selection of the element as part of the element so it shouldn't be that hard.
This likely though needs some though on the API
Suggested Solution (optional)
We can just have new Element method
selection()
and be done with it. But it likely won't work a lot like jquery, how important is that is different question.We can defined
$
to mean something in k6 - maybe by calling a method onk6/html
. And then use that 🤷.But it will be confusing as
$('li')
will not really mean anything as we don't know which page to query.Maybe it can only be difined in
each
🤷Already existing or connected issues / PRs (optional)
No response
The text was updated successfully, but these errors were encountered: