ElementsCollection in Selenide

Tutorial 11​​ ​​ Elements Collection concept​​ in​​ Selenide

What you will Learn in this blog:

  • Usage of ElementsCollection class​​ 

  • Usage of​​ SelenideElement

  • CollectionCondition

  • Code snippet

Usage of ElementsCollection class and SelenideElement

Launch​​ https://www.selenium-tutorial.com/courses​​ ​​ 

Inspect any course on this page

See below, the count of tags represented by tagname //a (viz links) is around 70

Let us automate this scenario. When we mouse hover $$ (represents findElements), notice that it returns​​ ‘ElementsCollection’​​ 

So​​ we can write

Execute, the count returns 70, see below

Usage of SelenideElement

Next, for each of the ‘Selenide webelement’, let us​​ 
a) print the text of element and​​ 
b) print the corresponding ‘href’ value

​​ 

Execute.​​ 

Notice below that the text of each element and corresponding ‘href’ link gets printed


CollectionCondition

We can use collection conditions​​ to directly assert something

Let us assert if the count size is greater than 65

Execute, assertion is pass

Let us change the integer to 605

Execute,​​ as expected, the assertion fails

So this is how we can use Elements Collections.

Code snippet

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.*;

import​​ java.net.MalformedURLException;

import​​ org.openqa.selenium.By;

import​​ org.testng.annotations.Test;

import​​ com.codeborne.selenide.CollectionCondition;

import​​ com.codeborne.selenide.ElementsCollection;

import​​ com.codeborne.selenide.SelenideElement;

 

public​​ class​​ ElementsTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

open("https://www.selenium-tutorial.com/courses");

 ElementsCollection​​ counts​​ =​​ $$(By.tagName("a"));

 System.out.println(counts.size());

for(SelenideElement​​ c:​​ counts) {

 String​​ txt​​ =​​ c.getText();

 String​​ hrf​​ =​​ c.getAttribute("href");

 System.out.println(txt​​ +​​ "########"​​ +​​ hrf);

 }

counts.shouldHave(CollectionCondition.sizeGreaterThan(605));

 Thread.sleep(5000);

 }

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024