DEV Community

Cover image for Page Object Model – Selenide Tutorial Series
Dilpreet Johal
Dilpreet Johal

Posted on

Page Object Model – Selenide Tutorial Series

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
}
Enter fullscreen mode Exit fullscreen mode

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]");
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
Enter fullscreen mode Exit fullscreen mode

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));
    }
Enter fullscreen mode Exit fullscreen mode

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


👩🏻‍💻 It's time to begin your SDET journey by joining the SDET-U Academy today 👇🏻
Join Academy

📧 Subscribe to my mailing list to get access to more content like this as well as be part of amazing free giveaways.

👍 You can follow my content here as well -

...

Thanks for reading!

Top comments (0)