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

Page Object Model and Page Factory in Selenium C #

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.

Overview

Why POM?

  • Ability to reprocess locators and avoid duplicate.
  • Improves test legibility and maintainability.
  • Creates an autonomous object repository for writing test playscript.

How POM Works

  • Each web page is correspond as a class file.
  • Locators are delimitate as variables and functions are specify as methods.

Prerequisites and Implementation in C #

  • Install .Net in the IDE and download the driver of the browser.
  • Create a new projection and import required packages.
  • Write code in separate classes for each page and run test from a common test class.

This article will analyze the Page Object Model in item and discuss how to enforce it.

What is Page Object Model in Selenium C #?

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:






Advantages of using POM in Selenium C #

Here are some of the advantages Page Object Model in Selenium C # offers:

  • Maintainability

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.

  • Reusability

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.

  • Independent Object Repository

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.

What is Page Factory?

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

Features of Page Factory in Selenium C #

Now, let ’ s understand various features that Page Factory offers:

  • You can initialize elements of a Page class without using the keywordFindElement.
  • Using Page Factory, @FindsByannotation comes in handy. It aid in finding the elements of a page and can accept attributes such asTagName, PartialLinkText, Name, LinkText, Id, Css, ClassName, and XPath. For example, to find an factor and create a page object.
[FindsBy (How = How.Name, Using = `` q '')]
  • Page Factory class has a electrostatic method telephoneinitElements. A web factor can be initialized using theinitElementsmethod, which is used when an annotating@FindsByis used to situate it.

Implementing POM in Selenium C #

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.

Directory Structure of Page Object Model in Selenium C #

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.

Prerequisites

Here are some prerequisites that you must feature:

  • IDE: You must have an IDE to write code. Using Ocular Studio 2022 in this code.
  • .NET: To run c # applications on your system you must instal the .NET environment on your system as good as in your IDE.
  • Install web browser and its browser driver: Using Google Chrome, andChromeDriver.

Implementing Page Object Model in Selenium C #

Follow the stairs mentioned below to implement POM in Selenium C #:

Step 1: Create a new project
In this example, habituate Microsoft Visual Studio 2022. Click on create a new project and select a .NET project.

Step 2: Install all the necessary package
You experience to establish the following packages from your NuGet bundle manager.

  • Selenium.WebDriver
  • Selenium.Support
  • DotNetSeleniumExtras.PageObjects.Core
  • WebDriverManager

Step 3: Create HomePage.cs

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 ();}}}

Talk to an Expert

Step 4: Create HomeTest.cs

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 ();}}}

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.

Tags
63,000+ Views

# Ask-and-Contributeabout this matter with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free