Select Page

.NET Core Test Automation in Selenium Using Page Object & Page Factory

by | Selenium

Selenium is a very powerful tool if you want to automate your web testing, and a few months ago even support for .NET Core tests was added to Selenium. However, .NET Core is missing the best feature that Selenium gives you: support for Page Object Model (or POM for short).

If you are unfamiliar with the concept, don’t worry! By the end of this tutorial you will be ready to write great tests using Page Object Model, and this is where TestProject‘s test automation platform comes in to help. TestProject‘s .NET Core SDK provides the ability to write tests using the PageFactory class and Page Object Model, all in one place.

So, what are the advantages of using .NET Core to write your tests?

  • C# is a powerful language with syntax such as nullables, properties, linq and more.
  • .NET Core runs on every platform, including Linux and macOS. Thus, you can easily write cross-platform tests!
  • .NET Core has some of the best development tools on the market, such as Visual Studio and Resharper. 

Tutorial Overview

  1. Build your First Selenium Test with .NET Core.
  2. Use Page Object Model to Improve your Selenium Test.
  3. Use TestProject SDK to Make Page Factory Work with .NET Core.
  4. Upload .NET Core Test Automation in Selenium to TestProject Using Page Object.
  5. Example of TestProject Framework Execution Reports.

1. Build your First Selenium Test with .NET Core

Let’s start this tutorial by learning how to create a “plain” Selenium automated test:
This test example below automates the TestProject Demo website. You can view the website here.

In this test we will do the following:

  1. Create a driver
  2. Navigate to example page
  3. Enter username & password
  4. Click login
  5. Input new profile pages
  6. Update the profile

Here’s the full code sample:

  1. using OpenQA.Selenium;
  2. using OpenQA.Selenium.Chrome;
  3. using OpenQA.Selenium.Support.UI;
  4. using System;
  6. namespace SeleniumTests
  7. {
  8. class MyFirstSeleniumTest
  9. {
  10. string name = “John Smith”;
  11. string password = “12345”;
  12. string country = “United States”;
  13. string address = “Street number and name”;
  14. string email = “john.smith@somewhere.tld”;
  15. string phone = “+1 555 555 55”;
  17. public bool Execute()
  18. {
  19. // Create driver
  20. var driver = new ChromeDriver(“PathToDriverExecutableLibrary”);
  22. // Navigate to example page
  23. driver.Navigate().GoToUrl(“”);
  25. // Enter username & password
  26. driver.FindElementById(“name”).SendKeys(name);
  27. driver.FindElementById(“password”).SendKeys(password);
  29. // Click login
  30. driver.FindElementById(“login”).Click();
  32. // Input new profile pages
  33. new SelectElement(driver.FindElementById(“country”)).SelectByText(country);
  34. driver.FindElementById(“address”).SendKeys(address);
  35. driver.FindElementById(“email”).SendKeys(email);
  36. driver.FindElementById(“phone”).SendKeys(phone);
  38. // Update the profile
  39. driver.FindElementById(“save”).Click();
  40. return new WebDriverWait(driver, TimeSpan.FromSeconds(2)).Until(d => d.FindElement(By.Id(“saved”)).Displayed);
  41. }
  42. }
  43. }

Let’s break it down, step by step:

1. Create a driver
var driver = new ChromeDriver(“PathToYourDriverExeLibrary”);<br>

Here we use Selenium’s ChromeDriver. It receives a path to chromedriver executable folder. Every time we want to automate a different browser, we will have to create a different driver.

2. Navigate to the example page

3. Enter username & password

  1. driver.FindElementById(“name”).SendKeys(name);
  2. driver.FindElementById(“password”).SendKeys(password);

Here we find the input fields by their ID’s and fill them out by using SendKeys. If we would have to do this more than once, we will have to locate the elements again.

4. Click login

5. Input new profile pages

  1. new SelectElement(driver.FindElementById(“country”)).SelectByText(country);
  2. driver.FindElementById(“address”).SendKeys(address);
  3. driver.FindElementById(“email”).SendKeys(email);
  4. driver.FindElementById(“phone”).SendKeys(phone);

This is the same as step 3 above, except that now we have an additional combo box to select from, using the SelectElement wrapper.

6. Update the profile

  1. driver.FindElementById(“save”).Click();
  2. return new WebDriverWait(driver, TimeSpan.FromSeconds(2)).Until(d => d.FindElement(By.Id(“saved”)).Displayed);

In addition to saving the profile, we waited for the “saved” element that becomes visible after save.

This code is fine for a first test. However, it has several problems:

  • We have to manually search for the elements we want to use. If we reload the page, we have to do this several times.
  • If we want to do things more than once, we’ll have to duplicate our code.
  • From this example, it is hard to know on which page we are.

Let’s see how we can get around these problems using Page Object Model.


2. Use Page Object Model to Improve your Selenium Test

Page Object Model is an object design pattern in Selenium. In this design pattern web pages are represented as classes, and the various elements on the page are defined as variables on the class. All possible user interactions can then be implemented as methods on the class.

Let’s use Page Objects to represent our example. In our case we have 2 pages: the Login Page and the Profile Page.
Let’s look at the Login Page first:


Share This