Page Objects with Partial Classes and Properties- WebDriver C#

0
95

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 a 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.

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

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

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 useful my Most Complete Selenium WebDriver C# Cheat Sheet. All you need to know- the most basic operations to the most advanced configurations.

BingMainPage.Asserter

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

LEAVE A REPLY