Part - 2 : Learning more about Selenide Elements

Let's start our baby steps from here...

From the recent blog, we got to know that the below methods play a major role in Selenide writing any test script -

  1. open(URL) = To launch the URL mentioned.

  2. $(selector) = returns the object of the first element found via the selector.

  3. $$(selector) = returns the collection of all the elements found via the selector.

Now, there's much more on finding elements and many ways to declare them -

Selenide provides a wide range of options to work on searching elements which helps to maintain readable code scripts too,

  • find(String cssSelector) or $(String cssSelector)

  • find(By) or $(By)

  • findAll(String cssSelector) or $$(String cssSelector)

  • findAll(By) or $$(By)

Here, the object returned by the above search methods is referred to as a 'proxy-element'. These don't perform any action (search element) until they are attached to an action or a condition check. Let's understand this by an example :

// ....
private SelenideElement username = $("input[name='username']");
private SelenideElement password = find("input[name='password']");
private SelenideElement forgetPasswordLink = $(By.linkText("Forgot your password?"));
private SelenideElement loginButton = $("button[type='submit']");
// ....
open("url");

username.shouldBe(visible).sendKeys("username");
// ...

password.shouldBe(visible).sendKeys("password");
// ...

forgetPasswordLink.should(exist);
loginButton.shouldBe(enabled).click();

As per the above lines of code, SelenideElements don't start to search until we call actions like, shouldBe() or click(), etc

A few of the actions listed are...

  • click()

  • doubleClick()

  • contextClick()

  • hover()

  • setValue(String) | val(String)

  • pressEnter()

  • pressEscape()

  • pressTab()

  • selectRadio(String value)

  • selectOption(String)

  • append(String)

  • dragAndDropTo(String)

  • ... etc

In the next part, we'll be looking at how to write CSS, XPath, etc via Selenide.

References :

Thank You, for reading. :) Happy Learning and Happy Testing!!