Site Loader
Auckland, New Zealand
In the last post we designed our Page Object Model code with two different classes i.e. for EA page as well as Login Page, we tried to access them via casting, but little refactoring of code using generics makes code even more neat than it is, some of the issues with previous code is
  1. It require lot of boxing and unboxing as shown below
     ((EAPage)PropertiesCollection.currentPage).FillDetails(values.Initial, values.FirstName, values.MiddleName);
  2. Page Navigation is handled by creating an instance of the class for page each and every page.
Hence, in this post we will start our discussion by developing a mechanism to access our pages to address the following above issues, for that we are going to use Generics in C#

Generics in C#

  • Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. ​
  • For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations​
  • The main intention of using Generics is to maximize code reuse, type safety, and performance.
Here is the complete video of the above discussion Here is the complete code of the above video Properties Collection Class
    public class PropertiesCollection
    {
        private static BasePage _currentPage;

        public static BasePage currentPage
        {
            get { return _currentPage; }
            set
            {
                ScenarioContext.Current["class"] = value;
                _currentPage = ScenarioContext.Current.Get("class");
            }
        }
    }

Base Page Class
    public abstract class BasePage
    {
        public T As() where T : BasePage
        {
            return (T)this;
        }
    }
EA Steps
    [Binding]
    class EASteps
    {

        [Given(@"I typed the UserName and Password")]
        public void GivenITypedTheUserNameAndPassword(Table table)
        {
           dynamic values = table.CreateDynamicInstance();
           PropertiesCollection.currentPage.As().Login(values.UserName, values.Password);
        }

        [When(@"I enter all the details to EA Page and Click Save")]
        public void WhenIEnterAllTheDetailsToEAPageAndClickSave(Table table)
        {
            dynamic values = table.CreateDynamicInstance();

            PropertiesCollection.currentPage.As().FillDetails(values.Initial, values.FirstName, values.MiddleName);
        }

        [Then(@"I should see my details saved")]
        public void ThenIShouldSeeMyDetailsSaved(Table table)
        {
            dynamic values = table.CreateDynamicInstance();
            PropertiesCollection.currentPage.As().GetFilledDetails(values.Initial, values.FirstName, values.MiddleName);
        }
Login Steps
   [Binding]
    class LoginSteps
    {

        IWebDriver currentDriver = null;


        [Given(@"I have navigated to my application")]
        public void GivenIHaveNavigatedToMyApplication()
        {
            Browser.Current.Navigate().GoToUrl(ConfigurationManager.AppSettings["seleniumBaseUrl"]);
            currentDriver = Browser.Current;
            PropertiesCollection.currentPage = new LoginPage();
        }

        [Given(@"I typed the admin and admin")]
        public void GivenITypedTheAdminAndAdmin(string userName, string password)
        {
            PropertiesCollection.currentPage.As().Login(userName, password);
        }

        [When(@"I click login button")]
        public void WhenIClickLoginButton()
        {
            PropertiesCollection.currentPage = PropertiesCollection.currentPage.As().ClickLogin();
        }

        [Then(@"I should see the EA page")]
        public void ThenIShouldSeeTheEAPage()
        {
            PropertiesCollection.currentPage.As().IsLoggedIn();
        }

    }
Thanks for watching the video and reading the post !!! Please leave your comments and let me know if there is anything need to be improved in this post. Thanks, Karthik KK

Post Author: Karthik kk

Leave a Reply

Your email address will not be published. Required fields are marked *