Home > Software engineering >  Creating children ElementsCollection based on parent's one in Selenide
Creating children ElementsCollection based on parent's one in Selenide

Time:04-22

I'm using Selenide and looking for an opportunity to create children ElementsCollection based on a parent's one. For example, I have a web table and a parent ElementsCollection consisting of table rows. So, after filtering this collection by some condition, I get, for example, 50 result rows. Then need to save the first cell in each row as a SelenideElement in a new ElementsCollection (children). This case doesn't have any issues, if I use List, because I can do this using stream() as:

List<SelenideElement> parents = $$("parent_css_selector");
List<SelenideElement> children = parents.stream().filter(s -> s.getText().equals("some_text")).map(s -> s.$("child_css_locator")).collect(Collectors.toList());

//or even in List<String> if I need to...

List<String> childrenTexts = parents.stream().filter(s -> s.getText().equals("some_text")).map(s -> s.$("child_css_locator")).getText().collect(Collectors.toList());

But since stream() was deprecated in Selenide 6.2.0 I cant find an opportunity to do this.

CodePudding user response:

I've raised this question in specialized Selenide topics in Gitter and GitHub. And I would like to note that I received a response within an hour and it is very valuable approach in project development and support. )) Here is the answer of Andrei Solntsev, Selenide founder.

I recommend to avoid such long iterations etc. It causes slow tests. Instead, I would write a proper xpath that finds all the needed elements with just one web driver call.

I registered a feature request for adding non-deprecated stream() method: #1773

I really DON’T RECOMMEND using iterating elements this way. Such a test is highly ineffective. Just write a CollectionCondition as the deprecation notice recommends.

As far as I understood, he will return non-deprecated stream() in ElementsCollection.

CodePudding user response:

Your IDE should sort this out for you. Since stream() is deprecated you can replace it with a for loop, such as this

List<SelenideElement> children;
    
{
  List<SelenideElement> list = new ArrayList<>();
  for (SelenideElement s : parents) {
    if (s.getText().equals("some_text")) {
      SelenideElement child_css_locator = s.$("child_css_locator");
      list.add(child_css_locator);
    }
  }
  children = list;
}

IntelliJ generated this automatically, apologies if it's not exactly what you wanted but it no longer relies on stream().

  • Related