Resolved How to create abstraction of a file which has too many methods

Palak Shah

Well-known member
Joined
Apr 29, 2020
Messages
97
Programming Experience
1-3
Hello Everyone,

I have one file which is of common method and has too many methods inside a single class file, now I want to create hierarchy so it can be divided further with the help of abstraction, I tried few things but then I got confused on how to access different methods?

Main Common Methods file which has lot of methods:
using Base;
using Constants;
using Entities;
using Extensions;
using SeleniumExtensions;
using Helpers;
using Model;
using AtlasNav;
using Model.PaymentOptions;
using Pages.DesignPages.DesignViews;
using Pages.UserAccount_Pages;
using Repositories;
using Pages;
using Pages.CheckoutPages;
using Pages.CMSPages;
using Pages.KlarnaCheckoutPages;
using Pages.SellerStorePages;
using Framework.DataAccessLayer.EntitiyFramework.Repositories;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using TestConfigurationCDO = Config.TestConfigurationCDO;

namespace ManualRegressionSuite.CommonMethods
{   public class CommonMethods : Base
    {
        public TestConfigurationCDO _testConfiguration;
        WebDriverWait wait;
        public IList<Site> _sites;
        public string _bolUkSiteUrl;
        public string _cmsSiteUrl;
        public Site _site;

        public CommonMethods(SharedParameters test_properties)
        {
            this.shared_parameters = test_properties;
            this.Driver = test_properties.Driver;
            this._testConfiguration = test_properties.testConfiguration;
            this._sites = test_properties.sites;
            this._bolUkSiteUrl = test_properties.SiteBag.domain_url;
            this._cmsSiteUrl = test_properties.SiteBag.cms_url;
            this._site = test_properties.site;
        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_SellerLogIn()
        {
            var site = shared_parameters.site.GetName();

            var homePage = GetInstance<HomePage>();
            homePage.To_SignInPage(site);

            var signInPage = GetInstance<SignInPage>();
            signInPage.To_SellerLandingPage();
        }
        /// <summary>
        /// Below method will navigate to Product Page through Atlas Navigation
        /// </summary>
        /// <param name="productId"></param>
        public void CommonMetho_GoToProductPage(int productId)
        {
            var homePage = GetInstance<HomePage>();
            homePage.To_ProductPage(productId);
        }
        /// <summary>
        /// Below method will:
        ///
        /// </summary>
        /// <param name="param"></param>
        public void CommonMethod_DesignProduct(WorkflowParameters param)
        {
            var generalProductPage = GetInstance<MasterProductPage>();
            generalProductPage.To_DesignPage();

            var designPage = GetInstance<DesignPage, DesignPageArgs>(new DesignPageArgs());
            designPage.basketToolbarView.UpdateProductQuantity(param.quantity.ToString());
            
            designPage.Design(param.designMethod);
        }
        /// <summary>
        /// Below method will Add product to Basket
        /// </summary>
        public void CommonMethod_AddToBasket()
        {
            var designPage = GetInstance<DesignPage, DesignPageArgs>(new DesignPageArgs());
            designPage.AddToCart();
        }
        /// <summary>
        /// Below method will review the basket item:
        /// </summary>
        /// <param name="productId"></param>
        public void CommonMethod_BasketReview(int productId)
        {
            var basketPage = GetInstance<BasketPage, BasketPageArgs>(new BasketPageArgs());

            var check = basketPage.BasketPriceView.CheckBasketPrice();
            Assert.IsTrue(check);

            var isPresent = basketPage.BasketItemsView.Validate_ItemIsInBasket(productId);
            Assert.IsTrue(isPresent);

            Assert.IsTrue(!ImageHelpers.IsEmpty(basketPage.BasketItemsView.GetItemImage(productId)));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public string CommonMethod_PurchaseItem(WorkflowParameters param)
        {
            var basketPage = GetInstance<BasketPage, BasketPageArgs>(new BasketPageArgs());

            basketPage.To_CheckoutPage(param.paymentOptions.portal);

            var checkoutPage = GetInstance<CheckoutPage>();
            checkoutPage.To_OrderSummaryPage(param.paymentOptions);
            
            var orderConfirmPage = GetInstance<OrderConfirmPage>();
            var _order = orderConfirmPage.To_Self_CheckOrderDatabase();

            return _order.OrderId.ToString(); ;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="OrderId"></param>
        public void CommonMethod_CMSVerification(string OrderId)
        {
            var cmsLoginPage = GetInstance<CMSLoginPage>();
            cmsLoginPage.To_CMSLandingPage();

            var cmsLandingPage = GetInstance<CMSLandingPage>();
            cmsLandingPage.To_OrderPage_OrderCheck(OrderId);

            var cmsOrderPage = GetInstance<CMSOrderPage>();
            cmsOrderPage.orderDetailsView.To_Self_CheckPrices(shared_parameters.price);
            
            var check = cmsOrderPage.orderDetailsView.IsOrderAuthorised(OrderId.ToInt());
            Assert.IsTrue(check);
        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_CMSDespatchNoteVerification()
        {
            var cmsOrderPage = GetInstance<CMSOrderPage>();
            cmsOrderPage.ChangeStatus_ByDropDown(OrderStatus.Dispatched);

            cmsOrderPage.deliveryDetailsView.Check_DespatchNote();
        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_CreateStore()
        {
            var sellerLandingPage = GetInstance<SellerLandingPage>();
            sellerLandingPage.To_SellerMyStoresPage();

            var sellerMyStoresPage = GetInstance<SellerMyStoresPage>();
            var uniqueName = sellerMyStoresPage.To_Self_CreateStore();
        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_NavigateToMyProductsPage()
        {

        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_CreateProduct()
        {

        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_AddProductToStore()
        {

        }
    }
    public class WorkflowParameters
    {
        public int productId = default(int);
        public int orderId = default(int);
        public DesignMethod designMethod = default(DesignMethod);
        public bool signedIn = default(bool);
        public bool increaseBasketQty = default(bool);
        public bool signedInCMS = default(bool);
        public bool editable = default(bool);
        public Site _site = default(Site);
        public PaymentOptions paymentOptions = default(PaymentOptions);
        public bool checkOrderAuthorise = default(bool);
        public int quantity = default(int);
    }
}


Now I want to create hierarchy such that main file common method will have on definitions of method and 2 separate class files will be there one for CMS Common Methods and one for WebSite Common methods


CMSCommonMethod File:
using Base;
using Constants;
using Extensions;
using Pages.CMSPages;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManualRegressionSuite.CommonMethods
{
    public class CMSCommonMethods : Base
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="OrderId"></param>
        public void CommonMethod_CMSVerification(string OrderId)
        {
            var cmsLoginPage = GetInstance<CMSLoginPage>();
            cmsLoginPage.To_CMSLandingPage();

            var cmsLandingPage = GetInstance<CMSLandingPage>();
            cmsLandingPage.To_OrderPage_OrderCheck(OrderId);

            var cmsOrderPage = GetInstance<CMSOrderPage>();
            cmsOrderPage.orderDetailsView.To_Self_CheckPrices(shared_parameters.price);

            var check = cmsOrderPage.orderDetailsView.IsOrderAuthorised(OrderId.ToInt());
            Assert.IsTrue(check);
        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_CMSDespatchNoteVerification()
        {
            var cmsOrderPage = GetInstance<CMSOrderPage>();
            cmsOrderPage.ChangeStatus_ByDropDown(OrderStatus.Dispatched);

            cmsOrderPage.deliveryDetailsView.Check_DespatchNote();
        }
    }
}

WebSiteCommonMethod:
using Base;
using Entities;
using Helpers;
using Pages;
using Pages.CheckoutPages;
using Pages.SellerStorePages;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManualRegressionSuite.CommonMethods
{
    public class WebSiteCommonMethods:Base
    {
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_SellerLogIn()
        {
            var site = shared_parameters.site.GetName();

            var homePage = GetInstance<HomePage>();
            homePage.To_SignInPage(site);

            var signInPage = GetInstance<SignInPage>();
            signInPage.To_SellerLandingPage();
        }
        /// <summary>
        /// Below method will navigate to Product Page through Atlas Navigation
        /// </summary>
        /// <param name="productId"></param>
        public void CommonMetho_GoToProductPage(int productId)
        {
            var homePage = GetInstance<HomePage>();
            homePage.To_ProductPage(productId);
        }
        /// <summary>
        /// Below method will:
        ///
        /// </summary>
        /// <param name="param"></param>
        public void CommonMethod_DesignProduct(WorkflowParameters param)
        {
            var generalProductPage = GetInstance<MasterProductPage>();
            generalProductPage.To_DesignPage();

            var designPage = GetInstance<DesignPage, DesignPageArgs>(new DesignPageArgs());
            designPage.basketToolbarView.UpdateProductQuantity(param.quantity.ToString());

            designPage.Design(param.designMethod);
        }
        /// <summary>
        /// Below method will Add product to Basket
        /// </summary>
        public void CommonMethod_AddToBasket()
        {
            var designPage = GetInstance<DesignPage, DesignPageArgs>(new DesignPageArgs());
            designPage.AddToCart();
        }
        /// <summary>
        /// Below method will review the basket item:
        /// </summary>
        /// <param name="productId"></param>
        public void CommonMethod_BasketReview(int productId)
        {
            var basketPage = GetInstance<BasketPage, BasketPageArgs>(new BasketPageArgs());

            var check = basketPage.BasketPriceView.CheckBasketPrice();
            Assert.IsTrue(check);

            var isPresent = basketPage.BasketItemsView.Validate_ItemIsInBasket(productId);
            Assert.IsTrue(isPresent);

            Assert.IsTrue(!ImageHelpers.IsEmpty(basketPage.BasketItemsView.GetItemImage(productId)));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public string CommonMethod_PurchaseItem(WorkflowParameters param)
        {
            var basketPage = GetInstance<BasketPage, BasketPageArgs>(new BasketPageArgs());

            basketPage.To_CheckoutPage(param.paymentOptions.portal);

            var checkoutPage = GetInstance<CheckoutPage>();
            checkoutPage.To_OrderSummaryPage(param.paymentOptions);

            var orderConfirmPage = GetInstance<OrderConfirmPage>();
            var _order = orderConfirmPage.To_Self_CheckOrderDatabase();

            return _order.OrderId.ToString(); ;
        }

        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_CreateStore()
        {
            var sellerLandingPage = GetInstance<SellerLandingPage>();
            sellerLandingPage.To_SellerMyStoresPage();

            var sellerMyStoresPage = GetInstance<SellerMyStoresPage>();
            var uniqueName = sellerMyStoresPage.To_Self_CreateStore();
        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_NavigateToMyProductsPage()
        {
  
        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_CreateProduct()
        {

        }
        /// <summary>
        ///
        /// </summary>
        public void CommonMethod_AddProductToStore()
        {

        }
    }
}

I am not sure for such thing what could be the proper architecture so that if in future we want to scale, it won't have any issue
 
A partial class is not a design pattern or a class. Its a type to provide a right of use from other files; and hence the word partial : partial type - C# Reference see here for an example : Partial Classes in C# - GeeksforGeeks.

Aside from that, here is an old article which I sometimes link people regarding utesting. While admittedly, unit testing is not something I spend a lot of time doing. It's still a good read : How design patterns can help you in developing unit testing-enabled applications

But if you want something specific to unit testing, or patterns for any type of testing, I strongly advise you read : Marc Clifton's article here : Advanced Unit Test, Part V - Unit Test Patterns
 
I don't think there maybe a name for it. In C#
Isn't it nearly just like mocking?
and my plan to use it in a behavioral way to get some parallel processing done, rather than a structural way that the pattern is normally used
Ah yes, I recall how I ended up giving you that link now. It contained a pattern I was recommending you to use over your chosen pattern. Upon receiving the link, If I recall you found a more suitable pattern from those pages. that pattern is what you dumped into your Blake3 project.

It's since become @Skydiver's goto site for all pattern development lookup's. Mine too sometimes, although, I try keep them in my head! It's an underused resource which I hope more people will take the time to bookmark and read through.
 
Back
Top Bottom