{"id":3851,"date":"2020-06-26T20:28:34","date_gmt":"2020-06-26T14:58:34","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3851"},"modified":"2025-06-23T07:20:32","modified_gmt":"2025-06-23T11:20:32","slug":"page-object-model-pom-and-page-factory-using-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/page-object-model-pom-and-page-factory-using-selenium-webdriver\/","title":{"rendered":"Page Object Model (POM) and Page Factory using Selenium WebDriver"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is the Page Object Model?<\/strong><\/h2>\n\n\n\n<p><strong>Page Object Model (POM)<\/strong>&nbsp;is a design principle, mostly used in test automation that creates&nbsp;<strong>Object Repository<\/strong>&nbsp;for web UI elements.&nbsp;It creates a separate Object Repository to store object locators. Under this model, for each web page, there should be a separate page class. <\/p>\n\n\n\n<p>The page class will have all object locators and most action methods preferably for the corresponding page. The main advantage of the model is that it reduces code duplication and improves test maintenance, making it easier to manage and update test scripts as the application evolves.<\/p>\n\n\n\n<p>By learning this approach in a <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium testing<\/a> course, you can gain practical insights into structuring your test automation framework more effectively, ensuring better scalability and cleaner code. Page Object Model (POM) can be used in any kind of framework like a data-driven, keyword-driven, modular, hybrid framework, etc.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why POM?<\/strong><\/h2>\n\n\n\n<p>Page Object Model has become the most popular test automation design pattern around the testing industry nowadays. POM facilitates as to concentrate the AUT rather than fixing the automation suite to perform the test. In-Page Object Model the information of the object is managed outside the test script, more than making it easy to understand the test script.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Advantages of the Page Object Model?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Makes test code more readable<\/li>\n\n\n\n<li>Helps in quickly automating test cases<\/li>\n\n\n\n<li>Test cases are easy to maintain<\/li>\n\n\n\n<li>Testing new functionality becomes extremely easy.<\/li>\n\n\n\n<li>It makes test script maintenance very easy<\/li>\n\n\n\n<li>The code becomes less and optimized<\/li>\n\n\n\n<li>It avoids writing the duplicate locators for same WebElement which is the main issue that occurs in other frameworks<\/li>\n\n\n\n<li>We can develop our tests faster<\/li>\n\n\n\n<li>Can re-use earlier created objects for one test in another test<\/li>\n\n\n\n<li>You don\u2019t need to change our test logic if UI changes, all we need to do is just we need amend the relevant Page Object(s) to reflect the changes- the test code remains the same.<\/li>\n\n\n\n<li>The Page Object Model approach makes test automation programmer more friendly, durable and comprehensive<\/li>\n\n\n\n<li>The main advantage in POM is our page Object Repository is Independent of Automation Tests. Keeping the separate repository for every page objects helps us to use this repository for different purposes with different frameworks like we integrate this repository with other tools like NUnit, JUnit, etc.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to implement POM?<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Simple POM:<\/strong><\/h3>\n\n\n\n<p>It is a basic structure of Page Object Model (POM) where all the Web Elements of<a href=\"https:\/\/wiki.saucelabs.com\/display\/DOCS\/Getting+Started+with+Selenium+for+Automated+Website+Testing\" rel=\"nofollow noopener\" target=\"_blank\"> Application Under Test<\/a> (AUT) and the Web Elements whose methods are operated are maintained inside a class file. A task like verification should be separate as a part of Test Methods.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/SaXddNgfpvXpSmQzu7aZoDAmfRDjOobin-KvCKUetFN9usV2EN-1EunP2MlJfKUiD6cXC7B0ZykHWLzGxOicG4Lc7Mf9cgokV2C5DKiTLiROdin1mXhxWVgZ15rAXKEogSy_9ig1ZxVdDf12Zw\" alt=\"Page Object Model (POM) and Page Factory using Selenium WebDriver\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Below are the steps to create a POM without Page Factory&nbsp;<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>1)<\/strong>&nbsp;We will create a Java Class for every web page in the application.<\/p>\n\n\n\n<p><strong>2)<\/strong>&nbsp;In each Java Class, declare all the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/find-element-findelements-selenium-webdriver\/\">Web Elements<\/a> as variable.<\/p>\n\n\n\n<p><strong>3)<\/strong>&nbsp;Implement corresponding methods acting on the variables.<\/p>\n<\/div><\/div>\n\n\n\n<p><strong>The design pattern can be implemented using 2 layers:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Page Layer<\/strong>&nbsp;will have the pages of the application as individual Java Class. Each Class will have WebElements declared as variables and the actions that are performed as methods.<\/li>\n\n\n\n<li><strong>Test Layer<\/strong>&nbsp;will hold the application test cases and its verification part.<\/li>\n<\/ul>\n\n\n\n<p><strong>Let&#8217;s take an example of a simple scenario:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the URL of an application.<\/li>\n\n\n\n<li>Type the Email Address and password data.<\/li>\n\n\n\n<li>Click on the Login button.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Page Layer<\/strong><\/h3>\n\n\n\n<p><strong>HomePage<\/strong>: The page opens when the URL is entered and this is where we enter the data for login.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps To Create POM With Real-Time Examples<\/strong><\/h2>\n\n\n\n<p><strong>Step 1:<\/strong> Create a Java Class for two web Pages<\/p>\n\n\n\n<p>In this&nbsp;example, we will access 2 web pages, \u201cHome\u201d and \u201cSearch\u201d pages.&nbsp;Hence, we will create Java classes under Package Name com.automation.pages<\/p>\n\n\n\n<p><strong>Package Name:<\/strong> com.automation.pages<\/p>\n\n\n\n<p><strong><em>FaceBookHomePage.java<\/em><\/strong><\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Create WebElements as variables using \u2018By\u2019 Class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Email, Password, Login button field on the HomePage.<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3:<\/strong> Create methods for the actions performed on WebElements:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Type action on the EmailAddress field.<\/li>\n\n\n\n<li>Type action on the Password field.<\/li>\n\n\n\n<li>Click action on the Login button.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>FaceBookHomePage.java<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> com.automation.pages;\n\n<strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n\n<strong>public<\/strong> <strong>class<\/strong> FaceBookHomePage {\nWebDriver driver;\n&nbsp;\nBy EmailId = By.<em>xpath<\/em>(\"\/\/div&#91;contains(@name,'email')]\");\nBy Password = By.<em>xpath<\/em>(\"\/\/div&#91;contains(@name,'pass')]\");\nBy LoginButton = By.<em>xpath<\/em>(\"\/\/div&#91;contains(@id,'u_0_b')]\");\n\n<strong>public<\/strong> <strong>void<\/strong> typeEmail(String emailId) {\n&nbsp;&nbsp;&nbsp;&nbsp; driver.findElement(EmailId).sendKeys(emailId);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> typePassword(String passwordtext) {\n&nbsp;&nbsp;&nbsp;&nbsp; driver.findElement(Password).sendKeys(passwordtext);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> clickContinue() {\n&nbsp;&nbsp;&nbsp;&nbsp; driver.findElement(LoginButton).click();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> FaceBookHomePage(WebDriver driverFromPreviousClass) {\n&nbsp;&nbsp;&nbsp;&nbsp; <strong>this<\/strong>.driver = driverFromPreviousClass;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Test Layer<\/strong><\/h3>\n\n\n\n<p>Here we implement Test Cases in this class.<\/p>\n\n\n\n<p>We create another package, com.automation.test, and create a Java Class here (MainClass.java).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Steps To Create Test Cases:<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize the driver and provide the URL to open the application.<\/li>\n\n\n\n<li>Create an object of the PageLayer Class and pass the parameter as a driver instance.<\/li>\n\n\n\n<li>By using the object created we call the methods in the PageLayer Class to perform actions\/verification.<\/li>\n\n\n\n<li>Close the driver.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> com.automation.test;\n\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.firefox.FirefoxDriver;\n<strong>import<\/strong> org.testng.annotations.AfterTest;\n<strong>import<\/strong> org.testng.annotations.BeforeTest;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>import<\/strong> com.automation.pages.FaceBookHomePage;\n\/\/import com.automation.pages.HomePage;\n\n<strong>public<\/strong> <strong>class<\/strong> MainClass {\nWebDriver driver;\n\n@BeforeTest\n<strong>public<\/strong> <strong>void<\/strong> SetDriver() {\nSystem.<em>setProperty<\/em>(\"webdriver.gecko.driver\",\"F:\\\\drivers\\\\geckodriver.exe\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver = <strong>new<\/strong> FirefoxDriver();\n&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().window().maximize();\n&nbsp;&nbsp;&nbsp;&nbsp;driver.get(\"https:\/\/www.facebook.com\/\");\n}\n&nbsp;\n@Test\n<strong>public<\/strong> <strong>void<\/strong> verifyLogin() {\n\nFaceBookHomePage home = <strong>new<\/strong> FaceBookHomePage(driver);\nhome.typeEmail(\"abcxyz@gmail.com\");\/\/ Give your Mail Id\nhome.typePassword(\"abtyhh@12345\");\/\/ Give your Password\nhome.clickContinue();\n\n}\n\n@AfterTest\n<strong>public<\/strong> <strong>void<\/strong> CloseDriver() {\ndriver.quit();\n}\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is Pagefactory?<\/strong><\/h2>\n\n\n\n<p>In Selenium the PageFactory Class is an extension to the Page Object design pattern. It is used to initialize the Page Object elements or instantiate the Page Objects itself. Annotations can also be created for elements as the describing properties may not always be descriptive to tell one object from the other. It is also used to initialize the elements of the Page class without having to use \u2018FindElement\u2019 or \u2018FindElements\u2019. To supply descriptive names of target objects to improve the code readability annotations can be used. To support the PageObject pattern, WebDriver\u2019s support library which contains a factory class. The important feature of Page Factory is the Cache feature that will store all frequently used WebElement in the cache.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Let us know more clarity on the term PageFactory<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It provides an alternate way of creating an object repository in terms of syntax and semantics for the web elements on a page.<\/li>\n\n\n\n<li>It uses a different strategy to initialize the web elements.<\/li>\n\n\n\n<li>For UI web elements the object repository could be built by<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>POM without PageFactory and<\/li>\n\n\n\n<li>POM with PageFactory<\/li>\n<\/ul>\n\n\n\n<p><strong>Let us consider a simple test&nbsp;scenario&nbsp;<\/strong><strong>&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;Create a TestBase class. In this, we create an object of WebDriver, maximize browser, implementing waits, launching URL, etc. In the below program, we have a taken chrome browser and set the System Property to launch a chrome browser.<\/p>\n\n\n\n<p><strong>TestBase.java (BASE CLASS)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a Package tests<\/li>\n\n\n\n<li>Create a Class TestBase.Java<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> tests;\n\n<strong>import<\/strong> java.io.IOException;\n<strong>import<\/strong> java.util.concurrent.TimeUnit;\n\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n\/\/import org.openqa.selenium.chrome.ChromeDriver;\n<strong>import<\/strong> org.openqa.selenium.firefox.FirefoxDriver;\n<strong>import<\/strong> org.testng.annotations.AfterSuite;\n<strong>import<\/strong> org.testng.annotations.BeforeSuite;\n\n<strong>public<\/strong> <strong>class<\/strong> TestBase {\n<strong>public<\/strong> <strong>static<\/strong> WebDriver <em>driver<\/em> = <strong>null<\/strong>;\n\n@BeforeSuite\n<strong>public<\/strong> <strong>void<\/strong> initialize() <strong>throws<\/strong> IOException{\n\nSystem.<em>setProperty<\/em>(\"webdriver.gecko.driver\",\"F:\\\\drivers\\\\geckodriver.exe\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>driver<\/em> = <strong>new<\/strong> FirefoxDriver();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/To maximize browser\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>driver<\/em>.manage().window().maximize();\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Implicit wait\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>driver<\/em>.manage().timeouts().implicitlyWait(20, TimeUnit.<strong><em>SECONDS<\/em><\/strong>);\n\/\/To open facebook\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>driver<\/em>.get(\"https:\/\/www.facebook.com\");\n\n}\n\n@AfterSuite\n\/\/Test cleanup\n<strong>public<\/strong> <strong>void<\/strong> TestClean()\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestBase.<em>driver<\/em>.quit();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 2:&nbsp;<\/strong>Create classes for each page (Eg.&nbsp;<em>Facebook Login<\/em>&nbsp;Page,&nbsp;&nbsp;<em>Facebook Inbox<\/em>&nbsp;Page) to hold the element locators and their methods. Usually,&nbsp;we will create page objects for all available pages in the AUT. For each page, we create a separate class with a constructor and we will identify all the locators and keep them in one class so that we can reuse the locators in multiple methods. It is easy to do the maintenance, if there is any change in the UI, we can simply change on one Page.<\/p>\n\n\n\n<p>Here, I create java files (<em>FacebookLoginPage.java<\/em>&nbsp;and&nbsp;<em>FacebookInboxPage.java<\/em>) for the corresponding pages (<em>Facebook Login<\/em>&nbsp;Page, and&nbsp;<em>Facebook Inbox<\/em>&nbsp;Page) to hold element locators and their methods.<\/p>\n\n\n\n<p><strong>FBHomePage.java (Webpage 1)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a package pages<\/li>\n\n\n\n<li>Create a class FbHomePage<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package pages;<\/strong>\n\n<strong>import org.openqa.selenium.WebDriver;<\/strong>\n<strong>import org.openqa.selenium.WebElement;<\/strong>\n<strong>import org.openqa.selenium.support.FindBy;<\/strong>\n<strong>import org.openqa.selenium.support.How;<\/strong>\n\n<strong>public class FbHomePage {<\/strong>\n<strong>WebDriver driver;<\/strong>\n\n<strong>&nbsp;&nbsp;&nbsp;&nbsp;public FbHomePage(WebDriver driver){&nbsp;<\/strong>\n<strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.driver=driver;&nbsp;<\/strong>\n<strong>&nbsp;&nbsp;&nbsp;&nbsp;}<\/strong>\n\n<strong>&nbsp;&nbsp;&nbsp;&nbsp;\/\/Using FindBy for locating elements<\/strong>\n<strong>\/\/@FindBy(how=How.XPATH, using=\"\/\/div&#91;text()='Settings']\") WebElement profileDropdown;<\/strong>\n<strong>@FindBy(how=How.XPATH, using=\"\/\/div\") WebElement profileDropdown;<\/strong>\n\n<strong>@FindBy(how=How.XPATH, using=\"\/\/text()&#91;.='Log Out']\/ancestor::span&#91;1]\") WebElement logoutLink;<\/strong>\n<strong>\/\/@FindBy(how=How.XPATH, using=\"\/\/\/div&#91;text()='Good morning, Testing!']\") WebElement loggedInUserNameText;<\/strong>\n\n<strong>&nbsp;&nbsp;&nbsp;&nbsp;<\/strong>\n<strong>\/\/ Defining all the Methods that can be performed in the Facebook home page<\/strong>\n\n<strong>&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp; Method to click on Profile Dropdown<\/strong>\n<strong>public void clickOnProfileDropdown(){<\/strong>\n<strong>profileDropdown.click();<\/strong>\n<strong>}<\/strong>\n<strong>\/\/ Method to click on Logout link<\/strong>\n<strong>public void clickOnLogoutLink(){<\/strong>\n<strong>logoutLink.click();<\/strong>\n<strong>}<\/strong>\n<strong>\/\/ Method to verify LoggedIn Username Text<\/strong>\n<strong>\/\/public String verifyLoggedInUserNameText(){<\/strong>\n<strong>\/\/String userName = loggedInUserNameText.getText();<\/strong>\n<strong>\/\/return userName;<\/strong>\n<strong>\/\/}<\/strong>\n\n<strong>}<\/strong><\/code><\/pre>\n\n\n\n<p><strong>FBLoginPage.java (Webpage 2)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> pages;\n\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.support.FindBy;\n<strong>import<\/strong> org.openqa.selenium.support.How;\n\n<strong>public<\/strong> <strong>class<\/strong> FbLoginPage {\nWebDriver driver;\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> FbLoginPage(WebDriver driver){&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>this<\/strong>.driver=driver;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n\/\/Using FindBy for locating elements\n@FindBy(how=How.<strong><em>XPATH<\/em><\/strong>, using=\"\/\/input&#91;@type='email']&#91;@name='email']\") WebElement emailTextBox;\n@FindBy(how=How.<strong><em>XPATH<\/em><\/strong>, using=\"\/\/input&#91;@type='password']&#91;@name='pass']\") WebElement passwordTextBox;\n\/\/@FindBy(how=How.XPATH, using=\"\/\/input&#91;@type='submit']&#91;@id='u_0_5']\") WebElement signinButton;\n@FindBy(how=How.<strong><em>ID<\/em><\/strong>, using = \"u_0_b\") WebElement signinButton;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Defining all the Methods that can be performed in the Facebook home page\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Method to set Email in the email text box\n<strong>public<\/strong> <strong>void<\/strong> setEmail(String strEmail){\nemailTextBox.sendKeys(strEmail);\n}\n\/\/ Method&nbsp; to set Password in the password text box\n<strong>public<\/strong> <strong>void<\/strong> setPassword(String strPassword){\npasswordTextBox.sendKeys(strPassword);\n}\n\/\/ Method to click on Login Button\n<strong>public<\/strong> <strong>void<\/strong> clickOnLoginButton(){\nsigninButton.click();\n}\n\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 3:<\/strong>&nbsp;Create Test (Eg.,&nbsp;<em>FBLoginTest<\/em>) as per the above pages. As per my test scenario which has mentioned above the scripts run as.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Launch browser and open facebook.com<\/li>\n\n\n\n<li>Enter user credentials and do sign in<\/li>\n\n\n\n<li>Verify the loggedIn user name and do logout<\/li>\n<\/ol>\n\n\n\n<p><strong>FBLoginTest.java (Test Case 1)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> tests;\n\n<strong>import<\/strong> org.openqa.selenium.support.PageFactory;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>import<\/strong> pages.FbHomePage;\n<strong>import<\/strong> pages.FbLoginPage;\n\n<strong>public<\/strong> <strong>class<\/strong> FbLoginTest <strong>extends<\/strong> TestBase{\n@Test\n<strong>public<\/strong> <strong>void<\/strong> init() <strong>throws<\/strong> Exception{\n\n\/\/driver.get(\"https:\/\/www.facebook.com\");\nFbLoginPage loginpage = PageFactory.<em>initElements<\/em>(<em>driver<\/em>, FbLoginPage.<strong>class<\/strong>);\nloginpage.setEmail(\"abcqwerty@gmail.com\");\/\/Set your Username\nloginpage.setPassword(\"xxxxyyyyzzzz\");\/\/Set your Password\nloginpage.clickOnLoginButton();\n\nFbHomePage homepage = PageFactory.<em>initElements<\/em>(<em>driver<\/em>, FbHomePage.<strong>class<\/strong>);\nhomepage.clickOnProfileDropdown();\nhomepage.verifyLoggedInUserNameText();\nhomepage.clickOnLogoutLink();\n}\n\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 4:&nbsp;<\/strong>Create testng.xml file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;\n&lt;!DOCTYPE suite SYSTEM \"http:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=<em>\"Everjobs Suite\"<\/em>&gt;\n\n&lt;test name=<em>\"Page Object Model\"<\/em>&gt;\n&lt;classes&gt;\n&lt;class name=<em>\"tests.TestBase\"<\/em> \/&gt;&nbsp;\n&lt;class name=<em>\"tests.FbLoginTest\"<\/em> \/&gt;&nbsp;\n&lt;\/classes&gt;\n&lt;\/test&gt;&nbsp;\n\n&lt;\/suite&gt; &lt;!-- Suite --&gt;<\/code><\/pre>\n\n\n\n<p>Now, if AUT undergoes any change at the login page or any other page we just need to change the page object where we don\u2019t need to change our test script again and again. Now your project structure will look like as below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/mTYpS5sTYdsQoUsF_obBtXj-icgPb2mhwqwgjbTYXLh8se5WiEZGaydAR_GZe5_8WBz_zkE3fjvwZi1V3ST1vJBRY7piB5Kt1aFMWpR3fnolGQY-jHy-N4CUStAU7aTfDbshW2NbyhGCOZkD2Q\" alt=\"POM.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Execute the testnG.xml file on the test suite and it will redirect to facebook.com webpage and enter all the credentials. It will then verify the username and then logout of the account. This is how we implement the Page Object Model with Page Factory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The Page Object Model (POM) and Page Factory in Selenium WebDriver provide a structured, maintainable approach to building test automation frameworks. POM promotes a clean separation between the test code and the UI elements, enhancing readability and ease of maintenance by encapsulating web elements within dedicated page classes. <\/p>\n\n\n\n<p>Using Page Factory further streamlines this process by initializing elements with annotations, making the code more efficient and reducing redundancy. Together, POM and Page Factory empower testers to create robust, scalable, and reusable test scripts, significantly improving the efficiency of Selenium WebDriver automation frameworks and ensuring that updates to UI elements require minimal changes to test scripts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Call to Action<\/h2>\n\n\n\n<p>Ready to master the Page Object Model (POM) and Page Factory in Selenium WebDriver? Join H2K Infosys and elevate your automation testing skills with our expert-led training. Our course offers in-depth insights into structuring your Selenium tests with POM, using Page Factory for efficient element handling, and building a scalable automation framework. Gain hands-on experience and best practices from industry professionals, ensuring you\u2019re well-equipped to tackle real-world automation challenges. Enroll <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium course<\/a> online today at H2K Infosys and bring your Selenium automation skills to a new level!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the Page Object Model? Page Object Model (POM)&nbsp;is a design principle, mostly used in test automation that creates&nbsp;Object Repository&nbsp;for web UI elements.&nbsp;It creates a separate Object Repository to store object locators. Under this model, for each web page, there should be a separate page class. The page class will have all object locators [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3871,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[1034,1033,1036,45,1035],"class_list":["post-3851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-page-factory","tag-page-object-model","tag-pom","tag-selenium","tag-using-selenium-webdriver"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3851"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3851\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3871"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}