Ninja QA

Inheriting Pages - C# Inheritance but with pages

In the previous article we covered the page classes. We showed that we could create a page class that contains one or more objects.
A question was asked recently around how do we share common objects across two different web applications.

 

The answer: Inheritance 

Imagine we have a 'Footer' that sits at the bottom of our page.
Some of the elements on the footer are identical on all of our web applications, except for a few, where some elements are specific to the application in question.

public class Footer : Page
    {
        public Footer() : base(By.Id("footerId"))
        {
            Console.WriteLine("The Footer has loaded on the page");
        }

        public Link TermsAndConditionsLink()
        {
            return new Link(By.Id("terms_conditions"));
        } 
    }

 

Instead of having 3 different page classes that have the Terms & Conditions link duplicated, we can use inheritance to share the Terms & Conditions link with the other two more specific types of footer.
This is nothing new, as we did it with our BaseElement and Specialized Objects.
Eg: BaseElement is shared across Link and Element.  This allows Link and Element to have access to the Verify Text and Get Text methods etc.

Lets imagine our specialized classes are called
SalesFooter and CallCenterFooter

public class SalesFooter : Footer
    {
        public SalesFooter() : base(By.Id("salesFooter"))
        {
            
        }

        public Element SalesDemoLink()
        {
            return new Element(By.Id("salesDemo"));
        }

    }

    public class CallCenterFooter : Footer
    {
        public CallCenterFooter()
            : base(By.Id("callCenterFooterId"))
        {

        }

        public Link CallTheCallCenter()
        {
            return new Link(By.Id("salesDemo"));
        }

    }



Both of these classes inherit from the 'Footer' class as such, they can share the TermsAndConditions link.
However, the SalesDemoLink cannot be shared to the CallCenterFooter class and vice versa.


Add comment

Loading