Page Object Model and Page Factory in Selenium C#
On This Page What is Page Object Model in Selenium C #?May 19, 2026 · 5 min read · Tool Comparison
Managing the locators and test logic together quick becomes a topsy-turvyness in Selenium mechanization. It is essential to write Selenium tests that are scalable and not break the UI when update. Page Object Model (POM) solves this by separating page locators from examination scripts, making code more readable, reclaimable, and maintainable. Why POM? How POM Works Prerequisites and Implementation in C # This article will analyze the Page Object Model in item and discuss how to enforce it. Page Object Model is a design pattern to build an object repository for the UI elements usable under testing. It is largely used along Selenium to automate our tests. The idea behind implementing the Page Object Model is to create a central repository to control the webpage. Using the model, you can create one corresponding page class carry all the web factor and page method. The names of the components are assigned based on the action they execute. Read More to learn Page Object Model in Different Frameworks, such as: Here are some of the advantages Page Object Model in Selenium C # offers: In the Page Object Model, there are freestanding page objects for several web pages. Therefore, when changing the functionality, it will continue to work without much impacting the test scripts. This make our test script maintainable and offers us clean code that is easy to understand. The main vantage of using the page object model in Selenium is to obtain the coveted book in less measure of coding. When utilize the POM, you can use the same object repository for different aim. Hence, it saves time and resources. creates an self-governing object depository for publish test scripts, which reduces the overall effort and amount of exam scripts. Moreover, this also trim the code size of your application due to the reusability of the code. SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses. Page Factory is a class offered by Selenium WebDriver to endorse Page Object Design Patterns. It came into use to farther ease the process of utilize the POM in Selenium. Using Page Factory in Page Object Model improves the process of implementing a tryout by increase the re-usability and maintainability of the constituent. There is no demand to implement imports and cache storage while using the Page Factory. Know Different Now, let ’ s understand various features that Page Factory offers: Let ’ s understand how to implement the Page Object Model in Selenium C # with a practical instance. Here creating a test where it will navigate to an e-commerce website, search for a product and assert that it gain the correct merchandise. On enforce the Page Object Model, you shall hold a nested folder structure, which indicates that some files are working as a repository for storing all the recyclable part of the labor. This will be the folder structure for our current project. Here are some prerequisites that you must feature: Follow the stairs mentioned below to implement POM in Selenium C #: Step 1: Create a new project Step 2: Install all the necessary package Step 3: Create HomePage.cs Step 4: Create HomeTest.cs Step 5: Run the test To run your test, open the test explorer and run the test. On a closing line, In this example, we understood how to apply the Page Object Model in Selenium C # and what edges we get to enjoy. You might not be able to feel the forcefulness of using the Page Object Model in small applications, still, for large-scale applications, there is no better way to approach the growth and testing of the application. In big applications, there ’ s often a motivation to update the test scripts, therefore, using POM reduces the overall attempt and eases the process of sustain the codification. On This Page # Ask-and-Contributeabout this matter with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Page Object Model and Page Factory in Selenium C #
Overview
What is Page Object Model in Selenium C #?
–
–
–
–
–
–
– Advantages of using POM in Selenium C #
What is Page Factory?
Features of Page Factory in Selenium C #
[FindsBy (How = How.Name, Using = `` q '')]
Implementing POM in Selenium C #
Directory Structure of Page Object Model in Selenium C #
Prerequisites
Implementing Page Object Model in Selenium C #
In this example, habituate Microsoft Visual Studio 2022. Click on create a new project and select a .NET project.
You experience to establish the following packages from your NuGet bundle manager.expend OpenQA.Selenium; using SeleniumExtras.PageObjects; utilise System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PageObjectModel.Source.Pages {public class HomePage {private IWebDriver _driver; [FindsBy (How = How.Name, Using = `` q '')] individual IWebElement _searchtxtbox; [FindsBy (How = How.Name, Using = `` btnK '')] private IWebElement _searchbtn; public HomePage (IWebDriver driver) {_driver = driver; PageFactory.InitElements (driver, this);} public null Search (string searchText) {_searchtxtbox.SendKeys (searchText); _searchbtn.Click ();}}}using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using PageObjectModel.Source.Pages; employ System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PageObjectModel.Tests {public class HomeTests {private IWebDriver _driver; [SetUp] public void InitScript () {_driver = new ChromeDriver ();} [Test] public vacuum SearchBrowserStack () {HomePage hp = new HomePage (_driver); _driver.Navigate () .GoToUrl (`` https: //google.com ''); hp.Search (`` BrowserStack ''); Assert.True (_driver.Title.Contains (`` browserstack ''));} [TearDown] public void Cleanup () {_driver.Quit ();}}}Related Guides
Automate This With SUSA
Test Your App Autonomously