Selenium C# Tutorial: Page Object Model (POM) & Page Factory in Selenium
Updated: Mar 5
Page Object Model (POM) is one of the most commonly used design patterns in test automation projects that involve an object repository for web elements. The main advantage of this model is that it reduces code duplication, which increases design efficiency and improves the overall test maintenance.
When using this model, there should be a corresponding Page class for each web page in the tested application. This class will identify a list of WebElement that we want to include in our tests and all the appropriate methods we will use to handle the different operations on those WebElements.
Here is a high-level example of POM structure based on my blog:

I will demonstrate the POM pattern on my Home Page and Navigation object in the following code example.

Code:
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Internal;
namespace SeleniumC
{
public class Program
{
static void Main(string[] args)
{
Driver.driver.Navigate().GoToUrl("http://www.agilequalitymadeeasy.com/");
Thread.Sleep(5000);
//Example of POM:
AgileQualityMadeEasy_Home_Page _Home_Page = new AgileQualityMadeEasy_Home_Page();
_Home_Page.CookieBanner.Click();
_Home_Page.ValidateMainTitle(_Home_Page.MainTitle);
_Home_Page.IsLogoDisplayed(_Home_Page.PageLogo);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using SeleniumExtras.PageObjects;
using OpenQA.Selenium.Internal;
namespace SeleniumC
{
public class AgileQualityMadeEasy_Home_Page
{
public AgileQualityMadeEasy_Home_Page()// Creating a Constructor (Mandatory)
{
PageFactory.InitElements(Driver.driver,this);
}
//Elements
[FindsBy(How = How.XPath,Using = "/html/body/div[2]/div/div[2]/button[2]")]
public IWebElement CookieBanner { get; set; }
[FindsBy(How = How.CssSelector, Using = ".font_4 > span:nth-child(1) > span:nth-child(1)")]
public IWebElement MainTitle { get; set; }
[FindsBy(How = How.CssSelector, Using = "#img_comp-kgwowrt01 > img:nth-child(1)")]
public IWebElement PageLogo { get; set; }
public void IsLogoDisplayed(IWebElement element)
{
if (element.Displayed==true)
{
Console.WriteLine("Element is available");
}
else
{
Console.WriteLine("Element is not available");
}
}
public void ValidateMainTitle(IWebElement element)
{
if (element.Text.Contains("AGILE QUALITY") == true)
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
}
}
}