Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

Page Objects with Partial Classes and Properties- WebDriver C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
26 Jun 2017Ms-PL2 min read 5.3K   2  
Find how to create more refined and more maintainable page objects in WebDriver. These page objects will follow the Single Responsibility Principle more closely.

Introduction

This is the second article from the WebDriver Page Objects Series. It is dedicated to creating page objects with partial classes without the Selenium.Support NuGet.

In my previous article from the series, I showed you how to use the standard page object model that comes from the Selenium.Support NuGet. However, I believe it has a couple of drawbacks. First, you need to install an additional NuGet package, which is an extra code dependency. Moreover, you do not have full control over how the elements are located though the PageFactory class and the FindsBy attributes. If you want to create a SelectElement, you cannot since the FindsBy variables/properties return directly IWebElement.

Test Case

For the examples, I will use once again the Bing’s home page, called BingMainPage. The main test case on this page is to search for a term and then assert the count of the returned results.

Image 1

Page Objects through Partial Classes Revised

Using this approach, the BingMainPage class’s definition is placed inside three different files:

  • BingMainPage – contains the constructor(s) and the action methods
  • BingMainPage.Map – stores all web elements’ properties
  • BingMainPage.Asserter – holds all assertions

BingMainPage

C#
using OpenQA.Selenium;
namespace HuddlePageObjectsElementsStringProperties.ImprovedVersion
{
 public partial class BingMainPage
 {
 private readonly IWebDriver _driver;
 private readonly string url = @"http://www.bing.com/";
 public BingMainPage(IWebDriver browser)
 {
 _driver = browser;
 }
 public void Navigate()
 {
 _driver.Navigate().GoToUrl(url);
 }
 public void Search(string textToType)
 {
 SearchBox.Clear();
 SearchBox.SendKeys(textToType);
 GoButton.Click();
 }
 }
}

The only difference with the previous version of this file is that we do not call PageFactory.InitElements(browser, this); in the constructor.

BingMainPage.Map

C#
using OpenQA.Selenium;
namespace HuddlePageObjectsElementsStringProperties.ImprovedVersion
{
 public partial class BingMainPage
 {
 public IWebElement SearchBox
 {
 get
 {
 return _driver.FindElement(By.Id("sb_form_q"));
 }
 }
 public IWebElement GoButton
 {
 get
 {
 return _driver.FindElement(By.Id("sb_form_go"));
 }
 }
 public IWebElement ResultsCountDiv
 {
 get
 {
 return _driver.FindElement(By.Id("b_tween"));
 }
 }
 }
}

The primary discrepancy between the two versions is here. We locate the elements though the WebDriver’s FindElement method instead of using the FindsBy attributes.

If you are using WebDriver often, you may find my Most Complete Selenium WebDriver C# Cheat Sheet useful. All you need to know - the most basic operations to the most advanced configurations.

BingMainPage.Asserter

C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace HuddlePageObjectsElementsStringProperties.ImprovedVersion
{
 public partial class BingMainPage
 {
 public void AssertResultsCount(string expectedCount)
 {
 Assert.AreEqual(ResultsCountDiv.Text, expectedCount);
 }
 }
}

The usage of the page object in tests stays the same.

In future articles, I will share with you other modifications of the design pattern that can make your tests even more maintainable.

So Far in the "Design Patterns in Automated Testing" Series

  1. Page Object Pattern
  2. Advanced Page Object Pattern
  3. Facade Design Pattern
  4. Singleton Design Pattern
  5. Fluent Page Object Pattern
  6. IoC Container and Page Objects
  7. Strategy Design Pattern
  8. Advanced Strategy Design Pattern

All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.
License Agreement

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
CEO Automate The Planet
Bulgaria Bulgaria
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.

Comments and Discussions

 
-- There are no messages in this forum --