App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Page Object Model and Page Factory in Selenium C#

Page Object Model and Page Factory in Selenium C#

By Mohit Joshi, Community Contributor -

A lot of QAs at the initial stages of writing tests often do not feel the need that they might be updating their test suites later. However, at the later stages, updates in the tests are necessary. Therefore, it is essential to write Selenium tests that are scalable and does not break the UI when updated. It is one of the challenges you might face while writing tests. 

The solution is simple, using a design pattern while writing test scripts called Page Object Model (POM). This article will analyze the Page Object Model in detail and discuss how to implement 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 available 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 containing all the web elements and page methods. The names of the components are assigned based on the actions they perform.

Read More to learn Page Object Model in Different Frameworks, such as:
Selenium Java
Selenium Python
Selenium JavaScript
Appium
Playwright
Cucumber
Cypress

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 separate page objects for several web pages. Therefore, when changing the functionality, it will continue to work without much impacting the test scripts. This makes our test scripts maintainable and offers us cleaner code that is easy to understand. 

  • Reusability 

The main advantage of using the page object model in Selenium is to obtain the desired script in less amount of coding. When using the POM, you can use the same object repository for different purposes. Hence, it saves time and resources.

  • Independent Object Repository

Page Object Model in Selenium creates an independent object repository for writing test scripts, which reduces the overall efforts and amount of test scripts. Moreover, this also reduces the code size of your application due to the reusability of the code. 

What is Page Factory?

Page Factory is a class offered by Selenium WebDriver to support Page Object Design Patterns. It came into use to further ease the process of using the POM in Selenium. Using Page Factory in Page Object Model improves the process of implementing a test by increasing the re-usability and maintainability of the elements. There is no need to implement imports and cache storage while using the Page Factory. 

Features of Page Factory in Selenium C# 

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

  • You can initialize elements of a Page class without using the keyword FindElement.
  • Using Page Factory, @FindsBy annotation comes in handy. It helps in finding the elements of a page and can accept attributes such as TagName, PartialLinkText, Name, LinkText, Id, Css, ClassName, and XPath. For example, to find an element and create a page object.
[FindsBy(How = How.Name, Using = "q")]
  • Page Factory class has a static method called initElements. A web element can be initialized using the initElements method, which is used when an annotation @FindsBy is used to locate it.

Implementing POM in Selenium C#

Let’s understand how to implement the Page Object Model in Selenium C# with a practical example. Here creating a test where it will navigate to an e-commerce website, search for a product and assert that it reached the correct product. 

Directory Structure of Page Object Model in Selenium C#

On implementing the Page Object Model, you shall have a nested folder structure, which indicates that some files are working as a repository for storing all the reusable components of the project. This will be the folder structure for our current project.

Prerequisites

Here are some prerequisites that you must have:

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

Implementing Page Object Model in Selenium C#

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

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

Creating new project for Page Object Model in Selenium C

Step 2: Install all the necessary packages
You have to install the following packages from your NuGet package manager. 

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

Step 3: Create HomePage.cs

using OpenQA.Selenium;
using SeleniumExtras.PageObjects;
using 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")]
private IWebElement _searchtxtbox;

[FindsBy(How = How.Name, Using = "btnK")]
private IWebElement _searchbtn;

public HomePage(IWebDriver driver) 
{
_driver = driver;
PageFactory.InitElements(driver, this);
}

public void Search(string searchText)
{
_searchtxtbox.SendKeys(searchText);
_searchbtn.Click();
}


}
}

Step 4: Create HomeTest.cs

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using PageObjectModel.Source.Pages;
using 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 void 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.

Running Tests for Page Object Model in Selenium C

Try Selenium Testing on Real Devices for Free

On a closing note, 

In this example, we understood how to implement the Page Object Model in Selenium C# and what edges we get to enjoy. You might not be able to feel the strength of using the Page Object Model in small applications, however, for large-scale applications, there is no better way to approach the development and testing of the application. In big applications, there’s often a need to update the test scripts, therefore, using POM reduces the overall efforts and eases the process of maintaining the code. 

Tags
Automated UI Testing Automation Testing Selenium Selenium Webdriver

Featured Articles

Selenium with C# : How to start running Automated Tests

Wait Commands in Selenium C and C#

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.