Page Object Model — Selenide Tutorial Series

Dilpreet Johal
2 min readOct 10, 2022

--

In this tutorial, we will cover how to implement Page Object Model in Selenide. Page Object Model is a popular design pattern to improve code readability and maintainability as well as to reduce duplicacy in your code. Selenide makes it really to get Page objects setup, let’s take a look at how we can do that –

Setup Page Class

We first need to setup a page class where we can store all our page locators and page methods.

public class HomePage {
// add locators and methods here
}

Setup Page Locators

Once base page class is setup, we can start adding the Page locators to the class –

public class HomePage {

public SelenideElement getStartedBtn() {
return $(By.id("get-started"));
}

public SelenideElement headingTitle() {
return $("h1");
}

public SelenideElement logoLink() {
return $(By.xpath("//a[@class=\"custom-logo-link\"]"));
}

public ElementsCollection linksList() {
return $$("#primary-menu li[id*=menu-item]");
}
}

Note: Make sure to import all the necessary packages from Selenide if auto-import doesn’t work.

Setup Page Methods

Now that we have the page locators setup, we can start adding in necessary page methods to reduce code duplicacy –

   public HomePage open() {
Selenide.open("https://practice.automationbro.com/");
return this;
}
public void assertUrl(String expectedUrl) {
String url = WebDriverRunner.url();
assertEquals(url, expectedUrl);
}

Update Tests to use POM

In your test file, you need to initialize the HomePage class and then start replacing your locators and adding in page locators as well as page methods. Here’s an example –

    @Test
public void testInteractingWithElements() {
// Open page url
home
.open()
.assertUrl("https://practice.automationbro.com/");

// By ID
home.getStartedBtn().click();

// verify heading by CssSelector
home.headingTitle()
.shouldHave(text("Think different. Make different."));

// verify by XPath
home.logoLink()
.should(be(visible));
}

As seen above, in just few steps you can implement Page object model concepts in Selenide. To learn more, check out this video below –

🎓 Learn all the skills needed to become a SDET. Click here to learn more — https://academy.sdetunicorns.com/

Thanks for reading!

--

--

Dilpreet Johal
Dilpreet Johal

Written by Dilpreet Johal

SDET Architect | YouTuber | Tech Blogger | Love to explore new tools and technologies. Get access to all the courses— https://sdetunicorns.com/

No responses yet