SDET Unicorns

Page Object Model – Selenide Tutorial Series

Table of Content

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 –

Thanks for reading!

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

Functional vs Regression Testing

Functional Testing vs Regression Testing: Main Differences

Testing has increasingly become a requirement in the software development lifecycle in recent years. The realization that development is no longer a focal point for stakeholders but saving costs by catching defects early in their application makes it necessary to learn different testing types.

Read More »