Click here to Skip to main content
13,900,419 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.7K views
2 bookmarked
Posted 26 Jun 2017
Licenced Ms-PL

Page Objects with Partial Classes and Properties- WebDriver C#

, 26 Jun 2017
Rate this:
Please Sign up or sign in to vote.
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.

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

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)

Share

About the Author

Anton Angelov
CEO Automate The Planet
Bulgaria Bulgaria
Anton Angelov is an IT Consultant and Quality Assurance Architect at Innovative Lab. He is passionate about automation testing and designing test harness and tools, having the best industry development practices in mind. In addition, he is an active blogger and the founder of Automate The Planet. He strives to make the site one of the leading authorities in Automation Testing by presenting compelling articles, inspiring ardent discussions amongst the community. He is also one of the most-rated-answer authors of questions about Test Automation Frameworks (WebDriver) on Stack Overflow.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 26 Jun 2017
Article Copyright 2017 by Anton Angelov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid