Page Object Model with C#

In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to build a framework to test our entire web page or application. For building a framework, we might consider a design pattern for our tests. And one of the most popular test design pattern for selenium tests is Page Object Model. It is also strongly encouraged to use by Selenium’s Wiki.

Page Object Model

First, let’s briefly understand what Page Object Model (POM ) is.

POM is the most widely used design pattern by the Selenium community on which each web page (or significant ones) considered as a different class. On each of this page classes (i.e page objects), you may define the elements of that page and specific methods for that page. Each page object represents the page of the web page or application. It is a layer between the test scripts and UI and encapsulates the features of the page.

Advantages of Page Object Model 

The advantages of page object model can be listed as:

  • Easy to maintain. Once you made your design even any locator or functionality changes on the web page you know exactly where to change your code.
  • Clean and understandable code. Instead of using the same code all over your test framework, many features are defined within the page objects. Hence the lines of codes written is reduced and your code becomes easier to understand.
  • Helps you point out the UI. Since you create page classes for almost every page on your web page, it is easy to comprehend the functionality and model of your web page even at a glance on your test frameworks.
  • Better test scripts. Your test scripts will consist descriptive method names (e.g homepage.goToLoginPage()) instead of incomprehensible and hard-coded code (e.g driver.findElement(“login”)).click()).

Page Factory

Page Factory is an extension to page objects that is used to initialize the web elements that are defined on the page object. You can define the web elements by using annotation with the help of page factory.

If you would like to learn more about POM, I suggest you might read Martin Fowler’s article about it.

How to Implement Page Object Model in C#

Let’s start by creating a new project in Visual Studio. I am using Visual Studio 2017 Community Edition which can download here.

And name our project.

Before writing our test cases, let’s create our page objects. In this tutorial, I am going to model For the sake of a decent design, let’s create a folder and call it “PageObjects”. We will add page object classes in this folder.

To do that, right-click on the project name (‘POMExample’ in this tutorial), hover on add and click on “New Folder”.

Name it as “PageObjects”.

Now we can add the page object classes inside the folder. In this tutorial, we will cover the home,  about and search result pages. So lets add three classes and name them as “Homepage”, “AboutPage”, “ResultPage” respectively. We are going to model the home page, about and the page that searches are enlisted.

After you add the classes you will have:

It is time to fill inside our page objects. Let’s start with ‘HomePage’ first.

Click on ‘HomePage.cs’ and your homepage class should look exactly like below:

Let’s understand step by step what our ‘Homepage.cs’ does.

This is our constructor. Each page should use the driver on the test class, therefore, this classes driver equals to the driver passed from the test class. PageFactory.InitElements() is used to [FindsBy] annotation to work.

Here, with the help of Page Factory [FindsBy] annotation finds the web elements on the homepage.

And finally, we define the functionalities of our homepage. Please note that a page object method may return another page object as shown above.

Please copy the ‘AboutPage.cs’ and ‘ResultPage.cs’ respectively.

We have created our page objects. Now it is time to write our test case(s) by using the page objects.

“Class1.cs” will be our test class so let’s rename it as “TestClass.cs” or you can name it as you wish.

And click “Yes” when asked to change the class name too.

Let’s take a closer look at our test method to understand how we use page object on the test case.

First, we create an instance of the page object so we can call the method from there. Since goToAboutPage() returns AboutPage, we may create the instance of that page object as shown above.

Let’s imagine how we would write this test case without using POM.

How the quality of code changed can be observed easily. If we use these elements on different tests, we have to find them over and over again in each test which will be a huge burden. So implementing POM within your automation frameworks will provide you many advantages.

GitHub Page of the Project



Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 4 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across wide range of browsers and platforms. In this tutorial you will learn how to do web test automation with Selenium Webdriver and the related tools. Audience...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Selenium Webdriver Performance testing with Jmeter and Selenium Grid

By Ozgur Kaya / Aug 9, 2016 / 2 Comments
In this post, we will complete Selenium Webdriver Performance testing scenario using Jmeter and Selenium Grid. 1- Install Java 7 or later If necessary 2- Download latest Jmeter version 3.0 or higher. 3- Download Jmeter PluginsManager JAR file and...
By | 2017-06-18T02:02:15+00:00 June 18th, 2017|Uncategorized|8 Comments

About the Author:

Ege Aksoz
Holding a BSc in Mechatronics, Ege loves to automate. He is now working as a Software Test Automation Engineer at Tallinn, Estonia. He is also passionate about Artificial Neural Networks and he is a master student on AI at Tallinn Technical University.


  1. Sandeep V June 26, 2017 at 6:21 pm - Reply

    Hi Ege,


    Wanted to check if you can train me on C# and development of selenium framework.

    We can have 1×1 discussion on this. Please can you update the course structure along with fee details.

    Sandeep V

  2. hiren July 18, 2017 at 12:34 am - Reply

    Hi Onur thanks a lot for this solution, i am using java do you have POM with java then c# if yo do can you please email me on

  3. Kizu September 5, 2017 at 2:42 pm - Reply

    This is one of the great article related to POM I have came across since last one week. Great job man.


    • Onur Baskirt
      Onur Baskirt September 5, 2017 at 3:23 pm - Reply

      I will also publish JAVA version soon 😉 For C# version Thanks to Ege.

  4. Vishek October 3, 2017 at 11:37 am - Reply

    Why do I get an error message that a static main function is required as an entry point on executing the above project??
    Need Help regarding this urgently

Leave A Comment