{"id":145,"date":"2025-07-19T02:19:17","date_gmt":"2025-07-19T02:19:17","guid":{"rendered":"https:\/\/qaplaybook.com\/?p=145"},"modified":"2025-07-19T02:19:19","modified_gmt":"2025-07-19T02:19:19","slug":"creating-page-object-model-in-selenium-a-beginners-guide","status":"publish","type":"post","link":"https:\/\/qaplaybook.com\/index.php\/2025\/07\/19\/creating-page-object-model-in-selenium-a-beginners-guide\/","title":{"rendered":"Creating Page Object Model in Selenium: A Beginner\u2019s Guide"},"content":{"rendered":"\n<p>If you\u2019re automating UI tests with Selenium and still writing your locators and actions directly inside your test scripts, you might quickly find your code hard to maintain. That\u2019s where the <strong>Page Object Model (POM)<\/strong> comes in.<\/p>\n\n\n\n<p>In this blog post, you\u2019ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What the Page Object Model is<\/li>\n\n\n\n<li>Why you should use it<\/li>\n\n\n\n<li>How to implement it in Selenium step by step<\/li>\n\n\n\n<li>Real-world example: Login page<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 What Is Page Object Model?<\/h2>\n\n\n\n<p><strong>Page Object Model (POM)<\/strong> is a design pattern that creates a separate Java class (or file) for each page in your application. This class contains all the <strong>web elements<\/strong> and the <strong>methods (actions)<\/strong> you can perform on that page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">In short:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 page = 1 class<\/li>\n\n\n\n<li>Tests interact with page classes, not directly with elements<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\ude80 Why Use Page Object Model?<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Benefit<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td><strong>Reusability<\/strong><\/td><td>One method or locator can be reused across tests<\/td><\/tr><tr><td><strong>Maintainability<\/strong><\/td><td>Update locator in one place if the UI changes<\/td><\/tr><tr><td><strong>Readability<\/strong><\/td><td>Tests look like readable steps instead of code blocks<\/td><\/tr><tr><td><strong>Separation of Concerns<\/strong><\/td><td>Logic and structure are separated from test flow<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 Structure of a Selenium POM Project<\/h2>\n\n\n\n<p>Typical folder structure:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cssCopyEdit<code>src\/\n  \u2514\u2500\u2500 pages\/\n        \u2514\u2500\u2500 LoginPage.java\n  \u2514\u2500\u2500 tests\/\n        \u2514\u2500\u2500 LoginTest.java\n  \u2514\u2500\u2500 utils\/\n        \u2514\u2500\u2500 BaseTest.java\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u270d\ufe0f Step-by-Step Example: Login Page POM<\/h2>\n\n\n\n<p>Let\u2019s create a basic test for a login page using POM in Java + Selenium.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create the Page Class (<code>LoginPage.java<\/code>)<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopyEdit<code>package pages;\n\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\n\npublic class LoginPage {\n    WebDriver driver;\n\n    \/\/ Locators\n    By usernameField = By.id(\"username\");\n    By passwordField = By.id(\"password\");\n    By loginButton   = By.id(\"loginBtn\");\n\n    \/\/ Constructor\n    public LoginPage(WebDriver driver) {\n        this.driver = driver;\n    }\n\n    \/\/ Actions\n    public void enterUsername(String username) {\n        driver.findElement(usernameField).sendKeys(username);\n    }\n\n    public void enterPassword(String password) {\n        driver.findElement(passwordField).sendKeys(password);\n    }\n\n    public void clickLogin() {\n        driver.findElement(loginButton).click();\n    }\n\n    public void loginAs(String username, String password) {\n        enterUsername(username);\n        enterPassword(password);\n        clickLogin();\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Create the Test Class (<code>LoginTest.java<\/code>)<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopyEdit<code>package tests;\n\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.ChromeDriver;\nimport org.testng.annotations.*;\nimport pages.LoginPage;\n\npublic class LoginTest {\n    WebDriver driver;\n    LoginPage loginPage;\n\n    @BeforeClass\n    public void setUp() {\n        driver = new ChromeDriver();\n        driver.get(\"https:\/\/example.com\/login\");\n        loginPage = new LoginPage(driver);\n    }\n\n    @Test\n    public void testValidLogin() {\n        loginPage.loginAs(\"testuser\", \"password123\");\n        \/\/ Add assertions here\n    }\n\n    @AfterClass\n    public void tearDown() {\n        driver.quit();\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep <strong>locators private<\/strong> in page classes<\/li>\n\n\n\n<li>Use <strong>clear method names<\/strong> like <code>enterEmail()<\/code>, <code>clickSubmit()<\/code><\/li>\n\n\n\n<li>Avoid mixing test logic with element finding<\/li>\n\n\n\n<li>For large pages, split methods logically (e.g., forms, modals)<\/li>\n\n\n\n<li>Reuse helper methods like <code>loginAs()<\/code> to simplify tests<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Summary<\/h2>\n\n\n\n<p>The Page Object Model is a key technique to structure Selenium automation tests for better readability, maintainability, and scalability. If you&#8217;re working on any kind of UI automation\u2014POM should be part of your standard setup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019re automating UI tests with Selenium and still writing your locators and actions directly inside your test scripts, you might quickly find your code hard to maintain. That\u2019s where the Page Object Model (POM) comes in. In this blog post, you\u2019ll learn: \u2705 What Is Page Object Model? Page Object Model (POM) is a &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-test-management","no-thumb"],"_links":{"self":[{"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":1,"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaplaybook.com\/index.php\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}